Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to display on a combo box all the ports that are available with their description. I did a code where I can just display the ports that have the description, but I can't view all the other ports that don't have the description.

Can you please help me? What am I missing in the code?

Thank you.

What I have tried:

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 A in tList)
            {
                ArduinoComCmbBox.Items.Add(A);
            }
Posted
Updated 5-Oct-22 22:46pm
Comments
Richard MacCutchan 5-Oct-22 9:25am    
The join in your LINQ is only capturing ports which match. So all the non-matched ones are ignored. So, either create a second list or add a clause to accept all ports in both lists.
Member 15189683 5-Oct-22 9:47am    
How should I do it?
Richard MacCutchan 5-Oct-22 9:54am    
You need to look at each list independently to see how you can get the ones that are not matching .I do not have any com ports to try it with.

1 solution

Try something like this:
C#
using (var searcher = new ManagementObjectSearcher("SELECT DeviceID, Caption FROM WIN32_SerialPort"))
{
    string[] portnames = SerialPort.GetPortNames();
    
    var ports = searcher.Get().Cast<ManagementBaseObject>()
        .ToDictionary(p => p["DeviceID"].ToString(), p => p["Caption"]);
    
    foreach (string name in portnames)
    {
        if (ports.TryGetValue(name, out var caption))
        {
            ArduinoComCmbBox.Items.Add($"{name} - {caption}");
        }
        else
        {
            ArduinoComCmbBox.Items.Add(name);
        }
    }
 
Share this answer
 
Comments
Member 15189683 6-Oct-22 8:43am    
I tried, but I got this error when started:
'Arduino.exe' (CLR v4.0.30319: Arduino.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_it_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols.
Exception thrown: 'System.ArgumentException' in mscorlib.dll
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
An item with the same key has already been added.
Richard Deeming 6-Oct-22 8:45am    
Then check your management searcher results - you must be getting multiple serial ports with the same DeviceID.
Member 15189683 6-Oct-22 8:50am    
I can't the error stop my code when try to do
var ports = searcher.Get().Cast<managementbaseobject>()
.ToDictionary(p => p["DeviceID"].ToString(), p => p["Caption"]);
Richard Deeming 6-Oct-22 8:53am    
var rawPorts = search.Get().Cast<ManagementBaseObject>();
var ports = rawPorts.ToDictionary(p => p["DeviceID"].ToString(), p => p["Caption"]);

Set a breakpoint on the second line, and examine the rawPorts variable to look for duplicate device IDs.
Member 15189683 6-Oct-22 9:45am    
The value in rawPorts is:
{System.Linq.Enumerable.<castiterator>d__97<system.management.managementbaseobject>}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900