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!
Sorry, can’t help on Mac (but am interested in solutions, with or without plug-ins).
Interesting how you get FriendlyName under Win using the Registry. I use Win32 API calls via Chris Carter’s DeviceList classes: http://www.cmas-net.co.uk/rslug/Eg-5-DeviceList.rbp, but a 64-bit-compatible solution would be great.
With Chris’ classes I can get the “Raw Name”, which includes an FTDI converter chip’s (user-programmable) serial number - can you also get the FTDI serial number using the Registry?
When I then lookup the details for this device using Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS\VID_0403+PID_6010+FT0LPM5UA\0000, I get the following:
I managed to cobble a Linux version together that seems to work at least on my Raspberry Pi. This needs some testing on other distributions:
// Get udevadm info
Dim sh As Shell
sh = New Shell
sh.Execute("udevadm info " + PortName)
var s as string = Trim(sh.Result)
// Analyze data
var lines() as string = s.ReplaceLineEndings(EndOfLine).Split(EndOfLine)
var model as string
var vendor as string
for each line as string in lines
if vendor = "" then
if line.IndexOf("ID_VENDOR") >= 0 then
var parts() as string = line.Split("=")
if parts.Count = 2 then
vendor = parts(1).Trim
end if
end if
end if
if model = "" then
if line.IndexOf("ID_MODEL") >= 0 then
var parts() as string = line.Split("=")
if parts.Count = 2 then
model = parts(1).Trim
end if
end if
end if
if vendor <> "" and model <> "" then exit
Next
if vendor <> "" or model <> "" then
if vendor.IndexOf("__") >= 0 then
var parts() as string = vendor.Split("__")
vendor = parts(0).Trim
end if
return new Pair(model, vendor)
else
return nil
end if