The serial port names assigned by the system are usually cryptic and don’t tell anything about the device. Luckily, such devices have a “friendly” name that is more informative. For example:
COM14 by itself wouldn’t tell the user much, but with the friendly name it is clear that it is the Arduino Uno they have plugged into the machine.
Here is the code that will extract this friendly name on Windows:
Public Shared Function GetSerialPortFriendlyName(PortName as string) As String
#if TargetMacOS
#Pragma Unused PortName
return ""
#elseif TargetLinux
#Pragma Unused PortName
return ""
#ElseIf TargetWindows
// Get the VID and PID for the port
var reg1 as new RegistryItem("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\COM Name Arbiter\Devices", false)
var val1 as string
try
val1 = reg1.Value(PortName)
catch
return ""
end try
if val1 = "" then return ""
// Get the friendly name using VID and PID
var parts() as string = val1.ToArray("#")
var key as string = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\"
key = key + parts(0).Replace("\\?\", "") + "\"
key = key + parts(1) + "\"
key = key + parts(2)
var reg2 as new RegistryItem(key)
var val2 as string
try
val2 = reg2.Value("FriendlyName")
catch
return ""
end try
if val2 = "" then return ""
// Extract the Descriptions
val2 = val2.Replace("(COM", "|COM")
val2 = val2.Replace(")", "")
parts = val2.Split("|")
if parts.Count <> 2 then return ""
var Description as string = parts(0).Trim
return Description
#EndIf
return ""
End Function
As you can see, this is missing the implementation for macOS and Linux, but the one for Windows works as expected. I haven’t found a good way to obtain the friendly name for USB/Serial devices for macOS yet, and I haven’t even looked for Linux.
I’m hoping that someone can perhaps provide a lead on how to do this on macOS (without plugins, please). Thanks!