How to retrieve 'bus reported device description' for Universal Serial Bus Controllers in MATLAB?

918 views Asked by At

I am trying to extract 'Bus reported device description' and 'Bus Relations' informations for Universal Serial Bus Controllers in Matlab.

I can't find it in the registry and I don't know exactly how to use setupapi.dll function in Matlab to get the informations.

I want to do this because I have a plurality of Arduino Nano devices and all of them have different COMs. I also use other USB Serial Devices under Matlab and for all of them I must create different serial objects, with different COM port names.

I want to create a Matlab function which will return what devices are connected on USB ports and what COM port they use .

I hope someone can help me with some ideas or code examples.

Thanks in advance!

1

There are 1 answers

0
Cloudy On

This is a very late answer, but I also run into this problem and I have found a solution to find the "Bus reported device description" after searching on the Internet. I do not need "Bus Relations" in my case but I guess it may be retrieved in a similar way. I am using Windows 10 21H1 and MATLAB R2021a.

In short, my solution has 4 steps:

  1. Find all the active COM port devices.
  2. Find the friendly names of all devices.
  3. Based on 1 and 2, get the friendly names of all active COM port devices.
  4. Based on 3, get the "Bus reported device description" of all active COM port devices.

Code:

% To improve speed (x10) add jsystem to the path from here:
% https://github.com/avivrosenberg/matlab-jsystem/blob/master/src/jsystem.m
if exist('jsystem','file')
    fsystem = @jsystem;
else
    fsystem = @system;
end

% Find all the active COM ports
com = 'REG QUERY HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM';
[err,str] = fsystem(com);
if err
    error('Error when executing the system command "%s"',com);
end

% Find the friendly names of all devices
ports = regexp(str,'\\Device\\(?<type>[^ ]*) *REG_SZ *(?<port>COM\d+)','names','dotexceptnewline');
cmd = 'REG QUERY HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ /s /f "FriendlyName" /t "REG_SZ"';
[~,str] = fsystem(cmd);  % 'noshell'

% Get the friendly names of all active COM port devices
names = regexp(str,'FriendlyName *REG_SZ *(?<name>.*?) \((?<port>COM\d+)\)','names','dotexceptnewline');
[i,j] = ismember({ports.port},{names.port});
[ports(i).name] = names(j(i)).name;

% Get the "Bus reported device description" of all active COM port devices
for i = 1:length(ports)
    cmd = sprintf('powershell -command "(Get-WMIObject Win32_PnPEntity | where {$_.name -match ''(%s)''}).GetDeviceProperties(''DEVPKEY_Device_BusReportedDeviceDesc'').DeviceProperties.Data"',ports(i).port);
    [~,ports(i).USBDescriptorName] = fsystem(cmd);
end

This solution works but may not be clean. I am not a Windows expert anyway. Suggestions are highly appreciated.