串行端口IOException [英] SerialPort IOException

查看:81
本文介绍了串行端口IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在两次遇到以下问题:

我正在使用USB到串行转换器.我有一个SerialPort对象.我将其指向正确的COM端口.在正常运行期间,一切正常.如果在程序未运行时断开电源并重新连接电源,则一切仍然正常.但是,如果我在程序运行时断电并重新连接设备,则会抛出以下IOException并被我的代码捕获:

由于线程退出,I/O操作已中止.或System.IO.Ports.InternalResources.WinIOError(Int32 errorCode,String str)在System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate,奇偶校验, System.IO.Ports.SerialStream..ctor中的Int32数据位,StopBits停止位,布尔型null(String portName,Int32 baudRate,奇偶校验,Int32数据位,StopBits停止位,Int32 readTimeout,Int32 writeTimeout,握手握手,布尔dtr ,位于System.IO.Ports.SerialPort.Open()处的布尔rtsEnable,布尔废弃值null,字节parityReplace)在C:\ Documents and Settings \ Owner \ Desktop \中的BillAcceptor.MEI_BillAcceptor.set_ComPort(String value)处Work \ HAL \ BillAcceptor \ BillAcceptor \ MEI_BillAcceptor.cs:line 73

即使重新启动程序也无法解决 错误;现在,每次我尝试打开SerialPort时都会引发异常.我唯一的解决方案是重新启动计算机.

设备制造商提供的软件是用本机C ++编写的.即使我的程序不能,它仍然可以与设备正确通信.这告诉我这是.NET SerialPort对象特有的.有什么方法可以以编程方式解决此错误(可以接受非托管WinAPI调用)?如果没有,我可以重新装上一个程序集,以免重新启动机器吗?

P.S..我正在使用.NET 2.0,并且正在捕获IOException,而不仅仅是通用异常.

I have encountered the following problem twice now:

I am using a USB to Serial converter. I have a SerialPort object. I am pointing it to the correct COM port. Everything works as expected during normal operation. If I disconnect and reconnect the power supply while my program is not running, everything still works fine. However, if I kill the power and reconnect the device while my program is running, the following IOException is thrown and caught by my code:

"The I/O operation has been aborted because of either a thread exit or an application request."

at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
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 BillAcceptor.MEI_BillAcceptor.set_ComPort(String value) in C:\Documents and Settings\Owner\Desktop\Work\HAL\BillAcceptor\BillAcceptor\MEI_BillAcceptor.cs:line 73

Even restarting my program does not resolve the error; now, the exception is thrown every time I try to open the SerialPort. The only solution I have is to restart my computer.

The software provided by the manufacturer of the device is written in native C++. It will still communicate with the device correctly, even though my program will not. This tells me that this is something specific to the .NET SerialPort object. Is there some way I can programmatically resolve this error (unmanaged WinAPI calls are acceptible)? If not, is there an assembly I can reload that will preclude my restarting the machine?

P.S.  I am using .NET 2.0, and I am catching the IOException, not just a generic Exception.

推荐答案

有相同的问题:

Had same issue:

我最终关闭然后处置了SerialPort对象:

I ended up closing then disposing SerialPort object:

serialport.Close();

serialport.Close();

serialport.Dispose();

serialport.Dispose();


这篇关于串行端口IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆