【问题标题】:Get Virtual COM Port Device ID/Name获取虚拟 COM 端口设备 ID/名称
【发布时间】:2017-12-20 12:32:24
【问题描述】:

我一直在尝试解决我的团队在工作中遇到的问题。我们需要远程确认设备是否通过 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

输出:

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

COM 端口

系统设备

【问题讨论】:

    标签: vb.net serial-port usb wmi


    【解决方案1】:

    尝试Win32_PnPEntity 而不是Win32_SerialPort

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

    子 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
    

    【讨论】:

    • 这是一个很大的帮助,你能帮我理解如何比较 PNP ID 和 COM 端口号吗?所有的信息都在那里,我只需要了解如何获取相关信息。
    • 虽然您只需要获取 PNP ID。您能否更新您的问题以具体说明您要查找的内容。也许包括您在设备管理器中看到的具体内容的屏幕截图。还从“开始 > 运行”运行“MsConfig32”,并尝试在设备管理器中找到您正在尝试定位的设备。很可能您将能够将设备管理器中的名称与 MsConfig 中的资源相匹配。如果用两者的输出更新您的问题。
    • 嗨,请看我的编辑。我已经包含了设备的屏幕截图。总结一下:我正在尝试通过将连接到端口的设备 ID 与其“应该”进行比较来确认特定设备已连接到特定的 USB/COM 端口。我无法从 MsConfig 工具获取任何信息,因为这似乎没有提供有关设备的信息。
    • 我的错字“MsInfo32”不是MsConfig32。硬件资源下的 MsInfo32 包含连接到计算机的每个设备的详细信息,包括芯片驱动程序和每个设备正在使用的 IRQ。
    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 2017-11-02
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多