首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解释HIDAPI python输出

解释HIDAPI python输出
EN

Stack Overflow用户
提问于 2018-05-16 02:48:34
回答 2查看 2.6K关注 0票数 4

我想在OS上使用Python从USB扫描仪读取一个字符串。下面的示例是我的出发点,并且我已经能够根据我的扫描器定制代码:我已经成功地执行了命令: h.open(),并打印出了制造商和产品字符串。The 扫描码用EVDEV用扫描仪进行验证。

挑战是解释返回的数据并将其映射回ascii字符串。

本文提供python示例代码。

代码语言:javascript
复制
 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输出:

代码语言:javascript
复制
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发现的那样)。任何与此等价的链接都会非常有帮助。--在没有良好文档的情况下解释输出是一个挑战。

  1. 为什么为h.read()选择64? D= h.read(64)

如果将64替换为1,2,3.8中的一个数字,则列表中的元素数是相同的。9或更多的结果是由8个元素组成的列表。

  1. 为什么变量'd‘是一个包含8个元素的输出列表?(在任何地方都没有指定8) 打印(“阅读:{}”‘.format(D)’)
  2. 每个打印输出列表代表什么?1类型字符?
  3. 输出列表中的每一列代表什么\ encode?
  4. 是否有一个已出版的表格将数字映射到键盘上?

我期待得到回应:,如果您有使用HIDAPI的经验(特别是使用Python),请在您的回答中说明这一点。输入双奖金一轮HID扫描仪体验

EN

回答 2

Stack Overflow用户

发布于 2021-03-07 19:50:10

HIDAPI ( Python正在使用的)不检索描述符,这是解析传入数据所需要的。您需要的是转储和解码这些描述符的方法:https://github.com/todbot/mac-hid-dump https://eleccelerator.com/usbdescreqparser/

票数 2
EN

Stack Overflow用户

发布于 2018-10-19 11:58:17

您需要阅读和理解USB规范。

Hid规格

按“搜索”按钮,查看文档。这是最重要的:“HID 1.11的设备类定义”也见“使用页面”文档。

每个设备都发送一个HID描述符,准确地描述报表中的每一个位。因此,要解释报告,代码可以解析描述符(api),也可以手动将字节/位分配给结构(不推荐,因为它只适用于众所周知的设备)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50361770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档