【发布时间】:2022-11-04 17:09:29
【问题描述】:
我目前正在尝试编写一个应该以编程方式拦截条形码扫描仪输出的服务。我设法让 pyusb 在 Windows 上工作(必须安装 libusb-1.0 作为后端),我可以枚举所有连接的 USB 设备以及描述符的层次结构。到目前为止,一切都很好。
下一步是从端点获取数据。我无法完全弄清楚 pyusb 在该特定情况下应该如何工作,即使在阅读了设备的用户手册(如果有帮助的话,它是 HHP 3800g)之后,我也无法提供任何关于如何实现这一点的智慧。 pyusb 读取不起作用。
看到两个接口都是 HID ifaces,我尝试使用 hidapi 访问设备输入。我设法使用路径描述符打开设备,但读取操作不起作用。
iface 0 is HDI Keyboard emulation (usage=2)
iface 1 is HDI POS (usage=6)
我什至试图用 Wireshark/USBPcap 嗅探 USB 流量,但没有用。我可以看到来自鼠标的 USB 流量(wee!),但没有来自条形码扫描仪的帧。
我应该补充一点,扫描仪可以正常工作,被视为该死的键盘并相应地表现。
下面是pyusb提取的设备的完整描述符。代码的 sn-p 紧随其后。
DEVICE ID 0536:02e1 on Bus 001 Address 002 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x110 USB 1.1
bDeviceClass : 0x0 Specified at interface
bDeviceSubClass : 0x0
bDeviceProtocol : 0x0
bMaxPacketSize0 : 0x20 (32 bytes)
idVendor : 0x0536
idProduct : 0x02e1
bcdDevice : 0x0 Device 0.0
iManufacturer : 0x1 Hand Held Products
iProduct : 0x2 3800G
iSerialNumber : 0x8 08011D1080
bNumConfigurations : 0x1
CONFIGURATION 1: 250 mA ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x49 (73 bytes)
bNumInterfaces : 0x2
bConfigurationValue : 0x1
iConfiguration : 0x3 Default
bmAttributes : 0xa0 Bus Powered, Remote Wakeup
bMaxPower : 0x7d (250 mA)
INTERFACE 0: Human Interface Device ====================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x2
bInterfaceClass : 0x3 Human Interface Device
bInterfaceSubClass : 0x1
bInterfaceProtocol : 0x1
iInterface : 0x4 HID Keyboard Emulation
ENDPOINT 0x83: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x83 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0x8
ENDPOINT 0x4: Interrupt OUT ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x4 OUT
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0x8
ENDPOINT 0x2: Interrupt OUT ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x2 OUT
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0x1
HIDAPI python 代码
device_list = hid.enumerate(DEVICE_ID[0], DEVICE_ID[1]) # got 2 devices (2 ifaces)
device_desc = next(dev for dev in device_list if dev['usage'] == 2) # alternately tried 2/6
device = hid.device()
device.open_path(device_desc['path'])
device.set_nonblocking(1) # tried with 0, no difference
while True:
d = device.read(64)
if d:
print(d)
times.sleep(0.05)
注意事项
- pyusb 只能与适当的后端一起工作,不能在 Windows 上本地运行。很容易修复
- USBPcap安装自己的驱动捕获USB流量导致pyusb无法工作(我没有 尝试手动设置后端参数)
最后的笔记
我不得不说,使用 input/raw_input 来获取扫描的条形码不是一种选择。它可以工作,但我需要区分合法的键盘输入和条形码扫描仪输入,因此我的目标是访问第二个 HID iface。
我还尝试使用 Windows Linux 子系统访问 USB 端口,但无济于事。我在 Windows 10 上并且与 USB 相关的东西被禁用(简称没有“lsusb”)
我有点急于让它工作,也许我错过了一些微不足道的东西,但我不是一个经验丰富的 USB 专家,因为我只阅读了“USB in a nutshell”并查看了一些接口的代码示例与非 HID 设备。
非常感谢任何帮助和见解。
【问题讨论】: