使用C#或VB访问设备信息

问题描述 投票:0回答:2

如果我在 Windows 上打开设备管理器,然后转到“端口 (COM LTP)”,我会看到 7 个设备。
1 - 内置计算机 RS323
2-6- USB 串口 (COM X)

如果我右键单击 -> 属性 -> 详细信息,我可以看到一个大的值列表。 我感兴趣的是“地址”和“硬件 ID”,即“FTDIBUS\COMPORT&VID_0403&PID_6001”

如何使用 C# 或更好的 VB 访问此信息?

我尝试过:

var win32DeviceClassName = "Win32_SerialPort";
var query = string.Format("select * from {0}", win32DeviceClassName);

然后为每个属性进行控制台打印,但只有内置 COM1 显示信息

附注我需要此信息,以找出哪个地址有哪个 com 端口,然后将 comport 更改为所需的。

c# vb.net winapi
2个回答
0
投票

试试这个:

    Try
        Using mos As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=""{4d36e978-e325-11ce-bfc1-08002be10318}""")
            Dim AvailableComPorts = SerialPort.GetPortNames().ToList()
            Dim q As ManagementObjectCollection = mos.Get()

            For Each x As ManagementObject In q
                Console.WriteLine(x.Properties("Name").Value)
                Console.WriteLine(x.Properties("DeviceID").Value)
            Next
        End Using
    Catch ex As Exception
        Throw
    End Try

0
投票

以下函数返回串行端口属性列表,其中包含名称和所有可用属性。我添加了一个可选重载“ShowNullProperties”,如果设置为 TRUE 将返回所有属性,无论该值是否为 null。 “Caption”和“DeviceID”的属性再次手动添加到列表末尾,这样我就可以在返回列表时轻松识别端口名称和设备 ID,而无需搜索整个列表。为了使下面的代码正常工作,您需要在设计器中创建一个名为 trv_ports 的树视图和一个图像列表,但是您可以注释掉图像列表代码,它将显示一个 ?对于图像图标。

Imports System.Management

    Private Function Get_ListofSerialPorts(Optional ByVal ShowNullProperties As Boolean = False) As List(Of List(Of String))

    ' This function returns a list of serial port property lists 

    Dim RtnList As New List(Of List(Of String))

    Dim portSearcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'")

    For Each port As System.Management.ManagementObject In portSearcher.Get()
        Dim NewList As New List(Of String)

        For Each prop As PropertyData In port.Properties

            If ShowNullProperties = True Then
                ' Show null properties 
                If prop.Value IsNot Nothing Then NewList.Add(prop.Name.ToString & ": " & prop.Value.ToString) Else NewList.Add(prop.Name.ToString & ": " & "Nothing")
            Else
                ' Do not show null properties 
                If prop.Value IsNot Nothing Then NewList.Add(prop.Name.ToString & ": " & prop.Value.ToString)
            End If

        Next
        ' Add these two properties on the end to use later for the name and device ID fields 
        NewList.Add(port("Caption").ToString)
        NewList.Add(port("DeviceID").ToString)

        RtnList.Add(NewList)

    Next
    Return RtnList

End Function

然后在 Form1 Load 事件中调用此函数并填充树视图。

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Initialized the Ports tree view and gets a list of availible ports 
    ' Load the image index and clear the treeview
    trv_Ports.ImageList = img_Icons ' Image list of icons 0 = serialport, 1 = Properties
    trv_Ports.Nodes.Clear() ' Clear out all nodes 

    ' Create the root node for the serial ports 
    Dim Newnode As New TreeNode
    Newnode.Text = "Ports (COM & LPT)" ' Parent Node
    Newnode.ImageIndex = 0
    Newnode.SelectedImageIndex = 0

    trv_Ports.Nodes.Add(Newnode)


    ' Step through each list and create a new node with the name of the caption and set the tag equal to the device id 
    For Each list In Get_ListofSerialPorts(True)
        Dim Newnode1 As New TreeNode
        ' Get the Device Name and Device ID from the last two items in the list then delete 
        Newnode1.Text = list(list.Count - 2) ' Device Name 
        Newnode1.Tag = list(list.Count - 1) ' Device ID
        list.Remove(Newnode1.Text) ' Now delete the last 2 entries which are the Name and ID 
        list.Remove(Newnode1.Tag)

        Newnode1.ImageIndex = 0 ' Show the serial port icon in the treeview
        Newnode1.SelectedImageIndex = 0

        trv_Ports.Nodes(0).Nodes.Add(Newnode1)

        Dim ListNode As New TreeNode
        For x As Integer = 0 To list.Count - 1

            ListNode = Newnode1.Nodes.Add(x)
            ListNode.Text = list(x)
            ListNode.Tag = Newnode1.Text & "," & list(x)
            ListNode.ImageIndex = 1 ' Show the properties icon
            ListNode.SelectedImageIndex = 1

        Next

    Next

    ' expand the availible ports node
    trv_Ports.Nodes(0).Expand()

    ' Collapse all the properties nodes
    For i As Integer = 0 To trv_Ports.Nodes(0).Nodes.Count - 1
        trv_Ports.Nodes(0).Nodes(i).Collapse()
    Next



End Sub

ShowNullProperties = True 的输出:(如果设置为 False,所有显示“Nothing”的值都不会添加到列表中)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.