Finding information about all serial devices connected through USB in C#

How to list all serial ports:

There are several System-Defined Device Setup Classes available to hardware vendors. Properly written drivers for COM-Ports should use the Ports (COM & LPT ports)-class (guid: 4d36e978-e325-11ce-bfc1-08002be10318). Probably this class is used by the device manager as well.

So you can use the following query to list every serial port you also see in the devicemanager:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
    "root\\CIMV2",
    "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\""
);
foreach (ManagementObject queryObj in searcher.Get())
{
    // do what you like with the Win32_PnpEntity
}

See this detailed description of the Win32_PnPEntity-class. You should have everything you need for identifying your device.

For determining the port number I examine the name property and extract it. Until now this works fine, but I don’t know if the port number is garanteed to be included in the name. I haven’t found any serial port device until now, that doesn’t have the port number included in the name.

The above query finds every serial port device, no matter if it is a bluetooth SPP, a FTDI-chip, a port on the mainboard, an extension card or a virtual serial port generated by some modem driver (i.e. Globetrotter GTM66xxW).

To determine the type of connection (bluetooth, usb, etc.) you can examine the deviceid (have a look at the first part of the deviceid). There you can also extract the bt-mac address (be careful with that: the deviceid looks different at least on Windows 7 and Windows XP).

Regarding why some devices are not listed with Win32_SerialPort:

I suspect it depends on the driver implementation, since I have some usb-devices that get their ports listed and some that don’t.

Leave a Comment