串行端口电缆未插入
在.Net的SerialPort对象中,有没有办法确定串行端口是否被拔出或插入。
In .Net's SerialPort object, is there a way to determine if a serial port gets unplugged or plugged in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
与 USB 不同,串行端口没有任何内置方法来检测链路状态的物理变化。 可以使用某些引脚(即 DTR、DSR,有时还有 DCD)来完成有限形式的“设备就绪/未就绪”信号,但这似乎并不完全是您正在寻找的(它不是内置的) RS232 - 设备必须支持它,您主要用它来与调制解调器通信。)
所以,简而言之:不,在一般情况下。 如果您知道/可以对您尝试与之通信的设备进行编程,并且您知道它会将某条线保持在高电平(例如),您可以轮询它以查找该线变高。 但如果你插入的设备没有经过编程来执行类似的可预测操作,那么就真的无从得知。 (某些设备默认情况下可能会将 DSR 保持在高水平,但这绝不是一个确定的选择。)
Unlike USB, the serial port does not have any built-in way to detect a physical change in link status. A limited form of "device ready/not ready" signalling can be done using some of the pins (namely DTR, DSR, and sometimes DCD), but this doesn't seem like exactly what you're looking for (it's not built in to RS232 -- the device must support it, you mainly use it to talk to modems.)
So, in short: no, in the general case. If you know/can program the device you're trying to communicate with, and you know that it will hold a certain line high (for example), you could poll it looking for that line to go high. But if you plug in a device which isn't programmed to do something predictable like that, then there's really no way to tell. (Some devices may hold DSR high by default but it's in no way a sure bet.)
大多数串行设备对查询都有某种类型的确认响应。 只需发送一个简单的查询并等待响应。 如果您没有收到它,则设备不存在或至少没有响应。
Most serial devices have some type of ack response to a query. just send a simple query and wait for response. If you don't get it, the device is not there or at least not responding.
我还没有尝试过,但看看 SerialPort.PinChanged 事件和 DsrChanged< /a>.
每当有任何正常设备插入串行端口并打开时,我都希望该端口的 DSR 引脚被置位; 相反,如果该设备被拔掉,或者当它被关闭时,那么我预计 DSR 引脚状态会改变/下降。
各个引脚的通常含义是:
其中,回答你的OP的是DSR。
I haven't tried it, but look at the SerialPort.PinChanged event and DsrChanged.
Whenever there's any normal device plugged in to the serial port and switched on, then I'd expect the port's DSR pin to be asserted; and conversely if that device is unplugged, or when it's switched off, then I'd expect the DSR pin state to change/drop.
The usual meaning of the various pins is:
Of these, the one that answers your OP is DSR.
有一种方法可以检测.NET中的串行端口删除/插入 - 请参阅我的答案检测串口插入/拔出
There is a way to detect Serial Port removal/insertion in .NET - see my answer at Detect serial port insertion/removal
您可以检测可用的串行端口,您可以尝试在 try...catch 块内与它们进行通信。
这是c#中端口检测的示例
you can detect the availables serial ports, the you can try to comunicate with them inside a try...catch block.
This is an example of ports detection in c#
这取决于您使用哪种电缆连接哪种设备。
最好的选择是尝试处理 PinChanged 事件处理程序。
有些设备在连接和打开时会提高 DSR,有些设备会提高 CTS,有些设备会使用这些设备进行握手。
It will depend on what kind of device you connect with what kind of cable.
Your best bet is to try handling the PinChanged event handler.
Some devices will raise DSR when connected and switched on, others CTS, others will use these for handshaking.