我正在尝试为GPS设备编写自定义应用程序,并且需要读取GPS数据。

我面临的问题是我无法打开GPS端口。我知道它是哪个COM,也知道波特率,但是每当进入IOException方法时,都会得到一个Open()

port = new SerialPort("COM6", 9600);
port.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
try
{
    port.Open();
}
catch (IOException ex)
{
    SetLabel(label1, ex.Message);
}


堆栈跟踪:

at System.IO.Ports.SerialStream.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at PortTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at PortTest.Program.Main()


令人非常恼火的是,我发现确实可以工作的示例本机C ++代码可以毫无问题地打开端口,甚至可以看一眼NMEA字符串。但是,所有这些都是使用Web上提供的示例C ++代码完成的,而且我还不足以使我的整个应用程序以此语言为基础。我想坚持使用C#。

还有其他方法可以使用Windows CE中的SerialPort类在C#中打开端口吗?还是完全使用其他类?
如果不是,是否有C ++编写的DLL可以允许Windows CE使用相同(或相似)的功能?

编辑(更多详细信息):
我得到的例外就是。 IOException。 Visual Studio调试器仅告诉我。我不知道这是否是由于设备上的某些Windows CE设置错误所致。我确实记得Windows Mobile设备上的异常字符串有问题,但是通过添加对System.SR的引用解决了该问题,在本例中我已经尝试过。如果我在Windows CE和异常消息方面缺少一些技巧,我也很想知道。 ;)

我也尝试使用没有事件的代码,并通过在构造函数中指定更多参数,并且在尝试打开端口时总是会得到异常。

最后,我已经尝试过在端口名称中添加:,但这也无济于事。

最佳答案

我建议为构造函数提供更多选项;奇偶校验,停止位和握手模式。

SerialPort port = new SerialPort ("COM6", 9600, Parity.None, 8, StopBits.One);
port.Handshake = Handshake.None;

10-01 21:34
查看更多