Sending USB commands with Python

Adam W. AWasilenko at gmail.com
Wed Aug 29 08:47:00 EDT 2012


On Wednesday, August 29, 2012 2:45:17 AM UTC-4, Tim Roberts wrote:
> Which operating system are you using?  If you are on Windows, then the
> 
> operating system has already loaded a printer driver for this device. 
> 
> 
> The libusb or libusbx libraries can be used to talk to USB devices.  There
> 
> is a Python binding.  On Windows, you still need to have a driver, but the
> 
> libusbx instructions can help you find an install one.
> 

I am on Windows and have installed a driver using libusb-win32.  Using http://pyusb.sourceforge.net/docs/1.0/tutorial.html as a template, this is my code so far:

import usb.core
import usb.util

dev = usb.core.find(idVendor=0x0922, idProduct=0x0021)

# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()

# get an endpoint instance
cfg = dev.get_active_configuration()
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_settting = usb.control.get_interface(dev,interface_number)
intf = usb.util.find_descriptor(
    cfg, bInterfaceNumber = interface_number,
    bAlternateSetting = 0
)

ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = \
    lambda e: \
        usb.util.endpoint_direction(e.bEndpointAddress) == \
        usb.util.ENDPOINT_OUT
)

assert ep is not None


I had to manually set bAlternateSetting to 0 for it to run and add dev to usb.control.get_interface(dev,interface_number).

Trying to do the status thing mentioned before, in the interpreter I did:

>>> ep.write('A')
2

And the manual says 2 is not a valid option... So something isn't adding up.



More information about the Python-list mailing list