Determine COM-Port number by given PnpDeviceID: Is the DeviceID of a COM-Port device always "COMx"?

StackOverflow https://stackoverflow.com/questions/14522460

Question

I need to determine the COM-Port of a Plug-And-Play-Device. I have the PnPDeviceID. It's a bluetooth usb dongle.

So, I retrieve information about the device using WMI like in the first example at this website. The result of the WMI-query contains a property "DeviceID" which is a string. The value in my case is "COM3".

I get the same value, when I do (instead of using WMI)

string[] names = System.IO.Ports.SerialPort.GetPortNames();

Now I am wondering, if the DeviceId of COM-Devices always has the format "COMx"? I actually need a property called "Port" with an integer as value.

There is another approach, to go over the registry, but I don't know, if this is suggested.

Was it helpful?

Solution

See comment of Hans Passant. They are commonly named COMx. And u should let the user pick.

More important to me: Mostly it is not necessary to get the com port number as integer, since the Com-port name (string) is used as identifier widely, e.g. with System.io... They take the string "COM3" or the driver specific name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top