我想在OS上使用Python从USB扫描仪读取一个字符串。下面的示例是我的出发点,并且我已经能够根据我的扫描器定制代码:我已经成功地执行了命令: h.open(),并打印出了制造商和产品字符串。The 扫描码用EVDEV用扫描仪进行验证。
挑战是解释返回的数据并将其映射回ascii字符串。
from __future__ import print_function
import hid
import time
print("Opening the device")
h = hid.device()
h.open(1118, 2048) # A Microsoft wireless combo keyboard & mouse
print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())
try:
while True:
d = h.read(64)
if d:
print('read: "{}"'.format(d))
finally:
print("Closing the device")
h.close()$ sudo python try.py输出:
Opening the device
Manufacturer: Microsoft
Product: Microsoft® Nano Transceiver v2.0
Serial No: None
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
--8<-- snip lots of repeated lines --8<--
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 7, 0, 0, 0]"
read: "[0, 0, 4, 9, 7, 0, 0, 0]"
read: "[0, 0, 7, 0, 0, 0, 0, 0]"
^Closing the device
Traceback (most recent call last):
File "try.py", line 17, in <module>
d = h.read(64)
KeyboardInterrupt问题
我找不到好的例子(就像EVDEV发现的那样)。任何与此等价的链接都会非常有帮助。--在没有良好文档的情况下解释输出是一个挑战。
如果将64替换为1,2,3.8中的一个数字,则列表中的元素数是相同的。9或更多的结果是由8个元素组成的列表。
我期待得到回应:,如果您有使用HIDAPI的经验(特别是使用Python),请在您的回答中说明这一点。输入双奖金一轮HID扫描仪体验
发布于 2021-03-07 19:50:10
HIDAPI ( Python正在使用的)不检索描述符,这是解析传入数据所需要的。您需要的是转储和解码这些描述符的方法:https://github.com/todbot/mac-hid-dump https://eleccelerator.com/usbdescreqparser/
发布于 2018-10-19 11:58:17
您需要阅读和理解USB规范。
按“搜索”按钮,查看文档。这是最重要的:“HID 1.11的设备类定义”也见“使用页面”文档。
每个设备都发送一个HID描述符,准确地描述报表中的每一个位。因此,要解释报告,代码可以解析描述符(api),也可以手动将字节/位分配给结构(不推荐,因为它只适用于众所周知的设备)。
https://stackoverflow.com/questions/50361770
复制相似问题