views:

151

answers:

2

There are several ways to list serial ports under Windows but I'm not sure what is the proper way: the way that does detect all serial ports that are available.

One good code example is http://www.naughter.com/enumser.html - where there are 9 (nine!) ways of enumerating serial devices.

The question is: what is the optimal way of doing it.

Requirements:

  • to not open ports in order to check if they are available.
  • to be able to detect ports with different names than COMx.
  • to work on Windows XP SP2 or above
A: 

There's an interesting example here which you could use to enumerate all the ports:

http://www.codeproject.com/KB/system/DevMgr.aspx

Simon Elliott
+1  A: 

Serial ports are very simple devices, dating from the stone age of computing hardware. They don't support Plug & Play, there is no way to tell that somebody plugged in a device. The only thing you can do is discover what ports are available, the SerialPort.GetPortNames() returns the list. Some USB emulators can generate a descriptive name to go with the port name, you can discover those with WMI, Win32_SerialPort class.

None of which helps you discover what COM port is connected to a particular device. Only a human knows, she physically plugged the cable in the connector. You'll need to provide a config UI that lets the user select the port number. A combo box gets the job done. Save the selection in your config data, it is very likely that the device is still connected to the same port the next time your program starts.

Hans Passant
I use SerialPort.GetPortNames().
dbasnett