Home Code Detect com port using C Sharp

Detect com port using C Sharp

by shedboy71
[lebox id="1"]

I have a few examples of interfacing a PC to an Arduino over the com port, typically you use a program written in a language such as VB.net or C# and either send or recieve data back and forth. One of the problems here is that in you program you need to define the com port, if like me you have plugged many arduino's and clones and various other types of boards you may have any com port available, when windows detects a new device it will assign a new com port. In fact on my PC I am up to COM41.

The issue that i saw with many examples was that the com port was hard coded, great if you know this in advance but what if this was to change, typically the PC program would fall over with a serial port error.

Here is one solution for Windows users, I decided to use WMI and query the serial ports on my system.

Using WMIExplorer or WMICodeCreator (google them) you can execute a select * from Win32_SerialPort query and you should see something like below.

wmi and arduino

wmi and arduino

The key thing here is that you can see I have an Arduino connected and its assigned COM port amongst other properties. We can send this query in our C# app and then look for the name Arduino in the description property. Above you can see this is Arduino Uno

Code

This is based on a C# WinForms app

You need to add a reference to the System.Management in your solution

Add the library – using System.Management;
Create a new function called DetectArduino() and call it
[codesyntax lang=”csharp”]
public Form1()
{
string comPort = DetectArduino();
InitializeComponent();
//the COM port of my Arduino
serialPort1.PortName = comPort;
serialPort1.BaudRate = 9600;
}
[/codesyntax]
Here is the function I used
[codesyntax lang=”csharp”]
public static string DetectArduino()
{
ManagementScope connectionScope = new ManagementScope();
SelectQuery serialQuery = new SelectQuery(“SELECT * FROM Win32_SerialPort”);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(connectionScope, serialQuery);

try
{
foreach (ManagementObject item in searcher.Get())
{
string desc = item[“Description”].ToString();
string deviceId = item[“DeviceID”].ToString();

if (desc.Contains(“Arduino”))
{
DebugOut(“Arduino details: ” + desc + ” on ” + deviceId);
return deviceId;
}
}
}
catch (ManagementException e)
{
/* Do Nothing */
DebugOut(“An error occurred while querying for WMI data: ” + e.Message);
}

return “NOPORT”;

}
[/codesyntax]
DebugOut was for outputting debug information which can be viewed in a tool such as DebugView

[codesyntax lang=”csharp”]

public static void DebugOut(string message)
{
    Debug.WriteLine(message);
}

[/codesyntax]

 

Of course there are still limitations for example this does not deal with Arduino clones that do not identify themselves as Arduino's and also isn't designed for more than 1 Arduino being connected but it's a basis for a solution, adding a list of strings to check for such as Arduino would not be that difficult

Share
[lebox id="2"]

You may also like