获取虚拟COM端口设备ID /名称

时间:2017-07-15 14:37:34

标签: vb.net serial-port usb wmi

我一直试图找到解决我工作团队所遇到的问题的方法。我们需要远程确认设备通过USB连接到系统,我相信这些是虚拟COM端口,因为设备管理器显示系统有6个COM端口。

我需要做的是确认特定设备已连接到特定USB端口/ COM端口,我尝试使用以下代码执行此操作。我希望PNP设备ID可以与该COM上的内容进行比较。但它的PNPDevice ID似乎不是该设备的ID。如果一个设备断开连接,则不会改变输出。

有人可以帮忙吗?我只需要连接到某个USB / COM端口的设备ID。由于这是一个零售环境,我无法安装第三方软件。并且所有系统都彼此相同。

我的代码:

    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  
> -----------------------------------------------

更新

我基本上试图验证特定设备是否连接到特定的COM / USB端口。例如,鼠标是否插入COM 4。

USB设备 USB Devices

COM端口 COM Ports

系统设备 System Devices

1 个答案:

答案 0 :(得分:0)

尝试使用Win32_PnPEntity代替Win32_SerialPort

您还可以使用此代码枚举对象的所有属性:

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