-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialDevID.m
46 lines (42 loc) · 1.46 KB
/
serialDevID.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function [IDstrings,ports]=serialDevID(ports)
% Find identifier names for USB-serial converters connected to the USB
% ports. A problem with non-serial locked drivers is that the enumeration
% of a dongle connected to an arbitrarily port can take any value.
%
% port: single character or string array, e.g. '/dev/ttyUSB0', for a single port
% or string array, e.g. ["/dev/ttyUSB0","/dev/ttyACM0"], serialportlist('all')
%
% Empty IDstrings are returned for nonexisting ports or ports which are not
% virtual ports created by USB converters, e.g. real physical ports (/dev/ttyS*)
%
% Example: [IDs,names]=serialDevID(serialportlist)
%
% Using strings, i.e. working only in matlab>2017a
% In Linux (ubuntu XX): resolve the symlinks in the pseudodir /dev/serial/by-id
if ~exist('ports','var')
try
ports=serialportlist('all');
catch
ports=seriallist; %older matlab versions
end
end
% set up for single or multiple ports
if isa(ports,'string') && length(ports)>1
nports=length(ports);
elseif ~isempty(ports)
nports=1;
ports=string(ports);
else
nports=0;
end
IDstrings(1:nports)="";
for i=1:nports
% strip '/dev/'
portID=strrep(char(ports(i)),'/dev/','');
IDstring='';
if exist('/dev/serial/by-id','file')
[~,IDstring]=unix(['find /dev/serial/by-id -type l -printf ''%l %p\n'' |grep ' ...
portID '| cut -d" " -f2']);
end
IDstrings(i)=strrep(IDstring(1:end-1),'/dev/serial/by-id/','');
end