如何通过C#中的串口读取GPS数据?

时间:2013-11-28 09:09:45

标签: c# gps serial-port

我想在C#中使用GPS通过串口获取GPS数据。我创建了ParseNMEA类以获取NMEA数据,解析它并获得$ GPGAA。这是班级。

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);
    }
}

我想在Form中调用此类。像这样:

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事件有任何问题。如果你有任何想法。请帮帮我。

谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;
    }
}