我在 Windows XP 嵌入式版本上运行 .Net 3.5 应用程序。此应用程序通过串行 (COM) 端口写入和读取一些数据。该应用程序在我的笔记本电脑(windows XP Professional)上运行良好,但在 windows XP embedded 上运行不正常。我不断收到此错误:
System.IO.IOException: The I/O operation has been aborted because of either a thread exit or an application request.
可能是什么原因造成的?
一些附加信息:为了阅读,我使用的是 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 对象抛出的大部分 IOExceptions。一些 PC/手持设备具有默认启用错误标志中止的 UART - 因此串行端口的 init 例程必须清除它(Microsoft 忽略了这样做)。 SerialPort 对象在设计时并未考虑启用 fAbortOnError。
我最近写了一篇很长的文章来更详细地解释这一点(see this 如果您有兴趣)。
关于c# - Windows XP 嵌入式版本出现 System.IO.IOException 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1194948/