Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 690
Danny Cullen Avatar asked Nov 27 '25 22:11

Danny Cullen


1 Answers

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

like image 77
ralf htp Avatar answered Nov 30 '25 11:11

ralf htp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!