Is it possible to know the connection status of a RS232 port?

丶灬走出姿态 提交于 2020-01-12 10:48:15

问题


In a 9-pin cable, which signal/pin would you guys recommend to check the connection status?

So far I'm using the DSR signal because, in the hardware I'm testing, if the cable is properly connected the DSR signal is 1 if it gets disconnected DSR goes to 0. However with another drivers and/or operating systems DSR varies during message transmission or is always 0. So I don't find this DSR signal that reliable unless in controlled conditions at 25ºC in a sunny day.

So how can I know at a given point in time if I'm still connected, i.e. the cable didn't got disconnected?

Note that the connection can be lost and I still have my serial port present and opened in my operating system's ports list, so checking for port presence solves nothing. My only hope is to rely on one of the 9 pins to check it.

Also I do not want to send or receive bytes to check the connection status. There should be a better way...

Thanks!


回答1:


There is no reliable way of determining Serial Connection status at any given time.

The standard approach is for each side to turn DTR to high, so whoever you're connected to sees DSR and thus knows something is on the other end. You haven't specifically stated the exact type of connection, but since you're having these issues, I'm going to assume for the moment that it is DTE/DTE and requires a null modem connection.

What makes this worse is that the problem may be with the cable itself and not with the software or the OS.

See this page for more info, but there is more than one pinout for a null modem cables.

The minimum wiring required inside the null modem cable looks like this:

This is what's called a "No-Handshake null modem cable". You can see that the DTR and DSR pins (pin 6 and 4) go nowhere. DSR pin states will not change when something is plugged in using this cable.

Then there's this:

This is a "Loop-Back handshake cable". You can see here that DTR and DSR (pins 6 and 4) are bridged to eachother at each connector and don't go along the length of the cable. This means that if you set your DTR high, you will see a DSR - your own. This tells you that there is a serial cable connected to your port, but the other end of that cable doesn't necessarily go anywhere. You could chop the cable in half and stick it up your nose and you'd still see what the software is telling you is a valid connection.

The better solution is a cable that looks like this:

or like this

This is a partial or full handshake cable. You can see that each side's pin 4 goes into the other's pin6. So each side can set DTR high, and see the other party's DSR. This is the only case where you can accurately determine if the cable is actually connected to a device; and even then only if each side agrees set DTR. Unless you control both sides of the communication, this is going to be hit or miss.

That's the best you can do without sending data down the wire and waiting for a valid reply.



来源:https://stackoverflow.com/questions/25232237/is-it-possible-to-know-the-connection-status-of-a-rs232-port

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!