C#ReadExisting()问题中的串口? [英] Serial Ports in C# ReadExisting() issue?
问题描述
大家好,
刚开始,调试一个使用
Hi All,
Just going through, debugging a program that uses
.ReadExisting()
我想知道这是否是为什么有些数据是不规则的,即它没有看到分隔符和字符串从单元返回的结尾。
我想知道使用ReadTo方法,即
I was wondering if this is why some of the data comes in irregularly i.e. it does not see the delimiter and the end of the string back from the unit.
I was wondering about using the ReadTo method i.e.
.ReadTo(">");
,因为发送的数据附加了>但昨天尝试这个导致了很多麻烦并且崩溃了PC(我开始怀疑这个板但它适用于HyperTerminal所以它必须是你的代码引用)。我用于发送接收的例程在底部详细说明,使用以下方法调用
as the data sent is appended with a ">" but trying this yesterday led to a lot of trouble and crashed PC (I'm starting to suspect the board "but it works with HyperTerminal so it must be your code" quote). The routine I am using for send receive is detailed at the bottom it is called with the following means
string ReadData;
Reply_Status = (int)REPLY.NO_REPLY;
ReadData = Write_Outstring(Command);
帮助!
格伦
HELP!
Glenn
private string Write_Outstring(Data_To_Unit)
{
string Data_From_Unit = "";
Reply_Status = (int)REPLY.NO_REPLY;
rtbCommsRecord.Text += Data_To_Unit + "\n";
myComPort.Write(Data_To_Unit + (char)10 + (char)13);
{
while (Reply_Status == (int)REPLY.NO_REPLY)
{
NoDataAtPort.Enabled = true;
}
NoDataAtPort.Enabled = false;
if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
{
Data_From_PitStop = "TIMEOUT\n";
}
else if (Reply_Status == (int)REPLY.YES_REPLY)
{
Data_From_Unit = myComPort.ReadExisting();
rtbCommsRecord.Text += Data_From_Unit; // +"\n";
rtbIncoming.Text = Data_From_Unit;
// rtbLogged.Text +=ReplyData;
if (Data_From_Unit.Contains("UD:"))
{
// rtbReturnedData.Text += Data_From_Unit + '\n' + '\r';
rtbUnlistedData.Text += Data_From_Unit+ '\n';// + '\r'; here is the problem
}
if (Data_From_Unit.Contains("LD:"))
{
// rtbReturnedData.Text += Data_From_Unit + '\n' + '\r';
rtbListedData.Text += Data_From_Unit;// +'\n';// + '\r';
}
if (Data_From_Unit.Contains("OK"))
{
rtbLogged.Text += rtbIncoming.Text;
}
rtbLogged.Text += rtbIncoming.Text;
}
else
{
MessageBox.Show("ERROR IN Pitstop Comms\n");
Reply_Status = (int)REPLY.NO_REPLY;
}
}
return (Data_From_Unit);
}
更新:ReadExisting()返回的数据不完整I通过粘贴一个消息框来缓慢/停止通信流程发现这一点,我想我需要一个如何使用.ReadTo()的例子如果我使用.ReadTo(>)会导致问题一些数据返回\\ \
\r>中和其他一些部分返回> \r>会.ReadTo(\ r和>)可能吗?
格伦
UPDATE: The Data being returned by ReadExisting() is not complete I found this by sticking a message box to slow/stop comms flow, I think I need an example of how to the above with .ReadTo() which will cause problems if I use .ReadTo(">") some data returns "\n\r>" and some other parts return ">\r>" would .ReadTo(\r and >) be possible?
Glenn
推荐答案
it`更好地读取串口数据接收事件中的数据。
那么在读取缓冲区中的所有数据之后使用(ReadExsting)方法读取缓冲区中的所有数据也会更好地解析你想要的字符串解析或分析!
it`s better to read data in serial port data receive event.
then it`s also better to read all in buffer using ( ReadExsting ) method after read all data in buffer try to parse string that you want to be parsed or analyzed !
这篇关于C#ReadExisting()问题中的串口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!