我遇到的问题是无法打开GPS端口。我知道它是哪个COM口,也知道波特率,但是每当我到达
Open()
方法时,就会出现IOException
。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++编写的DLL可用于Windows CE,以允许相同(或类似)的功能?
编辑(更多细节):我收到的异常就是IOException。 Visual Studio调试器没有告诉我更多信息。我不知道这是否是由于设备上某些失败的Windows CE设置引起的。我确实记得我的Windows Mobile设备上有一个关于异常字符串的问题,但是通过添加对System.SR的引用解决了该问题,我在这种情况下已经尝试过了。如果关于Windows CE和异常消息还有一些我不知道的诀窍,我也很想知道。 ;)
我还尝试过在不使用事件并指定更多构造函数参数的情况下使用代码,并且每次尝试打开端口时都会收到异常。
最后,我已经尝试将端口名称添加到冒号,这也没有帮助。