'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 1:[1]
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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeremy Caney |
