java串行I/O:以稳健的方式处理USB串行连接/断开
我使用 rxtx 通过提供 USB 通信端口的 FTDI2232H 在 Java 中进行串行 I/O 处理。它工作得很好,但有一个例外:如果我拔掉 USB 电缆,那么 COM 端口在运行时消失,它会左右抛出异常:
java.io.IOException: No error in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1427)
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1339)
当我再次重新插入电缆时,它不会恢复。
有没有办法让 rxtx 在 USB 通信端口连接/断开时正常工作? (我试图发帖到 rxtx 邮件列表,但由于某种奇怪的原因,即使我订阅了该列表,我也无法发送消息。我已向列表管理员发送电子邮件,但没有得到任何回复。)
如果没有,是否还有另一个串行I/O框架有什么作用?
I'm using rxtx for serial I/O handling in Java with an FTDI2232H that provides a USB comm port. It works great, with one exception: if I unplug the USB cable, so that the COM port disappears at runtime, it spews exceptions left and right:
java.io.IOException: No error in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1427)
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1339)
and when I re-plug the cable in again, it does not recover.
Is there any way to get rxtx to work properly with USB comm port connection/disconnection? (I've tried to post to the rxtx mailing list but for some strange reason I cannot send messages even though I am subscribed to the list. I've emailed the list admin and have gotten no response.)
If not, is there another serial I/O framework that does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来,我不知道这在 Java 中是否可行,但是有一条 Windows 消息被广播 - WM_DEVICECHANGE 消息,您可能需要拦截此消息,如果是设备弹出,请优雅地关闭 rxtx 代码。下面是一个示例 代码,用于检查 CDROM 驱动器中是否插入/移除 CD(它不在爪哇)。
编辑:添加了另一个链接这里您可能会感兴趣。
希望这有帮助,
此致,
汤姆.
By the sound of it, I do not know if this is possible in Java, but there is a windows message that gets broadcasted - WM_DEVICECHANGE message, it is likely you need to intercept this message and if it's a device eject, gracefully shut down the rxtx code. Here's an example code to check for insertion/removal of CD in CDROM drive (It is not in Java).
Edit: Have added another link here that might be of interest to you.
Hope this helps,
Best regards,
Tom.
出色地。我们使用 WM_DEVICECHANGE 并用 C++ 编码并准备 DLL,然后从 JAva 应用程序通过 JNI 访问它..所以我们得到连接和断开连接的通知
well. we use WM_DEVICECHANGE and coded in C++ and prepared DLL and then access it via JNI from JAva application.. so we get notification of connection and disconnection