【问题标题】:How to retrieve 'bus reported device description' for Universal Serial Bus Controllers in MATLAB?如何在 MATLAB 中检索通用串行总线控制器的“总线报告设备描述”?
【发布时间】:2015-01-31 00:24:33
【问题描述】:

我正在尝试在 Matlab 中提取通用串行总线控制器的“总线报告设备描述”和“总线关系”信息。

我在注册表中找不到它,我不知道如何在 Matlab 中使用 setupapi.dll 函数来获取信息。

我想这样做是因为我有多个 Arduino Nano 设备并且它们都有不同的 COM。 我还在 Matlab 下使用其他 USB 串行设备,对于所有这些设备,我必须创建不同的串行对象,具有不同的 COM 端口名称。

我想创建一个 Matlab 函数,该函数将返回 USB 端口上连接的设备以​​及它们使用的 COM 端口。

我希望有人可以帮助我提供一些想法或代码示例。

提前致谢!

【问题讨论】:

    标签: matlab setupapi device-manager


    【解决方案1】:

    这是一个很晚的答案,但我也遇到了这个问题,我在网上搜索后找到了解决方案,找到了“总线报告的设备描述”。在我的情况下,我不需要“总线关系”,但我想它可能会以类似的方式检索。我正在使用 Windows 10 21H1 和 MATLAB R2021a。

    简而言之,我的解决方案有 4 个步骤:

    1. 查找所有活动的 COM 端口设备。
    2. 查找所有设备的友好名称。
    3. 根据 1 和 2,获取所有活动 COM 端口设备的友好名称。
    4. 基于3,获取所有活动COM口设备的“Bus reported device description”。

    代码:

    % 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
    

    此解决方案有效,但可能不干净。反正我不是 Windows 专家。非常感谢您的建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多