2010-04-16 188 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