如何使用 PyUSB 与 USB 设备交互

How to interact with USB device using PyUSB

目前我已进入寻找设备的阶段,现在我已准备好使用第 22 页 specification 中列出的设备协议与 USB 通信。

我的机器上安装了 libusb,PyUSB 也安装了。

import usb.core
import usb.util

# find our device
dev = usb.core.find(idVendor=0x067b, idProduct=0x2303)

# was it found?
if dev is None:
    raise ValueError('Device not found')

# b are bytes, w are words

reqType = ''
bReq = ''
wVal = ''
wIndex = ''

dev.ctrl_transfer(reqType, bReq, wVal, wIndex, [])

上面的示例试图使用控制传输,我认为这就是协议所描述的内容。

我只想知道我的方向是否正确,或者我是否做错了根本性的事情。

设备已找到,只是我不确定的下一部分。

https://github.com/walac/pyusb/blob/master/docs/tutorial.rst 章节中有一个示例 跟我说说,亲爱的

>>> msg = 'test'
>>> assert dev.ctrl_transfer(0x40, CTRL_LOOPBACK_WRITE, 0, 0, msg) == len(msg)
>>> ret = dev.ctrl_transfer(0xC0, CTRL_LOOPBACK_READ, 0, 0, len(msg))
>>> sret = ''.join([chr(x) for x in ret])
>>> assert sret == msg

如果你想写入端点(批量传输等),你必须遵守 USB 树结构:-> configuration -> claim interface -> get endpoint ...

规范第22页不是USB协议是GNET协议(我不知道)。关键是您不需要低级 USB 即可与设备通信。您可以在 linux 或 windows 中使用标准的 tty 程序(echoscreenputtysocat、...)