C#完成消息后读取串行端口数据
我想读化学分析仪通过串口发送的信息。我已经编写了以下代码。它读取接收到的字节,但在某些情况下,某些字节被合并,并且在消息的中途接收到一组意外的字节。有没有什么方法可以让我等到消息结束后再获取所有字节C#完成消息后读取串行端口数据,c#,serial-port,port,C#,Serial Port,Port,我想读化学分析仪通过串口发送的信息。我已经编写了以下代码。它读取接收到的字节,但在某些情况下,某些字节被合并,并且在消息的中途接收到一组意外的字节。有没有什么方法可以让我等到消息结束后再获取所有字节 SerialPort com = new SerialPort(); List<byte> thisMessage = new List<byte>(); string status = ""; private void btnOpen_Click_1(object se
SerialPort com = new SerialPort();
List<byte> thisMessage = new List<byte>();
string status = "";
private void btnOpen_Click_1(object sender, EventArgs e){
btnOpen.Enabled = false;
btnClose.Enabled = true;
try
{
com.PortName = cmbPort.Text;
com.BaudRate = Int32.Parse(txtBaudRate.Text);
com.DataBits = Int32.Parse(txtBitLength.Text);
com.ReadBufferSize = 100000;
com.StopBits = StopBits.One;
com.DtrEnable = true;
com.RtsEnable = true;
com.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
}
private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytes = com.BytesToRead;
byte[] buffer = new byte[bytes];
com.Read(buffer, 0, bytes);
if (ContainEnd(buffer))
{
status += DateTime.Now.ToString("dd MMM yyyy hh:mm:ss tt") + " Message Received From Analyzer." + Environment.NewLine;
thisMessage.AddRange(buffer);
String ts = "";
foreach (byte b in thisMessage)
{
ts += b + " ";
}
status += ts + Environment.NewLine;
thisMessage = new List<byte>();
status += "End of a Message " + Environment.NewLine;
}
else
{
thisMessage.AddRange(buffer);
String ts = "";
foreach (byte b in buffer)
{
ts += b + " ";
}
status += DateTime.Now.ToString("dd MMM yyyy hh:mm:ss tt") + " Part of a Message " + ts + Environment.NewLine;
}
this.Invoke(new EventHandler(DisplayText));
}
SerialPort com=new SerialPort();
List thisMessage=新列表();
字符串状态=”;
私有void btnOpen_Click_1(对象发送方,事件参数e){
btnOpen.Enabled=false;
btnClose.Enabled=true;
尝试
{
com.PortName=cmbPort.Text;
com.BaudRate=Int32.Parse(txtbudrate.Text);
com.DataBits=Int32.Parse(txtBitLength.Text);
com.ReadBufferSize=100000;
com.StopBits=StopBits.One;
com.DtrEnable=true;
com.RtsEnable=true;
com.Open();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message,“Message”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
com.DataReceived+=新的SerialDataReceivedEventHandler(com\u DataReceived);
}
private void com_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
int bytes=com.BytesToRead;
字节[]缓冲区=新字节[字节];
com.Read(缓冲区,0,字节);
if(ContainEnd(缓冲区))
{
状态+=DateTime.Now.ToString(“dd-MMM-yyy-hh:mm:ss-tt”)+“从Analyzer收到的消息。”+Environment.NewLine;
thisMessage.AddRange(缓冲区);
字符串ts=“”;
foreach(此消息中的字节b)
{
ts+=b+”;
}
status+=ts+Environment.NewLine;
thisMessage=新列表();
状态+=“消息结束”+Environment.NewLine;
}
其他的
{
thisMessage.AddRange(缓冲区);
字符串ts=“”;
foreach(缓冲区中的字节b)
{
ts+=b+”;
}
status+=DateTime.Now.ToString(“dd-MMM-yyy-hh:mm:ss-tt”)+“消息的一部分”+ts+Environment.NewLine;
}
调用(新的EventHandler(DisplayText));
}
当我运行这段代码时,我会得到像52219471814810594这样的字节
此处,信息的这一部分不需要字节2。不能有像194或181这样的字节。因此,我认为存在对字节的错误读取
如何从串行端口获取正确的字节?“如何从串行端口获取正确的字节?”当从串行端口读取时,获取的字节是发送的字节,有时可能存在垃圾数据(取决于硬件)。你必须处理它的数据,它们不会在消息的中间发送字节2。每个字节旁边不能有两个数字2字节。分析器发送的字节不是180。因此,读取字节时一定有错误,而不是由其他仪器发送字节时有错误