我正在嘗試爲GPS設備編寫自定義應用程序,而且我需要讀取GPS數據。Windows CE和串行端口問題(無法打開端口)
我面臨的問題是我無法打開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#中打開端口?或者也許完全使用其他類?
如果沒有,是否有可能允許Windows CE提供相同(或類似)功能的C++寫入DLL?
編輯(更多詳情): 我得到的例外就是這樣。 IOException異常。 Visual Studio調試器告訴我沒有更多。我不知道這是否是由於設備上一些粗糙的Windows CE設置造成的。我記得我的Windows Mobile設備上的異常字符串有問題,但是通過添加對System.SR
的引用解決了這個問題,我已經在這種情況下嘗試過。如果我錯過了一些關於Windows CE和異常消息的技巧,我也很想知道。 ;)
我也嘗試使用沒有事件的代碼,並通過在構造函數中指定更多參數,並且在嘗試打開端口時總是會遇到異常。
最後,我已經嘗試添加一個:到端口名稱,這也沒有幫助。
不要在標題中使用標籤。並檢查拼寫。請。乾杯:) – abatishchev 2010-11-26 08:11:49