I need to Determine the serial port name connected to other machine using c#.
views:
40answers:
3A serial port doesn't report any connection state. You can open all available serial ports on your computer (if no other application already opened it) regardless if it is connected to something or not.
To find out if a serial port is connected to another machine, you have to open up all the available port, send your initialization data and listen if something correct comes back.
Imagine you have a good old serial modem connected to your pc. To find this out you have to open up all the available ports and send a 'AT' over the wire. If a 'OK' comes back you found a modem (maybe additional tasks are necessary to check if you found the right one [maybe there is more than one device connected to your pc]).
What i just missed out: Don't forget to configure the serial port! Don't set only baudrate and stop bits. Set all settings to the values you need (even if you use default settings). Cause these settings will be saved also if you close and reopen again. All settings are still valid unless you change them. Now imagine you have some other application on your pc that also opens up a serial port and changes the settings for some uncommon feature (e.g. XOnOff). If you don't set it back on your initialization phase you'll never be able to get a working connection!
Update
Listening to all the available ports is quite easy:
- First you need a list of all com ports.
- Then you create for each one a own thread (or backgroundworker)
- And each thread handles its given SerialPort
That's it.
Serial communication doesn't have anything compared to that of IP which has an address and port sent with every packet. The only data that is sent over a serial cable is the bytes you send yourself.
If you control both ends you can send the port number as a part of your own protocol.
This is just not the way serial ports work. It is not a bus, like USB or PCI, where you can plug something in and the operating system will do the ah-ha, new hardware! discovery automatically. Serial ports are very primitive, dating from the stone age of computer hardware.
It takes a human to plug a serial port device connector. With some luck, the connector will have a label which says what COM port number is assigned to the connector. Although that luck is hard to come by these days. She'll then tell a program to establish a connection on that particular COM port. Hyperterminal is the canonical implementation of such a program on Windows.
You cannot realistically open every COM port that might be available. That prevents another program from using another COM port. You'll prevent a modem from getting used for example. Part of the stone age legacy is that only one program can open a COM port, every other program will be locked out.
So, provide your program with a UI that lets the user select the COM port(s). Save the selection in your config data, it is very likely that the device is still connected to the same port when it starts back up. You can use WMI and the Win32_SerialPort class to provide a better description for the COM port (more than just the number). Some USB serial port emulators may set the Description property to something recognizable.
SerialPort.GetPortNames() enumerates the available COM port numbers. A basic sanity test is to check the SerialPort.DsrHolding property, it should be true when the serial port device is plugged in and powered-up.