Pages: [1]   Go Down
Author Topic: Getting Arduino Com Ports With C#  (Read 1011 times)
0 Members and 1 Guest are viewing this topic.
Adana / Turkey
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Adana / Turkey
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I Found smiley-grin

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 smiley
Logged

Pages: [1]   Go Up
Jump to: