简体   繁体   中英

How to read GPS data via serial-port in C#?

I want to Get GPS data via Serial Port by using GPS in C#. I made ParseNMEA class to get NMEA data, parse it and get $GPGAA. This is the class.

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

I want to call this class in Form. Like this:

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

But this doesn't work. I think the SerialPort_DataReceived event has any problem. If you have any idea. Please help me.

Thank you.

Is the data being transmitted from the device as a string or as bytes? You are reading the data as if it were coming in bytes but then you convert it to a string value. If it comes as a string just use the port.ReadLine() method but be sure you set the port.NewLine property to correspond to whatever newline character or character series your device transmits. Or you could use the port.ReadTo() method and specify the string you are looking for. Are you getting any data at all? If not find out what type of handshake your device requires and set the port.Handshake property to match.

In GetGpgga() you don't appear to wait for data to be received - I'd expect something more like (untested):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM