I have the following serial ports listed in my devicemanager:
The Win32_SerialPort class used in this article reports the physical com ports, if you wanna enumerate all the serial ports including the USB-Serial/COM
ports, you must use the MSSerial_PortName
class located in the root\wmi
namespace.
Also try these classes located in the same namespace
MSSerial_CommInfo
MSSerial_CommProperties
MSSerial_HardwareConfiguration
MSSerial_PerformanceInformation
Note : If you want to know the properties and methods of this class you can use the WMI Delphi Code Creator.
I had a similar issues trying to have an application locate the COM port for a USB Serial device.
By using the scope \\localhost\root\CIMV2
for the query SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0
, the application was able to find COM ports via each returned object's caption
or locate the exact port by checking the caption
for the device name.
ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(@"\\localhost\root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
using (comPortSearcher)
{
string caption = null;
foreach (ManagementObject obj in comPortSearcher.Get())
{
if (obj != null)
{
object captionObj = obj["Caption"];
if (captionObj != null)
{
caption = captionObj.ToString();
if (caption.Contains("CH340"))
{
_currentSerialSettings.PortName = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty);
}
}
}
}
}
The parsing code was found at [C#] How to programmatically find a COM port by friendly name
In my case, I have physical serial ports, USB serial ports, and com0com virtual serial ports. I need both the full names, and COM port addresses.
The query suggested in this answer does not find com0com ports. The query suggested in this answer requires Administrator priviledges.
SELECT * FROM Win32_PnPEntity
find all devices. It returns physical devices like this, and address can be parsed from Caption
:
Serial Port for Barcode Scanner (COM13)
However, for com0com ports Caption
is like this (no address):
com0com - serial port emulator
SELECT * FROM Win32_SerialPort
returns addresses (DeviceID
), as well as full names (Name
). However, it only finds physical serial ports and com0com ports, not USB serial ports.
So in the end, I need two WMI calls: SELECT * FROM Win32_SerialPort
(address is DeviceID
) and SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'
(address can be parsed from Caption
). I have narrowed down the Win32_PnPEntity
call, because it only needs to find devices that were not found in the first call.
I found the solution.
The following query (root\CIMV2
) gets the requested results:
SELECT * FROM Win32_PnPEntity WHERE ClassGuid="{4d36e978-e325-11ce-bfc1-08002be10318}"
Update
This answer is pretty old now. Ehen I asked it I still had to consider WinXP and was using Windows7. Since I don't deal with serial ports any more, I can't give any new information on that issue. At that time this solution reported all ports that the devicemanager was showing. But I know listing serial ports is not that easy so this answer might not be correct in all scenarios.