-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindPortOf.m
51 lines (44 loc) · 1.67 KB
/
FindPortOf.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
47
48
49
50
function [ port ] = FindPortOf( deviceToFind )
%FINDPORTOF Finds the COM port of the named device via the Windows registry
% Detailed explanation goes here
NONE = 0;
MIDDLE = 1;
START = 2;
CASE_INSENSITIVE = 3;
EXACT = 4;
bestMatchSoFar = NONE;
port = [];
% Query registry for USB devices
usbEnumKey = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\';
queryFlags = '/s /f FriendlyName /t REG_SZ';
[~, usbEnum] = dos(['REG QUERY ' usbEnumKey ' ' queryFlags]);
% Extract the relevant parts of the 'reg query' output
patternForCOM = '^ FriendlyName REG_SZ (.*?COM.*?)$';
%patternForCOM = '^ FriendlyName REG_SZ (.*?)$';
deviceStrings = regexp(usbEnum, patternForCOM, 'tokens', 'dotexceptnewline', 'lineanchors');
if isempty(deviceStrings)
port = [];
return;
end
% Parse the device names and port numbers
devices = regexp([deviceStrings{:}], '(.*) \((COM[\d*])\)', 'tokens');
for devIdx = 1:length(devices)
device = devices{devIdx}{1};
if ~isempty(device) % if empty, regex didn't match at all
name = device{1};
if bestMatchSoFar < EXACT && strcmp(name, deviceToFind)
port = device{2};
bestMatchSoFar = EXACT;
elseif bestMatchSoFar < CASE_INSENSITIVE && strcmpi(name, deviceToFind)
port = device{2};
bestMatchSoFar = CASE_INSENSITIVE;
elseif bestMatchSoFar < START && strncmpi(name, deviceToFind, min(length(name), length(deviceToFind)))
port = device{2};
bestMatchSoFar = START;
elseif bestMatchSoFar < MIDDLE && ~isempty(strfind(name, deviceToFind))
port = device{2};
bestMatchSoFar = MIDDLE;
end
end
end
end