从串行端口读取二进制数据
我之前一直使用 C# 通过串行端口从 GPS 读取 NMEA 数据。现在我正在做类似的事情,但不是来自串行的 GPS。我正在尝试阅读 TNC 的 KISS 声明。我正在使用这个事件处理程序。
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
这是 port_DataReceived。
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = comport.ReadExisting();
sBuffer = data;
try
{
this.Invoke(new EventHandler(delegate { ProcessBuffer(sBuffer); }));
}
catch { }
}
我遇到的问题是每个语句都会多次调用该方法。因此,仅使用部分语句调用 ProcessBuffer 方法。我如何阅读整个声明?
I previously have been reading NMEA data from a GPS via a serial port using C#. Now I'm doing something similar, but instead of GPS from a serial. I'm attempting to read a KISS Statement from a TNC. I'm using this event handler.
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
Here is port_DataReceived.
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = comport.ReadExisting();
sBuffer = data;
try
{
this.Invoke(new EventHandler(delegate { ProcessBuffer(sBuffer); }));
}
catch { }
}
The problem I'm having is that the method is being called several times per statement. So the ProcessBuffer method is being called with only a partial statment. How can I read the whole statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
串行通信允许通过使用超时将数据流分解为消息。但以下
KISS TNC 本协议中没有提供此类功能。
我的建议是通过解码帧结束字符将数据流分解为消息。
Serial communication allows to break data flow into messages by using timeout. But following
KISS TNC there no such functionality is presented in this protocol.
My suggestion is to break data stream into messages by decoding Frame End characters.
volody 是对的:您必须寻找 FEND (0xC0),并且只有在看到它时才尝试处理缓冲区。
volody is right: You have to look for the FEND (0xC0) and only try to process the buffer when you see it.
请参阅此
序列号 101
See this
Serial 101