File Exchange

image thumbnail

Identify Serial COM devices by friendly name in Windows

version 1.1.0.0 (1.94 KB) by Benjamin Avants
Uses Windows registry information to ID Serial COM devices

8 Downloads

Updated 08 Apr 2014

View License

This function scans the Windows registry in two places and associates active COM ports with their registered "Friendly Name". The function returns an N x 2 cell array where N is the number of active COM ports. The first column contains the friendly names of the devices where they exist and the second contains their COM number. If no devices with friendly names are found, devices is empty.
:
This function is not necessarily optimized but it is my solution to identifying serial devices connected by virtual COM port in Windows without having to query every device. If multiple devices with the same friendly name are connected to the computer, the output will contain both devices with their COM ports but they will be otherwise indistinguishable by this function.

Cite As

Benjamin Avants (2020). Identify Serial COM devices by friendly name in Windows (https://www.mathworks.com/matlabcentral/fileexchange/45675-identify-serial-com-devices-by-friendly-name-in-windows), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Serge

I prefer this simpler and more complete version:

function ports = comports
%List serial COM ports as a struct with names (Windows only).
% ports = comports -struct with fields: type, port, name
%
%To improve speed (x10) add jsystem to the path from here:
% https://github.com/avivrosenberg/matlab-jsystem/blob/master/src/jsystem.m

[err,str] = system('REG QUERY HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM');
if err
ports = [];
else
ports = regexp(str,'\\Device\\(?<type>[^ ]*) *REG_SZ *(?<port>COM.*?)\n','names');
cmd = 'REG QUERY HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ /s /f "FriendlyName" /t "REG_SZ"';
if exist('jsystem','file')==2 %~10x faster then 'system'
[~,str] = jsystem(cmd,'noshell');
else
[~,str] = system(cmd);
end
names = regexp(str,'FriendlyName *REG_SZ *(?<name>.*?) \((?<port>COM.*?)\)','names');
[i,j] = ismember({ports.port},{names.port});
[ports(i).name] = names(j(i)).name;
end

Does not report the active devices correctly. Missing the Enum services. I have made modifications to the code in
https://www.mathworks.com/matlabcentral/fileexchange/65501-identify-serial-com-devices-by-friendly-name-and-service-in-windows

Lars Risbo

It seems that DOS is not recognising the REG command? Running Win7/64 and Matlab 2017a

>> Skey = 'HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM';
>> [~, list] = dos(['REG QUERY ' Skey]);
>> list

list =

''REG' is not recognized as an internal or external command,
operable program or batch file.
'

Jim Hokanson

Hi Benjamin,

As Matthias points out your current code implementation assumes USB implementations of a serial port. You can point to \ENUM\ instead and get everything. Also, for what it is worth, I would prefer to see the use of structures instead of cell arrays. Overall though this was helpful in getting me started.

Thanks,
Jim

bien le

great work Benjamin!

Matthias

Hi Benjamin,

was not working for me until I changed the \ENUM\USB\ to \ENUM\FTDIBUS for a list of Serial Ports.

Now it works for me, thanks a lot!

Matt

To my knowledge, this is the first function that does this. Its faster than similar functions that start by trying to open a COM ports or using the system('mode') command, and it's better because it helps find the particular device you're looking for.

Matt

Updates

1.1.0.0

4/8/2014: Added checks on registry queries to ensure SKEYs are valid. Displays an error and returns if either entry is invalid.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux