How to interact with USB device using PyUSB

27,828

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

Share:
27,828
Danny Cullen
Author by

Danny Cullen

Updated on July 09, 2022

Comments

  • Danny Cullen
    Danny Cullen almost 2 years

    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.

  • Danny Cullen
    Danny Cullen almost 7 years
    I managed to connect via PuTTY to the COM port of my device, however It is giving me no feedback (black screen), even if I type and blindly type commands i.e. STX,N,CR I should probably ask another question.