How to interact with USB device using PyUSB

假装没事ソ 提交于 2019-12-08 01:26:00

问题


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 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.


回答1:


there is an example in https://github.com/walac/pyusb/blob/master/docs/tutorial.rst chapter Talk to me, honey

>>> 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

if you want to write to endpoints (bulk transfers etc) you have to obey the USB tree structure: -> configuration -> claim interface -> get endpoint ...

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



来源:https://stackoverflow.com/questions/44290837/how-to-interact-with-usb-device-using-pyusb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!