1
votes

I've been trying to get a solution to a problem my team in work have been having. We need to remotely confirm devices are connected to the system via USB, I believe these are virtual COM Ports as Device Manager shows the systems having 6 COM Ports.

What I need to do is confirm a particular device is connected to particular USB Ports/COM Ports, I'm trying to do this using the below code. I'd hoped that the PNP Device ID could be compared with what's meant to be on that COM. But it's PNPDevice ID's dont appear to be that of the device. And if one device is disconnected it makes no change to the output.

Could anyone help? All I need is the device ID connected to a certain USB / COM Port. I'm unable to install third party software due to this being a retail environment. And all systems are identical to each other.

My code:

    Public Sub GetInfo()
    Try
        ListBox1.Items.Clear()
        Dim searcher As New ManagementObjectSearcher(
       "root\cimv2",
       "SELECT * FROM Win32_SerialPort")

        Dim Caption As String = ""
        Dim CreationClassName As String = ""
        Dim Description As String = ""
        Dim DeviceID As String = ""
        Dim Name As String = ""
        Dim PNPDeviceID As String = ""
        Dim ProtocolSupported As String = ""
        Dim ProviderType As String = ""
        Dim Status As String = ""
        Dim DCount As Integer = 0

        For Each queryobj As ManagementObject In searcher.Get()
            DCount = DCount + 1

            Caption = "Caption: " & queryobj("Caption")
            CreationClassName = "CreationClassName: " & queryobj("CreationClassName")
            Description = "Description: " & queryobj("Description")
            DeviceID = "DeviceID: " & queryobj("DeviceID")
            Name = "Name: " & queryobj("Name")
            PNPDeviceID = "PNPDeviceID: " & queryobj("PNPDeviceID")
            ProtocolSupported = "ProtocolSupported: " & queryobj("ProtocolSupported")
            ProviderType = "ProviderType" & queryobj("ProviderType")
            Status = "Status: " & queryobj("Status")

            ListBox1.Items.Add(" ")
            ListBox1.Items.Add(Caption)
            ListBox1.Items.Add(CreationClassName)
            ListBox1.Items.Add(Description)
            ListBox1.Items.Add(DeviceID)
            ListBox1.Items.Add(Name)
            ListBox1.Items.Add(PNPDeviceID)
            ListBox1.Items.Add(ProtocolSupported)
            ListBox1.Items.Add(ProviderType)
            ListBox1.Items.Add(Status)
            ListBox1.Items.Add(" ")
            ListBox1.Items.Add("-----------------------------------------------")

        Next

        If ListBox1.Items.Count > 0 Then
            For i As Integer = 0 To ListBox1.Items.Count - 1
                builder.AppendLine(ListBox1.Items(i).ToString)
            Next
        End If

        ToolStripStatusLabel1.Text = "COM Devices Found: " & DCount

    Catch err As ManagementException
    End Try


    Dim fPath = Application.StartupPath & "\CRCTEST.txt"
    Dim afile As New IO.StreamWriter(fPath, True)
    afile.WriteLine(builder.ToString)
    afile.Close()
    afile.Dispose()
End Sub

Outout:

> Caption: Intel(R) Active Management Technology - SOL (COM6)
> CreationClassName: Win32_SerialPort Description: Intel(R) Active
> Management Technology - SOL DeviceID: COM6 Name: Intel(R) Active
> Management Technology - SOL (COM6) PNPDeviceID:
> PCI\VEN_8086&DEV_8C3D&SUBSYS_2175103C&REV_04\3&11583659&0&B3
> ProtocolSupported:  ProviderTypeRS232 Serial Port Status: OK  
> -----------------------------------------------    Caption: Communications Port (COM1) CreationClassName: Win32_SerialPort
> Description: Communications Port DeviceID: COM1 Name: Communications
> Port (COM1) PNPDeviceID: ACPI\PNP0501\1 ProtocolSupported: 
> ProviderTypeRS232 Serial Port Status: OK  
> -----------------------------------------------    Caption: Communications Port (COM2) CreationClassName: Win32_SerialPort
> Description: Communications Port DeviceID: COM2 Name: Communications
> Port (COM2) PNPDeviceID: ACPI\PNP0501\2 ProtocolSupported: 
> ProviderTypeRS232 Serial Port Status: OK  
> -----------------------------------------------    Caption: Communications Port (COM3) CreationClassName: Win32_SerialPort
> Description: Communications Port DeviceID: COM3 Name: Communications
> Port (COM3) PNPDeviceID: ACPI\PNP0501\11 ProtocolSupported: 
> ProviderType Status: OK  
> -----------------------------------------------    Caption: Communications Port (COM4) CreationClassName: Win32_SerialPort
> Description: Communications Port DeviceID: COM4 Name: Communications
> Port (COM4) PNPDeviceID: ACPI\PNP0501\12 ProtocolSupported: 
> ProviderTypeRS232 Serial Port Status: OK  
> -----------------------------------------------    Caption: SAGEM MONETEL USB Telium (COM5) CreationClassName: Win32_SerialPort
> Description: SAGEM MONETEL USB Telium DeviceID: COM5 Name: SAGEM
> MONETEL USB Telium (COM5) PNPDeviceID:
> USB\VID_079B&PID_0028\5&DDDEB9C&0&11 ProtocolSupported: 
> ProviderTypeModem Device Status: OK  
> -----------------------------------------------

Update

I'm basically trying to verify that a particular device is connected to a particular COM/USB Port. For example, is the Mouse plugged into COM 4.

USB Devices USB Devices

COM Ports COM Ports

System Devices System Devices

1

1 Answers

0
votes

Try Win32_PnPEntity instead of Win32_SerialPort.

You can also use this code to enumerate all the properties of your objects:

Sub SearchDevices()

    Try
        Dim Queries As New List(Of String) From {"Win32_SerialPort", "Win32_PnPEntity"}
        Dim QueryResults As New Dictionary(Of String, List(Of Object))
        Queries.ForEach(Sub(query)
                            Dim searcher As New ManagementObjectSearcher(
       "root\cimv2",
       "SELECT * FROM " + query)
                            Dim results As New List(Of Object)
                            Try

                                For Each queryobj As ManagementObject In searcher.Get()
                                    Dim d As New Dictionary(Of String, Object)
                                    Try
                                        For Each prop In queryobj.Properties
                                            d.Add(prop.Name, prop.Value)
                                        Next

                                    Catch

                                    End Try
                                    results.Add(d)
                                Next
                            Catch

                            End Try


                            QueryResults.Add(query, results)
                            Dim cnt = results.Count
                        End Sub)

        For Each k In QueryResults
            Dim cnd = k.Value.Count
        Next
    Catch err As ManagementException
    End Try
End Sub