在Python中过滤USB HID列
我遇到一个问题,找不到答案。请注意,我不是专家,可能会漏掉一些简单的东西,比如我打印设备的方式。
我想从一个USB设备上存储一个设备版本号(下面简称dvm)。问题是这个USB设备有两个报告列,一个是功能列,一个是输出列。
所以当我打印这个设备时,会得到两组打印结果。
#这是我定义的一个函数:
def device(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设备呢?
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()
问题解决了。