确定设备是否连接

确定设备是否连接

本文介绍了确定设备是否连接/断开到RS232端口,而不打开端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im正在使用C ++ Win32应用程序,如果设备已经插入其中一个RS232端口,然后如果已断开连接,我将尝试从本质上自动检测。



检查连接的设备是否容易,因为我的用例允许我假设这个特定的线程将是第一个尝试启动与端口的通信我可以扫描可用端口每分钟左右,一旦我找到一个端口与设备上它标志该端口有一个设备,关闭端口,然后设置一个事件,以便实际上将使用设备的过程知道它现在可以



断开连接检测是我遇到的问题当我扫描连接的设备时,我可以实际发送数据到端口,以确保,如果有一个设备,它是我正在寻找的具体设备,但一旦连接,该端口将已经被其他进程打开,我无法再从侦测线程打开该端口。所以我正在寻找一种方法来打开端口在听模式或类似的东西,所以我可以看看设备是否仍然存在。



我短暂地发现了一些关于观看DSR或DTR线路或某事......但是找不到更多或如何实际做的事情。



有任何建议吗? p>

编辑:看起来我需要再澄清一下...为了检测断开连接,我不能发送数据到任何方式的RS232端口。此外,我不能假设另一个应用程序实际上已打开端口。设备可以物理连接,但没有和打开连接...但我仍然不能冒险发送数据到它。我希望有一种方法可以检查该端口上仍然有电源或类似的东西。

解决方案

在连接的硬件上,当您断开硬件时是否会有调制解调器状态寄存器的变化,但如果有,那么您可以使用功能。



然而,有一个问题,你需要一个文件句柄的COM端口来调用任何API函数,这是排除的文档说明:

COM端口观看线路状态,而另一个进程已打开端口进行通信。



有办法做到这一点,但它们涉及到一个驱动程序。 SysInternals拥有,Google搜索会打开一些公司销售软件,用于在应用程序之间共享COM端口访问,但使用标准API无法使用AFAIK同时访问。


I"m working on a C++ Win32 application for which I'm trying to essentially "auto detect" if a device has been plugged into one of the RS232 ports and then if it has been disconnected.

The checking for a connected device is easy enough because my use case allows me to assume that this particular thread will be the first to try to initiate communication with the port. I'm able to scan the available ports every minute or so and once I've found a port with the device on it I flag that port has having a device, close the port, then set an event so that the process that actually will use the device knows it can now connect on that port.

The disconnect detect is where I run into trouble. When I'm scanning for connected devices I can actually send data to the port to be sure that, if there is a device, it's the specific device I'm looking for. But once it's connected, that port will already be open by the other process and I can no longer open that port from the detect thread. So I am looking for a way to either open the port in a "listen mode" or something like that so I can just see if the device is still there.

I briefly came across something about watching the DSR or DTR line or something...but couldn't find any more or how to actually do it.

Any suggestions?

Edit: Looks like I need to clarify a little more... For detecting the disconnect, I cannot send data to the RS232 port in any way. Also, I cannot assume that another application actually has the port open. The device may be physically connected, but without and open connection...but I still can't risk sending data to it. I was hoping there was a way to just check that there was still power on that port or something like that.

解决方案

It depends on the connected hardware whether there will be a change in the modem state registers when you disconnect the hardware, but if there is then you could check the state of for example the CTS or DSR line using the GetCommModemStatus() function.

There is however the problem that you need a file handle to the COM port to call any API function, and this is exclusive as the documentation of CreateFile() states:

So you can not open the COM port to watch the line state while another process has the port opened for communication.

There are ways to do this, but they involve a driver. SysInternals has the Portmon tool, and a Google search will turn up some companies selling software for sharing COM port access between applications, but AFAIK simultaneous access is impossible using the standard API.

这篇关于确定设备是否连接/断开到RS232端口,而不打开端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:21