2017-07-15 245 views
0

我一直在尝试解决我的团队在工作中遇到的问题。我们需要远程确认设备通过USB连接到系统,我相信这些是虚拟COM端口,因为设备管理器显示具有6个COM端口的系统。获取虚拟COM端口设备ID /名称

我需要做的是确认一个特定的设备连接到特定的USB端口/ COM端口,我试图用下面的代码来做到这一点。我希望PNP设备ID可以与该COM上的含义进行比较。但它是PNPDevice 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

回答

0

尝试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 
+0

这是一个很大的帮助,你又能帮助我明白,我怎么能比较PNP ID与COM端口号?所有的信息都在那里,我只需要了解我如何获得相关信息。 – user3224987

+0

我虽然只是需要获得PNP ID。你能否更新你的问题来具体说明你正在寻找什么?也许包括一个屏幕截图,特别是你在设备管理器中看到的。此外,从“开始>运行”中运行“MsConfig32”,并尝试找到设备管理器中您尝试查找的设备。赔率是您可以将设备管理器中的名称与MsConfig中的资源进行匹配。如果用两者的输出更新你的问题。 –

+0

嗨,请看我的编辑。我已经包含了设备的屏幕截图。总结一下:我试图通过比较连接到端口的设备ID和它应该是什么来确认特定的设备连接到特定的USB/COM端口。我无法从MsConfig工具获取任何信息,因为这似乎并未提供有关设备的信息。 – user3224987