I am working on several projects that involve using Arduinos and other serial connected devices with a MATLAB GUI. I have a solution for identifying the devices that opens all the COM ports one at a time and queries them. By checking the responses I can identify which devices are connected to which COM ports. This only works if the devices have unique responses to the query sent. I am currently using *IDN? as my query because it seems to be fairly standard. Of course, to interact with the Arduinos, I have to make sure they respond to this query in a predictable way. My problem is that this process is fairly slow and a little error prone. In Windows Device Manager, each Port is identified by name and COM number. Is there any way to get these names inside of MATLAB? I want to be able to ID the ports without having to open connections to them all.
Prashant Kumar answered .
2025-11-20
Skey = 'HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM';
% Find connected serial devices and clean up the output
[~, list] = dos(['REG QUERY ' Skey]);
list = strread(list,'%s','delimiter',' ');
coms = 0;
for i = 1:numel(list)
if strcmp(list{i}(1:3),'COM')
if ~iscell(coms)
coms = list(i);
else
coms{end+1} = list{i};
end
end
end
key = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\';
% Find all installed USB devices entries and clean up the output
[~, vals] = dos(['REG QUERY ' key ' /s /f "FriendlyName" /t "REG_SZ"']);
vals = textscan(vals,'%s','delimiter','\t');
vals = cat(1,vals{:});
out = 0;
% Find all friendly name property entries
for i = 1:numel(vals)
if strcmp(vals{i}(1:min(12,end)),'FriendlyName')
if ~iscell(out)
out = vals(i);
else
out{end+1} = vals{i};
end
end
end
% Compare friendly name entries with connected ports and generate output
for i = 1:numel(coms)
match = strfind(out,[coms{i},')']);
ind = 0;
for j = 1:numel(match)
if ~isempty(match{j})
ind = j;
end
end
if ind ~= 0
com = str2double(coms{i}(4:end));
% Trim the trailing ' (COM##)' from the friendly name - works on ports from 1 to 99
if com > 9
length = 8;
else
length = 7;
end
devs{i,1} = out{ind}(27:end-length);
devs{i,2} = com;
end
end