Created
May 17, 2021 03:02
-
-
Save ratozumbi/112dec0a99202d3e137fc5e990c2d2b9 to your computer and use it in GitHub Desktop.
Autodetect Arduino board on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Win32; | |
using System.Collections.Generic; | |
using System.IO.Ports; | |
namespace SerialManagement | |
{ | |
public static class AutodetectPorts | |
{ | |
public class ArduinoPort | |
{ | |
public string portName; | |
public string boardName; | |
} | |
public static List<ArduinoPort> allPorts; | |
/// <summary> | |
/// Get Arduino serial port by board name | |
/// </summary> | |
/// <param name="boardName"></param> | |
public static string GetPortByBoardName(string boardName) | |
{ | |
if (allPorts == null) | |
allPorts = GetPorts(); | |
foreach(ArduinoPort ap in allPorts) | |
{ | |
if (ap.boardName.Contains(boardName)) | |
return ap.portName; | |
} | |
return ""; | |
} | |
/// <summary> | |
/// Get all Arduino ports which are connected to serial ports | |
/// </summary> | |
public static List<ArduinoPort> GetPorts() | |
{ | |
return ValidatePorts(FindArduinoPorts()); | |
} | |
/// <summary> | |
/// Discover all Arduino ports | |
/// </summary> | |
private static List<ArduinoPort> FindArduinoPorts() | |
{ | |
string friendlyName; | |
string portName; | |
List<ArduinoPort> arduinoPorts = new List<ArduinoPort>(); | |
RegistryKey rk1 = Registry.LocalMachine; | |
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum"); | |
foreach (string s3 in rk2.GetSubKeyNames()) | |
{ | |
RegistryKey rk3 = rk2.OpenSubKey(s3); | |
foreach (string s in rk3.GetSubKeyNames()) | |
{ | |
if (s.Contains("VID") && s.Contains("PID")) | |
{ | |
RegistryKey rk4 = rk3.OpenSubKey(s); | |
foreach (string s2 in rk4.GetSubKeyNames()) | |
{ | |
RegistryKey rk5 = rk4.OpenSubKey(s2); | |
friendlyName = (string)rk5.GetValue("FriendlyName"); | |
if (friendlyName != null && friendlyName.Contains("Arduino")) | |
{ | |
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters"); | |
portName = rk6 != null ? (string)rk6.GetValue("PortName") : ""; | |
if (!string.IsNullOrEmpty(portName)) | |
{ | |
ArduinoPort arduinoPort = new ArduinoPort(); | |
arduinoPort.portName = portName; | |
arduinoPort.boardName = friendlyName; | |
arduinoPorts.Add(arduinoPort); | |
} | |
} | |
} | |
} | |
} | |
} | |
return arduinoPorts; | |
} | |
/// <summary> | |
/// Validate discovered ports | |
/// </summary> | |
private static List<ArduinoPort> ValidatePorts(List<ArduinoPort> comports) | |
{ | |
List<ArduinoPort> validComports = new List<ArduinoPort>(); | |
if (comports.Count > 0) | |
{ | |
foreach (string s in SerialPort.GetPortNames()) | |
{ | |
foreach (ArduinoPort ap in comports) | |
{ | |
if (ap.portName == s) | |
validComports.Add(ap); | |
} | |
} | |
} | |
return validComports; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment