tags:

views:

40

answers:

3

I need to Determine the serial port name connected to other machine using c#.

A: 

A 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:

That's it.

Oliver
@Oliver, I need to listen to all the ports in my machine, such that i receive messages from any port. How can i listen to all the ports simultaneously ?
Anuya
Are you sure you need the backgroundworker? The SerialPort class is be default asynchronous and so is the DataReceived()-Event which gets fired.
Bobby
@Bobby: If you need to listen to more than one serial port at a time it makes sense to put each one into his own thread to avoid interferences between each another (aside the one the programmer wishes)
Oliver
@Oliver: Yes, I understand that...but As far as I know the complete SerialPort class is already asynchronous. So, if you use the DataThreshold-Property and the DataReceived-Event you're always getting an asynchronous event for each SerialPort which is open. Though, I see what you mean if you 'manually' probe and listen each SerialPort.
Bobby
A: 

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.

phq
@phq, I need to listen to all the ports in my machine, such that i receive messages from any port. How can i listen to all the ports simultaneously ?
Anuya
+1  A: 

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.

Hans Passant