问题描述
我用的SerialPort正与条形码阅读器通信(只读)。
I'm working with SerialPort to communicate (read only) with barcode reader.
我已经安装的驱动程序与读者进行操作,就好像它是通过COM端口连接的,虽然它是一个USB设备。当设备插入时,多了一个COM口出现在列表中。
I've installed driver to operate with the reader as if it was connected via Com-port, though it is a usb device. When the device is plugged, one more Com-port appears in the list.
问题是如下。我初始化的SerialPort对象从条形码阅读器阅读,但如果读者被拔掉,我也没办法,最后确定或正确处置的SerialPort对象,因为它是附加到这个端口不再存在。
The problem is the following. I initialize the SerialPort object to read from barcode reader, but if the reader is unplugged, I have no way to finalize or dispose the SerialPort object correctly, because the port it is "attached" to no longer exists.
结果是WinIOException关闭程序时。我不能不仅与的SerialPort工作的代码,但在Program.cs的水平以及抓住它。据堆栈WinIOException最后确定和处理的对象的SerialPort的尝试之后被抛出。
The result is WinIOException when the program is closed. I cannot catch it not only in the code working with the SerialPort but at the program.cs level as well. According to the stack WinIOException is thrown after the attempts of finalizing and disposing the SerialPort object.
是否有任何想法,我怎么能这样的情况正常运行? ?或者至少捕获异常
Are there any ideas how I can operate with this situation properly? Or at least to catch the exception?
我知道肯定的一点是,这个问题是不是在这个特殊的驱动程序;我从其他制造商多了一个条形码阅读器(有异曲同工之妙的驱动程序) - 的情况是一样的。
The thing I know for sure is that the problem is not in this particular driver; I had one more barcode reader from another manufacturer (with the same purpose driver) - the situation is the same.
推荐答案
感叹,这一个古老的问题USB串口仿真器。串行端口是从石器时代日期设备。他们曾经是拧入公交车,没办法当程序正在使用无图纸火花和滚滚浓烟将其删除。石器时代还包括缺乏任何形式的插件和播放支持,这样一个程序可以检测到设备突然奇闻趣事。
Sigh, this an age old problem with USB serial port emulators. Serial ports are devices that date from the stone age. They used to be screwed into the bus, no way to remove them while a program is using it without drawing sparks and billowing smoke. Stone age also includes the lack for any kind of plug-and-play support so that a program could detect that the device is suddenly gonzo.
不幸的是,大多数的照出的设备驱动程序,模仿他们只是让他们消失,即使程序有打开的端口。这个工程只是以及催人泪下闪存驱动器从插座中当Windows文件写入它。有等待来自设备驱动器,以便它可以产生DataReceived,ErrorReceived和PinChanged事件的通知后台工作线程。该线程遭受心脏发作时,设备突然消失。你不能听清楚,这是一个由SerialPort类启动一个线程,你不能用try / catch语句包裹。
Unfortunately, the majority of the crummy device drivers that emulate them just make them disappear, even though a program has the port opened. This works just about as well as jerking a flash drive out of the socket when Windows is writing files to it. There's a background worker thread that waits for notifications from the device driver so that it can generate the DataReceived, ErrorReceived and PinChanged events. That thread suffers a heart attack when the device suddenly disappears. You can't catch that, it is a thread that was started by the SerialPort class, you can't wrap it with try/catch.
这是大众的需求,微软做了一些关于它在.NET 4.0中。实际上并不一定在该版本中会发生什么。如果你被困在一个早期版本,你唯一可以做的合理的事情就是带旁边的USB插槽的标志:不要在使用中删除,而这必然使得有人拔出设备至少两次,看看会发生什么。 。之后他们觉得无聊与和打扰你了。
By popular demand, Microsoft did something about it in .NET 4.0. Not actually sure what happens in that release. If you're stuck on an earlier release, the only reasonable thing you can do is tape a sign next to the USB slot: "Don't remove while in use!" Which inevitably makes somebody unplug the device at least twice to see what happens. After which they get bored with that and leave you in peace.
在非常不合理的解决方法是与此内容的app.exe.config文件:
The very unreasonable workaround is an app.exe.config file with this content:
<?xml version ="1.0"?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1"/>
</runtime>
</configuration>
不要使用它。
Don't use it.
这篇关于问题的SerialPort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!