2010-04-16 189 views
0

我以前一直使用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方法仅以部分语句被调用。我怎样才能读完整个陈述?

回答

2

串行通信允许使用超时将数据流分解为消息。但是在 KISS TNC之后,这个协议没有提供这样的功能。

Each frame is both preceded and followed by a special FEND (Frame End) character, analogous to an HDLC flag. No CRC or checksum is provided. In addition, no RS-232C handshaking signals are employed.

我的建议是通过解码帧结束字符将数据流分解成消息。

0

volody是对的:您必须查找FEND(0xC0),并且只在您看到它时尝试处理缓冲区。

0

看到这个

Serial 101

+0

这篇文章是太棒了,但我用的时候运用他正在写关于编码挣扎。我在尝试定义串行端口时是这样的: private SerialPort comport = new SerialPort(); comport.Encoding.GetEncoding(“Windows-1252”); 在那里添加它给我一个无效的令牌错误。在我的阅读声明旁边。 comport.Encoding.GetEncoding(“Windows-1252”); string data = comport.ReadExisting(); 它给了我一个错误:不能用实例引用访问。我不知道在哪里使用这个。你能帮我吗? – rross 2010-04-26 14:42:21

+0

在VB 昏暗的SP作为新IO.Ports.SerialPort 私人小组的button1_Click(BYVAL发件人为System.Object的,BYVALË作为System.EventArgs)把手Button1.Click 前开放 SP '其他串行端口设置 '。 Encoding = System.Text.Encoding.GetEncoding(“windows-1252”) End Sub – dbasnett 2010-04-26 16:16:14