与NXP的FRDM-KW40Z EV的串行接口:读取ADC O/P时出现问题. [英] Serial Interface with NXP's FRDM-KW40Z EV: Problem reading ADC O/P.

查看:38
本文介绍了与NXP的FRDM-KW40Z EV的串行接口:读取ADC O/P时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello, 

我一直在尝试我的第一个串行通信协议.我正在使用恩智浦的FRDM-KW40Z板.我正在运行一个ADC演示,该演示在Putty终端窗口中显示温度.但是我无法在编写的GUI中获得温度.在终端 在窗口中,我收到一条文本:按任意键开始测量温度".然后,当我按任意键时都会显示温度.我在GUI中得到了相同的文本,但是无法使用键盘输入来查看温度.我什至没有建立一个序列 沟通还是第一步本身失败了?你们能帮我发现问题吗?美好的一天.

I've been trying out my first serial communication protocol. I'm working with NXP's FRDM-KW40Z board. I'm running an ADC demo that displays the temperature in the Putty terminal window. But I can't get the temperature in the GUI I scripted. In the terminal window, I get a text: "press any key to start measure temperature" and after that, the temperature is displayed when I press any key. I get the same text in my GUI, but I cannot use the key input to view the temperature. Did I even establish a serial communication or did I fail at the first step itself? Could you guys please help me to spot the problem? Good day.

using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace Serial_receive
{
    public partial class Form1 : Form
    {
        // All members variables should be placed here
        // make it more readable, hopefully!
        string t;
        SerialPort sp;
        private static readonly EventHandler DoUpdate;

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;

            // User can already search for ports when the constructor of the FORM1 is calling 
            // And let the user search ports again with a click
            // Searching for ports function

            SearchPorts();
        }
       //search button  
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            SearchPorts();
        }
        void SearchPorts()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }
  
        private void button2_Click(object sender, EventArgs e)
        {
       // Catch exception if it will be thrown so the user will see it in a message box
            OpenCloseSerial();
        }      
        void OpenCloseSerial()
        {
            try
            {
                if (sp == null || sp.IsOpen == false)
                {
                    t = comboBox1.Text.ToString();
                    sErial(t);
                    button2.Text = "Close Serial port"; // button text
                }
                else
                {
                    sp.Close();
                    button2.Text = "Connect and wait for inputs";   // button text

                }
            }
            catch (Exception err)   // catching error message
            {
                MessageBox.Show(err.Message);   // displaying error message
            }           
        }
      
        void sErial(string Port_name)
        {
            try
            {
                sp = new SerialPort(Port_name, 115200, Parity.None, 8, StopBits.One);   // serial port parameters
                sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                sp.DtrEnable = true;
                sp.Open();

                Console.Read(); //??

            }
            catch (Exception err)
            {
                throw (new SystemException(err.Message));
            }
        }
        //

        private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            
            Console.ReadKey();
            string indata = sp.ReadExisting();

            //retrieve number of bytes in the buffer
            int bytes = sp.BytesToRead;
            //create a byte array to hold the awaiting data
            byte[] comBuffer = new byte[bytes];
            //read the data and store it
            sp.Read(comBuffer, 0, bytes);
                              
            sp.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            //Control.Invoke();
            //SerialDataReceivedEventHandler(sp_DataReceived);

        }

        //
        private  void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
        {

            //This below line is not need , sp is global (belongs to the class!!)
            //SerialPort sp = (SerialPort)sender;
            if (e.EventType == SerialData.Chars)
            {
                if (sp.IsOpen)
                {
                    string w = sp.ReadExisting();
                    if (w != String.Empty)
                    {
                        Invoke(new Action(() => richTextBox1.AppendText(w)));
                    }
                }
                Console.WriteLine("Data Received:");
                Console.Write(SerialData.Chars);
                
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (sp == null || sp.IsOpen == false)
            {
                OpenCloseSerial();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Serial Channel to FRDM-KW40Z";
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            Refresh();
        }
        //-------------------------
        }

    }


推荐答案

控制台读命令在Form中不起作用,我想您已经发现了.而且,虽然Console.Read()会被简单地忽略,但Console.ReadKey()会引发异常.您为什么仍需要阅读?还有其他检测键盘的方法 按Form,但是也许您真的不需要该功能.

Console Read commands will not work in a Form, as I guess you've discovered. And while the Console.Read() will simply be ignored, the Console.ReadKey() will throw an exception.  Why do you need the Reads anyway? There are other ways to detect keyboard presses in a Form, but perhaps you really don't need that functionality.

在Visual Studio的输出"窗口中进行调试时,控制台写入"命令将显示您的文本.我看到您已经将接收到的数据放入RichTextBox中,所以很好.

The Console Write commands will show your text while you're debugging in Visual Studio's Output window. I see that you've put the data received in your RichTextBox, so that's good.

如果您不确定是否要打开端口,可以在代码中放置几个​​断点并对其进行调试.看来您的代码中可能已经有足够的try/catch结构,您可能会知道它是否无法正常运行, 但同样,断点和调试可能会帮助您进一步跟踪.

If you're in doubt about whether or not you've even opening the port, you could put a few breakpoints in the code and debug it. It seems that you've probably got enough try/catch constructs in your code that you would probably know if it didn't run properly, but again, breakpoints and debug might help you track it down further.


这篇关于与NXP的FRDM-KW40Z EV的串行接口:读取ADC O/P时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆