与 u-blox gps 的 C# 串行通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/284285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:36:29  来源:igfitidea点击:

C# serial communication with u-blox gps

c#serial-portgpsusbserial

提问by user36927

I have a GPS from u-blox.com with a USB-connection and driver. The driver installs a virual COM port that pops up when you plug the USB in. Using a hyperterminal I can then watch the flow of data from the GPS.

我有一个来自 u-blox.com 的 GPS,带有 USB 连接和驱动程序。驱动程序安装了一个虚拟 COM 端口,当您插入 USB 时会弹出该端口。然后我可以使用超级终端查看来自 GPS 的数据流。

Then I want the data in my program, not so easy...

然后我要我程序里的数据,没那么容易...

I have implemented some methods using the serialPort class to read from the GPS, but am unsuccessful. I have programmed several serial device readers and writers before in C#, but this one stops me.

我已经使用 serialPort 类实现了一些从 GPS 读取的方法,但没有成功。我之前用 C# 编写了几个串行设备读取器和写入器,但这个阻止了我。

As an example, the simple code in simpleSerialwill not give you anything unless you unplug and replug the USB.

作为一个例子,在简单的代码simpleSerial除非你拔下,然后重新插入USB不会给你任何东西。

Have tried reading it with matlab, which works great, but as the rest of my program that needs the GPS data is in c#, that doesn't quite fix the problem.

曾尝试用 matlab 阅读它,效果很好,但由于我的程序的其余部分需要 GPS 数据在 c# 中,这并不能完全解决问题。

Is there some high level C# things going on in the serialPort class that I can circumvent? Or is there any known problems reading USB-serialports, which I assume works like my GPS?

在 serialPort 类中是否有一些我可以规避的高级 C# 事情?或者在读取 USB 串行端口时是否存在任何已知问题,我认为它的工作原理与我的 GPS 一样?

回答by ChrisN

If you can communicate with the GPS using HyperTerminal then in principle there's no reason why you shouldn't be able to in C#. Are you sure you've configured the serial port correctly, particularly the baud rate, data bits, stop bits, parity, and flow control settings?

如果您可以使用超级终端与 GPS 进行通信,那么原则上您没有理由不能在 C# 中进行通信。你确定你已经正确配置了串口,特别是波特率、数据位、停止位、奇偶校验和流量控制设置吗?

You could use SysInternals PortMontool to look at the low-level I/O and compare how HyperTerminal and your C# program each configure the serial port. Maybe this will provide some useful information.

您可以使用SysInternals PortMon工具查看低级 I/O,并比较 HyperTerminal 和您的 C# 程序各自如何配置串行端口。也许这会提供一些有用的信息。

回答by sindre j

I have sucessfully used the SerialPort class in .Net 2 also with gps on a virtual comport. It seems your virtual comport driver is slightly off. If you can't locate a newer driver I would suggest you call the WinAPI functions for reading the serial port.

我已经成功地在 .Net 2 中使用了 SerialPort 类,并在虚拟端口上使用了 gps。看来你的虚拟通讯驱动程序有点不对劲。如果您找不到更新的驱动程序,我建议您调用 WinAPI 函数来读取串行端口。

Take a look at this code for instance: http://www.codeproject.com/KB/system/SerialPortComm.aspx

看看这个代码,例如:http: //www.codeproject.com/KB/system/SerialPortComm.aspx

回答by Jon Norton

I ran into a similar problem in an application I was writing and the problem turned out to be that I was trying to us SerialPort.WriteLinewhich sends a \r\nto end the line when I really needed to just send \n. When I switched to SerialPort.Writewith a \nappended to the end, everything worked as in HyperTerminal.

我在我正在编写的应用程序中遇到了类似的问题,结果问题是我试图向我们SerialPort.WriteLine发送 a\r\n来结束线路,而当我真的需要只发送\n. 当我切换到SerialPort.Write一个\n附加到最后,一切都按在超级终端。

回答by Jon Norton

Not sure if this is the same issue you are experiencing.

不确定这是否与您遇到的问题相同。

I noticed that if the RX_FLAG is used with SetCommMask API, the GetOverlappedResult does not seems to returns after the first WaitCommEvent. It will appears that the API is waiting for something to happens until the com mask is reset (At least that is what I observed). However, there is still some data received if you go and read the port after which, the device does not respond any more. In this scenario, you would need to unplug and replug the device to get it to respond again.

我注意到如果 RX_FLAG 与 SetCommMask API 一起使用,GetOverlappedResult 似乎不会在第一个 WaitCommEvent 之后返回。看起来 API 正在等待某些事情发生,直到重置 com 掩码(至少这是我观察到的)。但是,如果您去读取端口,仍然会收到一些数据,然后设备不再响应。在这种情况下,您需要拔下并重新插入设备以使其再次响应。

However, if the RX_CHAR is used instead, the GetOverLappedResult returns with the correct status and the device works as per normal.

但是,如果改为使用 RX_CHAR,则 GetOverLappedResult 返回正确的状态,并且设备正常工作。

Hope this information helps.

希望这些信息有帮助。

回答by joshperry

The problem is that there is a bug in either the USB Serial port drivers or in Microsofts implementation of their SerialPort classes. I have had the exact same issues with USB to Serial adapters using chips made by Prolific (and hence drivers by Prolific).

问题是 USB 串行端口驱动程序或其 SerialPort 类的 Microsoft 实现中存在错误。我在使用 Prolific 制造的芯片(以及 Prolific 的驱动程序)的 USB 到串行适配器方面遇到了完全相同的问题。

I have not been able to narrow down the issue exactly but now that the .NET Framework source-code is available I will be trying to step into SerialPort and see what the problem is exactly.

我无法准确地缩小问题的范围,但是现在 .NET Framework 源代码可用,我将尝试进入 SerialPort 并查看问题究竟出在哪里。

A work around is to use a different Serial access class. Before .Net 2.0 I had written a SerialStream class and for some reason it seems to work fine with these same USB to Serial Adapters that the SerialPort class will not work with.

解决方法是使用不同的串行访问类。在 .Net 2.0 之前,我编写了一个 SerialStream 类,出于某种原因,它似乎可以与 SerialPort 类无法使用的这些相同的 USB 到串行适配器一起正常工作。

You can grab my class at http://www.6bit.com/download/SerialStream.zipI've provided this same code to some fellow GPS programmers over the years and they have also had success with it.

你可以在http://www.6bit.com/download/SerialStream.zip 上我的课程。多年来我已经向一些 GPS 程序员同行提供了同样的代码,他们也取得了成功。

回答by Dan McClain

I have used the following code to communicate over USB to an Arduino, it send and receives a byte from the Arduino. It's simple, and isn't talking to a GPS, but hopefully it will help somehow!

我使用以下代码通过 USB 与 Arduino 通信,它从 Arduino 发送和接收一个字节。这很简单,并且不与 GPS 通话,但希望它会以某种方式有所帮助!

    public static byte ReadByte()
    {
        byte byteRead = new byte();
        SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        port.Open();
        int byteValue = port.ReadByte();
        port.Close();

        byteRead = Convert.ToByte(byteValue);

        return byteRead;
    }

    public static void SendByte(byte packet)
    {
        SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        port.Open();
        byte[] writeByte = new byte[1];
        writeByte[0] = packet;
        port.Write(writeByte, 0, 1);
        port.Close();
    }

回答by Dan McClain

Got the same problem with a U-Blox GPS but managed to solve it by enabling the DTR and RTS properties. Now I can read the NMEA sentences just fine. One problem I found is that I still have to remove and replug my GPS to the USB port from time to time. Hope it helps.

U-Blox GPS 遇到了同样的问题,但通过启用 DTR 和 RTS 属性设法解决了它。现在我可以很好地阅读 NMEA 句子了。我发现的一个问题是,我仍然需要不时将 GPS 移除并重新插入 USB 端口。希望能帮助到你。

回答by Dan McClain

Got this from u-blox support.

从 u-blox 支持部门获得此信息。

The following hex command will force a watchdog reset on the GPS module.

以下十六进制命令将强制 GPS 模块上的看门狗复位。

B5 62 06 04 04 00 00 00 00 00 0E 64

this a low level command for the ARM7 uP in the GPS It works with all u-blox GPS receiver.

B5 62 06 04 04 00 00 00 00 00 0E 64

这是 GPS 中 ARM7 uP 的低级命令 它适用于所有 u-blox GPS 接收器。

byte[] data = new byte[]{0xB5, 0x62, 0x06, 0x04, 0x04, 0x00,
                         0x00, 0x00, 0x00, 0x00, 0x0E, 0x64}; 
sp.Write(data, 0, data.Length);

It works for me.

这个对我有用。

回答by Juan Liska

Try using handshakes/flow-control. They are also useful when employing a usb-serial adapter.

尝试使用握手/流量控制。在使用 USB 串行适配器时,它们也很有用。