如何发送“1”到串口,然后接收pic在收到“1”后发送给我的值。 [英] how to send "1" to a serial port and then receive the values the pic will be sending me after it received "1"

查看:108
本文介绍了如何发送“1”到串口,然后接收pic在收到“1”后发送给我的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以写一个字节到串口然后读取它会发给我的值吗?

i希望能够写入串口1

,当我的微型控制器收到这个1时,它会发给我稍后想要使用的portD的值



但是我不能与串口正确通信



这不是我正在尝试构建的程序,但我只测试了如何从/向读取和写入的基础知识一个串口,到目前为止没有任何工作:/



我只是测试如何与串口通信,这是我正在尝试的代码



i有一个按钮,当我点击我想要发送1然后textbox1将显示收到的值



can i write a byte to the serial port and then read the values it 'll send me ?
i want to be able to write to the serial port "1"
and when my pic micro controller receives this "1" it will sends me the values of portD in which i want to use later

but i 'm not able to communicate correctly with the serial port

this is not the program i'm trying to build but i' just testing the basics on how to read and write from/to a serial port and nothing is working so far :/

i'm just testing how to communicate with the serial port and this is the code i'm trying

i have a button that when i click i want a "1" to be sent and then the textbox1 will display the value received

SerialPort serialPort1 = new SerialPort("COM2", 300, Parity.None, 8, StopBits.One);
        
        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("1");
            serialPort1.Close();

            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Open();

            }
            else
            {
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Open(); //opens the port
            }

        }
        int rs;

        private void serialPort1_DataReceived(object sender,SerialDataReceivedEventArgs e)
        {
            SerialPort serialPort1=(SerialPort)sender;
            try
            {

                rs = serialPort1.ReadByte();
                this.Invoke(new EventHandler(type));
            }
            catch (System.TimeoutException)
            {
            }


        }
        private void type(object s,EventArgs e)
        {
            textBox1.Text = rs.ToString();
        }

推荐答案

首先,停止打开和关闭端口:它没有帮助,它可能会导致数据丢失。当端口关闭时,物理端口接收的任何内容都将被丢弃......并且它允许其他应用程序抓取端口。并且您不需要事件处理程序来传递数据。



因此,打开端口一次,添加DataReceived处理程序一次,然后发送数据。 />
然后使用该事件获取PIC返回的所有数据并显示它 - 但不要使用int来传输它,因为RX线程可以在UI线程有机会之前轻松覆盖它阅读并显示它。相反,一次读取所有字节,并通过Invoke方法调用将数据传递到UI线程:

Firstly, stop opening and closing the port: it won't help and it may cause data to be lost. While the port is closed, anything received by the physical port will be thrown away...and it allows other applications to "grab" the port as well. And you don't need an event handler to pass the data either.

So open the port once, add the DataReceived handler once, and send the data.
Then use the event to fetch all the data the PIC returns, and display it - but don't use an int to transfer it, as the RX thread could easily overwrite it before your UI thread gets a chance to read it and display it. Instead, read all the bytes in one go, and pass the data through to the UI thread via a Invoke method call:
string data = SerialPort1.ReadExisting();
Invoke(new MethodInvoker(() => { ShowData(data);}));




private void ShowData(string s)
   {
   textBox1.Text += s;
   }


这篇关于如何发送“1”到串口,然后接收pic在收到“1”后发送给我的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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