我正在 Windows XP 嵌入版本上运行 .Net 3.5 应用程序。该应用程序通过串行 (COM) 端口写入和读取一些数据。该应用程序在我的笔记本电脑(Windows XP Professional)上运行良好,但在嵌入式 Windows XP 上运行不佳。我不断收到此错误:
System.IO.IOException:由于线程退出或应用程序请求,I/O 操作已中止。
可能是什么原因造成的?
一些附加信息:要阅读,我正在使用ReadExisting
并不是Readline
。另外,在阅读之前,我还要确保端口也已打开。
System.IO.IOException: The I/O operation has been aborted because of either a thread exit or an application request.
at System.IO.Ports.SerialStream.EndRead(IAsyncResult asyncResult)
at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.Ports.SerialPort.ReadExisting()
at ScalesApp.Scales.handleDataReceived(Object sender, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
问题在于 SetCommState 的 DCB 中启用了 fAbortOnError,这解释了 SerialPort 对象抛出的大多数 IOException。某些 PC/手持设备具有默认启用错误中止标志的 UART - 因此串行端口的 init 例程必须清除它(微软忽略了这一点)。 SerialPort 对象在设计时并未考虑启用 fAbortOnError。
我最近写了一篇很长的文章来更详细地解释这一点(see this如果你有兴趣)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)