0

我似乎无法找到答案。请记住,我不是专家,所以我可能会遗漏简单的内容,也许我正在打印我想要的设备。我正在尝试从 USB 设备存储设备版本号(来自此处的 dvm)。问题是 USB 设备有两列用于报告、功能和输出。

因此,当我打印设备时,我会收到两组打印语句。

#

def 设备(target_usage,target_vendor_id):

hidDevice = False
all_devices = hid.HidDeviceFilter(vendor_id = target_vendor_id).get_devices()
print "\n", all_devices
if len(all_devices) == 0:
    # Exit if no devices found, report error.
    hidDevice = False
    time.sleep(0.2)
    print "No devices can be detected, please check device is connected."
    sys.exit(1)
    return
elif len(all_devices) > 2:
    # announce there are more than 1 device connected to prevent conflicting upgrades
    hidDevice = True
    time.sleep(0.2)
    print "Too many devices connected, ensure the only device connected is the device needed to test."
    sys.exit(1)

else:
    # loop through all devices
    for device in all_devices:
        try:
            device.open()

            # print device details
            device_name = unicode("=== INFO: Found %s %s (vID=[%04x], pID=[%04x], version number [%04x]) ===" % \
            (device.vendor_name, device.product_name, device.vendor_id, device.product_id, device.version_number))
            dvm = unicode("%04x" % \
            (device.version_number))
            print dvm;
            print device_name; 

        finally:
            device.close()

    hidDevice = True

return hidDevice
#

调用此函数时,它将打印所有设备,但我最终得到以下结果(针对隐私问题修改了 pids/vids 等。)

[HID 设备 (vID=0x0000, pID=0x0000, v=0x0000); 制作; 型号,路径:\?\hid#vid_0000&pid_0000&col01#7&00000000&1&0000#{00000000-0000-0000-0000-000000000000},HID设备(vID=0x0000,pID=0x0000,v=0x0000);制作; 型号,路径:\?\hid#vid_0000&pid_0000&col02#7&00000000&1&0000#{00000000-0000-0000-0000-000000000000}

重要的部分是 col01 和 col02。

如何过滤掉第二个枚举的 HID 设备?

4

1 回答 1

0

包括以下代码。

for report in device.find_output_reports(): 
                    if target_usage in report:
                        # add to target list
                        Targets.append(device)

            finally:
                device.close()

        for item in Targets:        

            try:
                item.open(output_only = True)
                dvm = unicode("%04x" % \
                (device.version_number))
                print dvm
            finally:
                item.close()

问题已解决。

于 2013-02-14T11:41:29.487 回答