- Home /
Auto-detect Arduino COM port
Hello, I'm doing a project with Arduino and I wanted to know if you can detect from the arduino automatically the number of Port and send it to the Unity project. Thank you!
Comment
Answer by poanzefoijzoeijoirojio9029312 · Nov 30, 2016 at 12:49 PM
Hi,
According to this code sample :
https://www.codeproject.com/tips/349002/select-a-usb-serial-device-via-its-vid-pid
.... you can do something like that:
public static string AutodetectArduinoPort()
{
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");
string temp;
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);
if ((temp = (string)rk5.GetValue("FriendlyName")) != null && temp.Contains("Arduino"))
{
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
if (rk6 != null && (temp = (string)rk6.GetValue("PortName")) != null)
{
comports.Add(temp);
}
}
}
}
}
}
if (comports.Count > 0)
{
foreach (string s in SerialPort.GetPortNames())
{
if (comports.Contains(s))
return s;
}
}
return "COM9";
}