Search code examples
c#serial-portgps

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\


Solution

  • 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!