Go Down

Topic: Getting Arduino Com Ports With C# (Read 1 time) previous topic - next topic

etrbet

Hi I use this code for getting available com ports from computer with C#

string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
comboBox1.Items.Add(port);
}

My question is that How can I get only Arduino port because I get all ports and I take exception on my desktop program. I want to take only arduino ports.

Anybody have idea?

zoomkat

Usually something is sent to the available comport, and if the arduino is attached to that port and recognizes what is sent, ti would send a response back to the pc.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

PeterH

There may be a better way to do this by looking at attributes of the virtual COM port, but one approach is to look at the "Ports (COM & LPT)" class in the registry. This has a key for each COM port, and you could tell from the DriverDesc value whether this is a virtual COM port provided by the Arduino USB driver.
I only provide help via the forum - please do not contact me for private consultancy.

etrbet

I Found :D

here is code

try
            {             
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_PnPEntity");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Caption"].ToString().Contains("Arduino"))
                    {
                        Console.WriteLine(queryObj["Caption"]);
                    }

                }
                Console.ReadLine();
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e.Message);
            }

dont forget add reference System.Management :)

Go Up