RS232通讯,收到数据 [英] RS232 communication, Data received

查看:84
本文介绍了RS232通讯,收到数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。从我的问题中永久划分数据。下面的代码。谢谢。



HOST2发送消息样本:12634567897897TESTMESSAGE



HOST 1结果屏幕:



HOST1:港口开放。

HOST2:126345678978

HOST2:97TESTMESSAGE



收到的消息随机拆分。没有一行。



我的要求:



HOST1:港口是开放的。

HOST2:12634567897897TESTMESSAGE



是否可以这种方式



  private   string  indata; 

private void button1_Click( object sender,EventArgs e)
{
try
{
serialPort1.PortName = portBox.Text ;
serialPort1.BaudRate = 9600 ;
serialPort1.DataBits = 8 ;
serialPort1.StopBits = StopBits.One;
serialPort1.ReadTimeout = -1;
serialPort1.ReadBufferSize = 4096 ;
serialPort1.WriteTimeout = -1;
serialPort1.Parity = Parity.None;
serialPort1.Handshake = Handshake.None;
serialPort1.Open();
richTextBox1.Text + = HOST1:端口已打开。\\\ n;
}
catch // (exception ex)
{
richTextBox1.Text + = HOST1:Port不开放。\\\\ n;
}

}

private void button2_Click( object sender,EventArgs e)
{
serialPort1.Write( \ u0005 \\\\ n);
}

private void Form1_Load( object sender,EventArgs e)
{
portBox.Items.Clear();
string [] ports = SerialPort.GetPortNames();
foreach string Comport in ports)
{
portBox.Items.Add(Comport);
}
}

private void serialPort1_DataReceived( object sender,SerialDataReceivedEventArgs e)
{
indata = serialPort1.ReadExisting();

richTextBox1.Invoke((MethodInvoker) delegate {richTextBox1.Text + = HOST2: + indata + \ r \\ n \\ n;});

}

解决方案

您使用ReadExisting [ ^ ]用于读取数据。此函数立即返回返回输入缓冲区中实际可用的数据(可能甚至没有数据)。所以你应该收集收到的数据,直到收到某种数据结束或发生超时。



如果用新行终止答案,你可以使用< a href =https://msdn.microsoft.com/de-de/library/system.io.ports.serialport.readline%28v=vs.110%29.aspx> ReadLine [相反,^ 。但是你应该设置一个超时值,以避免在没有收到答案的情况下无限阻塞。


RS232很慢。 RS232接收字符1乘1.



您需要确保收到完整的消息。

要么你知道长度消息,或者您知道消息的结束字符,或者您只是等待超时告诉您消息已结束。



超时是强制性的,因为消息可以被打断。



我是C#专家,但1知道RS232。


Hi guys. Permanent division of the data from my problem. The code below. Thanks.

HOST2 send message sample : 12634567897897TESTMESSAGE

HOST 1 Result screen :

HOST1 : Port is open.
HOST2 : 126345678978
HOST2 : 97TESTMESSAGE

Received message randomly split. Not a single line.

My request:

HOST1 : Port is open.
HOST2 : 12634567897897TESTMESSAGE

Is it possible in this way

private string indata;

       private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               serialPort1.PortName = portBox.Text;
               serialPort1.BaudRate = 9600;
               serialPort1.DataBits = 8;
               serialPort1.StopBits = StopBits.One;
               serialPort1.ReadTimeout = -1;
               serialPort1.ReadBufferSize = 4096;
               serialPort1.WriteTimeout = -1;
               serialPort1.Parity = Parity.None;
               serialPort1.Handshake = Handshake.None;
               serialPort1.Open();
               richTextBox1.Text += "HOST1 : Port is open.\r\n";
           }
           catch//(Exception ex)
           {
               richTextBox1.Text += "HOST1 : Port is not open.\r\n";
           }

       }

       private void button2_Click(object sender, EventArgs e)
       {
           serialPort1.Write("\u0005\r\n");
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           portBox.Items.Clear();
           string[] ports = SerialPort.GetPortNames();
           foreach (string Comport in ports)
           {
               portBox.Items.Add(Comport);
           }
       }

       private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           indata = serialPort1.ReadExisting();

           richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.Text += "HOST2 : " + indata + "\r\n"; });

       }

解决方案

You use ReadExisting[^] for reading data. This function returns immediately returning the data actually available in the input buffer (which might be even no data). So you should collect received data until some kind of end of data is received or a timeout occurs.

When the answer is terminated with a new line, you might use ReadLine[^] instead. But you should set a timeout value then to avoid infinite blocking if no answer is received.


RS232 is way slow. RS232 receive chars 1 by 1.

You need to make sure that you have received the complete message.
Either you know the length of message, or you know the end char of the message, or you simply wait for a timeout to tell you that the message is over.

Timeout is mandatory since the message can be interrupted.

I am C# expert, but 1 know RS232.


这篇关于RS232通讯,收到数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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