检测蓝牙COM端口的状态 [英] Detecting Status of Bluetooth COM Ports

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

问题描述





我们有一个通过COM端口进行通信的VB应用程序。 我们需要能够检测COM端口是否发生故障,然后警告用户,并停止将来在应用程序中使用该端口。 当我们通过USB使用虚拟COM端口时,这很有效,因为
我们可以使用SerialPort.IsOpen属性监控连接状态。



但是............如果COM端口通过蓝牙连接,我们可以检测到COM端口是否无法打开,但是如果它已经打开并且以某种方式失败,我们的应用程序就会挂起。 我们无法调试它。 在我们发送任何数据之前,我们
检查IsOpen属性以确保端口可用。

Hi,

We have a VB app that communicates over COM ports.  We need to be able to detect if the COM port has failed, then warn the user, and stop any future use of the port within the app.  This works well when we are using Virtual COM ports over USB, as we can monitor the status of the connection using the SerialPort.IsOpen property.

However........... if the COM port is over a Bluetooth connection, we can detect if the COM port cannot be opened, but if it has been opened and somehow has failed, our app simple hangs.  We have been unable to debug it.  Before we send any data we check the IsOpen property to make sure the port is available.

任何帮助表示赞赏。







推荐答案


如果COM端口通过蓝牙连接,我们可以检测到COM端口是否无法打开,但是如果它已经打开并且以某种方式失败,我们的应用程序就会挂起。

if the COM port is over a Bluetooth connection, we can detect if the COM port cannot be opened, but if it has been opened and somehow has failed, our app simple hangs.

如果确实无法检测到它何时挂起,那么您需要一个计时器。  您的代码应该不断重新设置计时器,以便它通常永远不会超时。 但是如果代码挂起,则计时器超时并且计时器中的代码勾选
事件处理程序尝试恢复应用程序。 但是,这并非万无一失。

If it really is impossible to detect when it hangs then you need a timer.   You code should be continually re-setting the timer so that it normally never times out.  But if the code hangs, the timer times out and code in the timer tick event handler attempts to recover the application.  It's not foolproof, however.

更好的解决方案是防止应用程序挂起。 大多数串行I / O方法都是异步操作,或者具有超时选项,这意味着您的代码会记录错误,并且如果操作的时间超过预期,则会继续
。 您可能需要修改用于访问端口的进程,以便可以使用超时属性。 例如: 

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readtimeout%28v=vs.110%29.aspx

A much better solution would be to prevent the application from hanging in the first place.  Most of the Serial I/O methods either operate asynchronously or have a timeout option that means that your code records an error and continues if an operation takes longer than expected.  You may need to modify the processes that you are using to access the port so that you can make use of the timeout property.  For instance: 
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readtimeout%28v=vs.110%29.aspx


这篇关于检测蓝牙COM端口的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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