如何使用PyUSB与USB设备交互 [英] How to interact with USB device using PyUSB
问题描述
I have so far gotten to the stage of finding the device, now I am ready to talk to the USB using the devices protocol laid out in the specification on page 22.
libusb已安装在我的计算机上,PyUSB也已安装.
libusb is installed on my machine and so is 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, [])
上面的示例正在尝试使用控制传输,我认为这是协议描述的内容.
The above example is attempting to use a control transfer, which I assume is what the protocol describes.
我只想知道我是沿着正确的路线还是做的是根本上错误的事情.
I just want to know if I am along the right lines or if I am doing something fundamentally wrong.
找到了设备,这只是我不确定的下一部分.
The device is found, it's just the next part I am unsure of.
推荐答案
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
...
if you want to write to endpoints (bulk transfers etc) you have to obey the USB tree structure: -> configuration -> claim interface -> get endpoint
...
不是USB协议是GNET协议(我不知道).关键是您不需要低级USB即可与设备通话.您可以在Linux上使用标准的tty程序(echo
,screen
,putty
,socat
...),或者在Windows中使用类似的程序
on page 22 of the specification is not USB protocol is GNET protocol (which i do not know). the point is that you do not need low level USB to talk to the device. you can use standard tty programs (echo
, screen
, putty
, socat
,...) on linux or something analog in windows
这篇关于如何使用PyUSB与USB设备交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!