COM port - how to identify a device (.net/c#)
I'm programming for Windows CE and I need to connect to a scanner. There is no problem - I know that it is on port COM0, but if I plug in another device earlier it gets another COM port... to get all com ports the easy way is:
SerialPort.GetPortNames()
But I don't know how to identify a device? Is there any standard way? I can开发者_JAVA百科't 'ping' it since there is one device that only sends data... it spams data all the time... and I don't know how to 'ping' a device...
The problem of a serial connection is that is stateless. You can't know if anyone is on the other side listening or who is listening.
To find this out you just have a few choices:
- To find out if there is someone use hardware handshake (like RTS, CTS, etc.) if possible.
- To find who is there, you normally send some kind of identify message where you know the correct answer (e.g. to a modem you'll send
AT
and you'll receive anOK
.
So if you can't use any of the above methods you can't automatically detect which port to use. So the only working model is to ask the user for the correct setting(s) (e.g. ComboBox with available ports or BaudRates, CheckBoxes for the different boolean configuration settings, etc).
精彩评论