简体   繁体   English

如何从串口读取GPS数据

[英]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.我正在尝试从 BU-353S4 USB GPS 读取来自串行端口的数据。 I'm getting nothing as far as readable NMEA sentences.就可读的 NMEA 句子而言,我一无所获。 The GPS works perfectly with a Raspberry Pi. GPS 与 Raspberry Pi 完美搭配。

This is for a .NET console application.这适用于 .NET 控制台应用程序。 There are similar questions all over the web, but none of the samples seem to work. web 中到处都有类似的问题,但似乎没有一个示例有效。

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? ? ?)))))(((((#Y?""???????? ??z ?----------?D??

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

Turns out the GPS was sending SiRF instead of NMEA.原来 GPS 发送的是 SiRF 而不是 NMEA。 Once I followed the steps in this SuperUser post to switch it to NMEA, everything worked perfectly!一旦我按照此 SuperUser 帖子中的步骤将其切换到 NMEA,一切正常!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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