GET A FRIENDLY PORT NAME

Does anyone have an idea where I can get the port name printed on my PC?

List of ports provided by Device Manager

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 , etc., if any, but that is not what I want. Instead of removing COM26 , I want USB-SERIAL CH340 or USB-SERIAL CH340 (COM26) . How could I do this?

+4
source share
4 answers

Try it.

 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.

+6
source

I got mixed results from other answers. I came up with this code that works best for me.

Add a link to System.Management in the 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()); portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList(); } 
+8
source

You can use WMI ... Add a link to System.Management in the application Then,

shown in StackOverflow: Retrieving 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); 
+2
source

This is not a serial port name; this is COM26. The name specified in the device manager is probably the name of the device that provides the emulation.

Why do you need this name? If you describe your problem in more detail, figuring out a solution will be easier.

0
source

Source: https://habr.com/ru/post/1397530/


All Articles