read NMEA sentences from GPS

2019-04-11 05:16发布

问题:

I have a Mio P550 device, which has a GPS included. I try to use SerialPort to get NMEA sentences, by simply use SerialPort.Read(). Data is returned in some weird encoding. GPS should return NMEA sentences in ASCII, but it doesn't. Here is my code for reading:

                dataLength = this.serialPort.Read(buffor, 0, Gps.BUFFOR_LENGTH);
                Debug.WriteLine("data length: " + dataLength);

                if (dataLength > 0)
                {
                    for (int i = 0; i < dataLength; i++)
                    {
                        char c = Convert.ToChar(buffor[i]);

                        if (c == '\r' || c == '\n')
                        {
                            string data = stringBuilder.ToString();
                            Debug.WriteLine("data readed: " + data);

                            if (data.StartsWith("$GPGGA"))
                            {
                                this.OnLocationChanged(data);
                            }

                            stringBuilder.Length = 0;
                        }
                        else
                        {
                            stringBuilder.Append(c);
                        }

                        Debug.WriteLine("readed data: " + stringBuilder.ToString());
                    }

And here is sample value I get in return: xæææxfæ`æxæføøøxxxøx

I have also second device (Asus A636N), which return NMEA sentences in ASCII and my code works great.

What I have to do with Mio device to get NMEA sentences in ASCII? Or how I can get encoding for data returned by device? I try use all classes from System.Text.Encoding.xxx.GetString() to get string from readed bytes but it doesn't return right data - it returns data similar to sample above.

回答1:

You probably need to make sure the baud rate for your serial port is set to 4800 baud as required by the NMEA specification.



回答2:

see this code sample: https://code.google.com/p/avr-nmea-gps-library/

it also uses a 4800 baud rate.



标签: c# gps nmea