简体   繁体   中英

How to read GPS data from serial port

I'm trying to read the data coming in on a serial port from a BU-353S4 USB GPS. I'm getting nothing as far as readable NMEA sentences. The GPS works perfectly with a Raspberry Pi.

This is for a .NET console application. There are similar questions all over the web, but none of the samples seem to work.

var port = new SerialPort
{
    PortName = "COM5",
    BaudRate = 4800,
    Parity = Parity.None,
    DataBits = 8,
    StopBits = StopBits.One,
};
port.DataReceived += Port_DataReceived;


private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string line = "";
    SerialPort port = (SerialPort)sender;
    line = port.ReadExisting();
    Console.Write(line);
}

and...

private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort port = (SerialPort)sender;
    var count = port.BytesToRead;
    var buffer = new byte[count];
    port.Read(buffer, 0, count);
    var line = Encoding.ASCII.GetString(buffer);
    Console.Write(line);
}

No matter what I try, I end up with something like:

?)))))(((((#Y?""???????? ??z ?----------?D? ?

?J?&%%%%%%%%%?? ?%$$$$$$$$$f Qx?++++******?? ? ? #???? ) xm???? =?? ? ????? ???? ]? t? D0?? ????? 3 4???? 2\

Turns out the GPS was sending SiRF instead of NMEA. Once I followed the steps in this SuperUser post to switch it to NMEA, everything worked perfectly!

The issue has to do with the BAUD RATE. Try changing your value to one that's compatible with your device.

I was receiving this error, and this is what I did to solve the issue:

_GPSReceiver = new SerialPort("COM9");
_GPSReceiver.ReceivedBytesThreshold = 1024;
_GPSReceiver.ReadTimeout = 5000;
_GPSReceiver.BaudRate = 4800;
_GPSReceiver.Parity = Parity.None;
_GPSReceiver.StopBits = StopBits.One;
_GPSReceiver.DataBits = 7;
_GPSReceiver.Handshake = Handshake.None;
_GPSReceiver.Encoding = ASCIIEncoding.ASCII;

_GPSReceiver.DataReceived += new SerialDataReceivedEventHandler(GPSReceiver_DataReceived);
_GPSReceiver.Open();

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