Click here to Skip to main content
15,917,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I'm trying to query the WMI for serial port connections (COM ports). I can see the ports on the Device Manager but when trying to query them through WMI (Win32_SerialPort), nothing shows up. I am able to connect to one of the ports (COM7) via the HyperTerminal. It is a GSM/GPRS modem that I am attempting to connect to using C#.

The description in Device Manager for the modem is HSPADatacard NMEA Device (COM7).

Attempting to do this on a Win10 machine.

Any help is much appreciated.

What I have tried:

Running cmd with admin rights, the 'mode' command does reflect the additional COM ports when the modem is plugged in.

Using a WMICodeCreator to generate a VBscript to report on the ports did not yield any results either.

Script generated:
JavaScript
strComputer = "." 
<pre>Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_SerialPort",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_SerialPort instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "DeviceID: " & objItem.DeviceID
    Wscript.Echo "Name: " & objItem.Name
Next
Posted
Updated 22-Aug-18 15:37pm

The Win32\_SerialPort class | Microsoft Docs[^] lists only physical serial ports and not virtual ones provided by device drivers.

One solution is to use the MSSerial_PortName class in the \root\WMI namespace. See also List of SerialPorts queried using WMI differs from devicemanager? - Stack Overflow[^].
 
Share this answer
 
@Jochen Arndt, Tried changing the query to use MSSerial_PortName and all I got was an error in the query with no results returned.

After some reading the following query did work:

JavaScript
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%COM%' AND PNPClass = 'Ports'",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_PnPEntity instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "DeviceID: " & objItem.DeviceID
    If isNull(objItem.HardwareID) Then
        Wscript.Echo "HardwareID: "
    Else
        Wscript.Echo "HardwareID: " & Join(objItem.HardwareID, ",")
    End If
    Wscript.Echo "Manufacturer: " & objItem.Manufacturer
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "PNPClass: " & objItem.PNPClass
    Wscript.Echo "PNPDeviceID: " & objItem.PNPDeviceID
Next


That generated the output of :
Quote:
-----------------------------------
Win32_PnPEntity instance
-----------------------------------
Caption: HSPADataCard NMEA Device (COM7)
Description: HSPADataCard NMEA Device
DeviceID: USB\VID_19D2&PID_0082&MI_01\6&1C9A94D5&0&0001
HardwareID: USB\VID_19D2&PID_0082&REV_0000&MI_01,USB\VID_19D2&PID_0082&MI_01
Manufacturer: HSPADataCard
Name: HSPADataCard NMEA Device (COM7)
PNPClass: Ports
PNPDeviceID: USB\VID_19D2&PID_0082&MI_01\6&1C9A94D5&0&0001


The next step will be to query and/or set the relevant port's parameters (like Baud rate, parity etc)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900