0

If i open the Device Manager on Windows, then go to "Ports (COM LTP)" i see 7 devices. 1 - Builtin Computer RS323 2-6- USB Serial Port (COM X)

if i make a right click -> Properties -> Details, i can see a big list of values. Interesting for me are "Address" and "Hardware Ids" which is "FTDIBUS\COMPORT&VID_0403&PID_6001"

How can i access this info with C# or better VB? I tried

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

and then make a console print for each property but only the built in COM1 displays info

P.S. I need this info, to find out which adress has wich com-port and then change the comport to a desired.

sgt_johnny
  • 329
  • 2
  • 16
  • See [List of SerialPorts queried using WMI differs from devicemanager?](http://stackoverflow.com/questions/19840811/list-of-serialports-queried-using-wmi-differs-from-devicemanager) – H.G. Sandhagen Jan 21 '17 at 17:30
  • Does this only deliver a list of ports, or all properties? Win32_SerialPort does not put out "Address" of Port that can be seen manually – sgt_johnny Jan 21 '17 at 17:40

2 Answers2

0

Try this:

    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
Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26
  • Thanks for the answer, it gives me vid/pid, but the adresss is missing. My output looks like this: `USB Serial Port (COM6)` `FTDIBUS\VID_0403+PID_6001+A901ET6UA\0000` but an adress field like `00000001` is missing – sgt_johnny Jan 22 '17 at 09:23
0

The below function returns a list of serial port property lists with names and all available properties. I added an optional overload "ShowNullProperties" if set to TRUE will return all properties regardless if the value is null. The properties for "Caption" and "DeviceID" are manually added again at the end of the list so I can easily identify the port name and device ID when the list is returned without having to search the entire list. For the below code to work you will need a treeview called trv_ports in your designer and a image list however you can comment out the image list code and it will show a ? for the image icons.

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

Then in the Form1 Load event call this function and populate a treeview.

      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

Output with ShowNullProperties = True: ( if set to False all values that show "Nothing" would not be added to the list)

enter image description here

Neelix
  • 143
  • 1
  • 8