2013-11-28 99 views
0

我想通过使用C#中的GPS通过串行端口获取GPS数据。我使用ParseNMEA类来获取NMEA数据,解析它并获得$ GPGAA。这是班级。如何通过C#中的串行端口读取GPS数据?

public class ParseNMEA 
{ 
    private SerialPort _port; 
    private byte[] _buffer; 

    public string GetGpgga(string portname, int baudrate, Parity parity, int databits, StopBits stopbits) 
    { 
//Set serial-port 
     _port = new SerialPort(); 
     _port.PortName = portname; 
     _port.BaudRate = baudrate; 
     _port.Parity = parity; 
     _port.DataBits = databits; 
     _port.StopBits = stopbits; 
     _port.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); 
     _port.Open(); 

//Parse buffer 
     string sdata = ""; 
     Encoding encoding = ASCIIEncoding.GetEncoding(1252); 
     if (null != _buffer) 
     { 
      sdata = encoding.GetString(_buffer); 
     } 
     string[] string_array = sdata.Split('$'); 
     string Gpgga = null; 
     for (int i = 0; i < string_array.Length; i++) 
     { 
      string stringTemp = string_array[i]; 
      string[] line_array = stringTemp.Split(','); 
      if (line_array[0] == "GPGGA") 
      { 
       Gpgga = string.Join(",", string_array[i]); 
      } 
     } 
     return Gpgga; 

    } 

    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     SerialPort port = (SerialPort)sender; 
     _buffer = new byte[port.BytesToRead]; 
     port.Read(_buffer, 0, _buffer.Length); 
    } 
} 

我想在窗体中调用这个类。像这样:

private ParseNMEA _parse; 
_parse = new ParseNMEA(); 
private void button_start_click(object sender, EventArgs e) 
{ 
     string gpgga = _parse.GetGpgga(comport, baudrate, parity, databits, stopbits); 
     textBox1.Text = gpgga; 
} 

但这不起作用。我认为SerialPort_DataReceived事件有任何问题。如果你有任何想法。请帮帮我。

谢谢。

回答

0

数据是从设备作为字符串还是字节传输的?您正在读取数据,好像它是以字节形式出现的,但是您将其转换为字符串值。如果以字符串形式出现,只需使用port.ReadLine()方法,但要确保将port.NewLine属性设置为与设备传输的任何换行符或字符串相对应。或者你可以使用port.ReadTo()方法并指定你正在查找的字符串。你是否收到任何数据?如果没有找出你的设备需要什么类型的握手,并设置port.Handshake属性来匹配。

+0

感谢您的评论。我以字节的形式获取数据。 – user3021107

0

GetGpgga()你不会出现等待要接收的数据 - 我期望更多的东西一样(未经测试):

public class ParseNMEA 
{ 
    private SerialPort _port; 
    private byte[] _buffer; 

    public ParseNMEA() 
    { 
     //Set serial-port 
     _port = new SerialPort(); 
     _port.PortName = portname; 
     _port.BaudRate = baudrate; 
     _port.Parity = parity; 
     _port.DataBits = databits; 
     _port.StopBits = stopbits; 
     _port.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); 
     _port.Open(); 
    } 

    public string LastGpgga { get; set; } 

    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     SerialPort port = (SerialPort)sender; 
     _buffer = new byte[port.BytesToRead]; 
     port.Read(_buffer, 0, _buffer.Length); 

     //Parse buffer 
     string sdata = ""; 
     Encoding encoding = ASCIIEncoding.GetEncoding(1252); 
     if (null != _buffer) 
     { 
      sdata = encoding.GetString(_buffer); 
     } 
     string[] string_array = sdata.Split('$'); 
     string Gpgga = null; 
     for (int i = 0; i < string_array.Length; i++) 
     { 
      string stringTemp = string_array[i]; 
      string[] line_array = stringTemp.Split(','); 
      if (line_array[0] == "GPGGA") 
      { 
       Gpgga = string.Join(",", string_array[i]); 
      } 
     } 
     this.LastGpgga = Gpgga; 
    } 
} 
+0

谢谢,但这不起作用。虽然我可以先得到_buffer并解析它,但我无法获得下一个_buffer。 – user3021107

+0

不知道我从理论上讲,代码应该始终在属性中包含最新的Gpgga - 所以属性应该随时间变化(尝试在调试器中设置一个断点,在“SerialPort_DataReceived”事件中,并且它应该被触发定期) –