5

Anyone here has an idea where I can get the ports name listed in my PC?

List of ports provided by Device Manager

By using this code:

For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
    cmbPort.Properties.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next

I could get COM26 and etc. if any, but that's not what I want. Instead of retrieving COM26, I want USB-SERIAL CH340 or USB-SERIAL CH340 (COM26). How could I do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Related question with answer using WMI: http://stackoverflow.com/questions/2937585/how-to-open-a-serial-port-by-friendly-name – M.Babcock Feb 21 '12 at 01:28
  • 1
    This one is probably more related: [How do I get the friendly name of a COM port in Windows?](http://stackoverflow.com/questions/304986/how-do-i-get-the-friendly-name-of-a-com-port-in-windows) – M.Babcock Feb 21 '12 at 01:35

4 Answers4

12

I got mixed results from other answers. I've come up with this code working better for me.

Add Reference to System.Management in your application

using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
{
    var portnames = SerialPort.GetPortNames();
    var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

    var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();
    foreach (var i in portList)
    {
        Console.WriteLine(i);
    }
}
salpenza
  • 37
  • 7
Guillermo Ruffino
  • 2,940
  • 1
  • 26
  • 23
6

Try this .

Public Shared Function ListFriendlyCOMPOrt() As List(Of String)

    Dim oList As New List(Of String)

    Try
        Using searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM26%'")
            For Each queryObj As ManagementObject In searcher.Get()
                oList.Add(CStr(queryObj("Caption")))
            Next
        End Using

        Return oList

    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

    Return oList

End Function

That should work..

John Woo
  • 258,903
  • 69
  • 498
  • 492
Prince Jea
  • 5,524
  • 7
  • 28
  • 46
2

You could use WMI... Add Reference to System.Management in your application then,

shown on StackOverflow: Getting Serial Port Information

using System.Management;
using System.IO;

        string result = "";
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
        {
            string[] portnames = SerialPort.GetPortNames();
            var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
            var tList = (from n in portnames join p in ports on n equals p["DeviceID"].ToString() select n + " - " + p["Caption"]).ToList();

            foreach (string s in tList)
            {
                result = result + s;
            }
        }
        MessageBox.Show(result);
Community
  • 1
  • 1
John Bartels
  • 2,583
  • 3
  • 19
  • 26
0

That isn't the name of the serial port; it is COM26. The name listed in the device manager is probably the name of the device providing the emulation.

Why do you want that name? If you describe your problem more completely, figuring out the solution will be easier.

codekaizen
  • 26,990
  • 7
  • 84
  • 140