防止 .net 串行端口在 FTDI USB 设备断开连接时崩溃

发布于 2024-09-18 06:26:14 字数 422 浏览 5 评论 0原文

我有一个应用程序应该与多个自定义设备进行通信,其中一些使用 FTDI USB 转串口转换器芯片,还有一些使用 TCP。应用程序需要能够随时从可能随时插入或拔出的设备接受数据;该应用程序充当设备和数据库之间的桥梁。

似乎当设备被拔出时,通常会导致 SerialPort 类在 BackgroundWorker 线程中抛出异常并使应用程序崩溃。

我目前的补救措施可能非常复杂,是让一个辅助应用程序发送/接收串行端口数据并将其转发到 TCP 套接字或从 TCP 套接字转发数据。当我的主应用程序发现 USB 设备已插入时,它会启动另一个应用程序,然后使用 TCP 套接字与其进行通信。如果插入多个 USB 设备,将为每个设备启动一个单独的帮助应用程序实例。当拔出 USB 端口时,辅助应用程序将崩溃,但“意外关闭”消息将被抑制。

这种方法确实有效,但似乎非常令人不满意。一定有更好的方法。

I have an application which is supposed to communicate with multiple custom devices, some of which use FTDI USB-to-serial converter chips, and some of which use TCP. The application needs to be able to accept data at any time, from devices which may be plugged or unplugged at any time; the application serves as a bridge between devices and a database.

It seems that when a device is unplugged, it will often cause the SerialPort class to throw an exception in a BackgroundWorker thread and crash the application.

My present remedy, which is probably absurdly complicated, is to have a helper application send/receive serial port data and relay it to/from a TCP socket. When my main application sees that a USB device is plugged in, it launches this other application and then uses a TCP socket to communicate with it. If multiple USB devices are plugged in, a separate instance of the helper application will be launched for each. When a USB port is unplugged, the helper application will crash, but the "Unexpected shutdown" message will be stifled.

This approach works, but it seems massively unsatisfying. There's got to be a better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-09-25 06:26:14

我在 .NET 2.0 中遇到了同样的问题,我恢复使用 FTDI dll 包装器:
http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/CSharp /FTD2XX_NET_1010.zip

工作得很好,您可以控制驱动程序的真正好处,例如以编程方式设置延迟计时器。

我编写了自己的类来检测拔出事件,在尝试读取数据时捕获 FT_IO_ERROR 事件。通过捕获错误并声称它是拔出,这并不完全令人满意。但它有效。

我一直在与 FTDI 讨论这个问题,最近他们发布了一份新的应用说明:
http://ftdichip.com/Support/Documents/AppNotes/AN_152_Detecting_USB_%20Device_Insertion_and_Removal.pdf

使用 WM_DEVICECHANGE 事件来检测 USB 拔出。也可以,但有一个小问题。因为它是一个窗口消息,所以它仅在您有 GUI 时才起作用,并且在某些情况下,如果另一个应用程序正在运行并在您可以处理该事件之前捕获该事件,则该事件将无法到达您的应用程序。

最后一个选项是使用 WMI 进行检测。您可以使用 ManagementEventWatcher
在创建和删除时创建侦听器。也可以正常工作,但是我的笔记本电脑上有一些 USB 端口,FTDI 驱动程序不会返回正确的 COM 端口和位置 ID(实际上,什么也没有),这是我可以从 WMI 读取的信息,所以我无法将 WMI 事件链接到 DLL 包装器中连接的设备。

我于 2010 年 6 月/7 月向 FTDI 报告了此问题,据称 GetCOMportNumber 和位置 ID 问题已在其 2.08.02 驱动程序版本(8 月)中修复,但我没有时间重新检查此问题。

到目前为止我对 USB 地狱的经历......

I had the same problems with .NET 2.0 and I reverted to using the FTDI dll wrapper:
http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/CSharp/FTD2XX_NET_1010.zip

Works very well, and you have access to control the real goodies of the driver, such as programmatically setting the latency timer.

I wrote my own class to detect unplug events, by catching the FT_IO_ERROR event, when trying to read the data. This is not entirely satisfying, by catching an error and claiming it is an unplug. But it works.

I have been talking to FTDI about this and recently they released a new application note:
http://ftdichip.com/Support/Documents/AppNotes/AN_152_Detecting_USB_%20Device_Insertion_and_Removal.pdf

That uses the WM_DEVICECHANGE event to detect USB unplugs. Also works, but there is a small catch. Because it is a window message, it only works if you have a GUI and there are cases where the event will not arrive at your application, if another application is running and catches the event before you can process it.

The final option is to use WMI to do the detection.You can use the ManagementEventWatcher
to create a listener on create and delete. Also got this to work, however there were some USB ports on my laptop where the FTDI driver would not give the correct COM port and location ID back (actually, just nothing at all) and that is the info which I could read from WMI, so I could not link a WMI event to the connected device in the DLL wrapper.

I reported this issue to FTDI in june/july 2010 and supposedly the GetCOMportNumber and Location ID issues are fixed in their 2.08.02 driver version (august), but I have not had the time to recheck this.

So far my experiences with the USB hell....

失眠症患者 2024-09-25 06:26:14

我已经测试过在 .Net 4.0 下使用时移除 USB 转串行适配器。它不会像以前版本的 .Net 那样导致应用程序崩溃。此外,在以前的版本中,不仅应用程序会崩溃,有时您还必须重新启动才能使端口正常工作。

I have tested removing a USB to serial adapter while in use under .Net 4.0. It does not cause application crashes as did previous versions of .Net. Also, under previous versions, not only would the application crash, but sometimes you had to reboot to get the port to work at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文