[Tutor] Using pyusb

Mike Holloway me at mikeholloway.co.uk
Wed Mar 19 21:14:56 CET 2008


Hi all,

I'm very new to this list, so hello all!
I'm just starting out using python, my background is lamp.

I have a Prolific Technologies bridged usb cable that I wish to talk to 
using pyusb and libusb, I've tried following examples and compiling my 
own code but I'm really struggling getting it to work.

I'm trying to send a sequence of letters to the cable, for them to 
reappear on the other side. I've been trying to use bulkWrite and 
bulkRead methods but I'm not sure I'm using them right. There's also 
controlMethod, but I'm not sure what that is used for.

Can anyone help get me started, I'm concerned mostly with the 
communication, I reckon I could actually get somewhere if I can just 
nail the first bit, here's my code so far:

* Cheers in advance, Mike.


import usb
import sys
import os
import time
from array import array

class DeviceDescriptor:
    def __init__(self, vendor_id, product_id, interface_id) :
        self.vendor_id = vendor_id
        self.product_id = product_id
        self.interface_id = interface_id

    def get_device(self) :
        buses = usb.busses()
        for bus in buses :
            for device in bus.devices :
                if device.idVendor == self.vendor_id :
                    if device.idProduct == self.product_id :
                        return device
        return None

class XFPS():
    VENDOR_ID      = 0x067B #: Vendor Id
    PRODUCT_ID   = 0x0000   #: Product Id for the bridged usb cable
    INTERFACE_ID = 0        #: The interface we use to talk to the device
    BULK_IN_EP   = 0x83     #: Endpoint for Bulk reads
    BULK_OUT_EP  = 0x02     #: Endpoint for Bulk writes
    PACKET_LENGTH = 0x40    #: 64 bytes

    device_descriptor = DeviceDescriptor(VENDOR_ID, \
                                         PRODUCT_ID, INTERFACE_ID)

    def __init__(self,) :
        # The actual device (PyUSB object)
        self.device = self.device_descriptor.get_device()
        # Handle that is used to communicate with device. Setup in L{open}
        self.handle = None
   
    def open(self) :
        self.device = self.device_descriptor.get_device()
        if not self.device:
            print >> sys.stderr, "Cable isn't plugged in"
        try:
            self.handle = self.device.open()
            self.handle.claimInterface(self.device_descriptor.interface_id)
        except usb.USBError, err:
            print >> sys.stderr, err

    def close(self):   
        """ Release device interface """
        try:
            self.handle.reset()
            self.handle.releaseInterface()
        except Exception, err:
            print >> sys.stderr, err
        self.handle, self.device = None, None
       
    def my_bulk_write(self):
        A = chr(0x75) # u
        B = chr(0x69) # i
        X = chr(0x6F) # o
        Y = chr(0x70) # p
        LB = chr(0x6C) # l
        RB = chr(0x6B) # k
        LT = chr(0x68) # h
        RT = chr(0x6A) # j

        S = chr(0x32)
        s = chr(0x73)

        self.close()
        self.open()
       
        msg = [A,B,A,B,A,B,A,B]
        #help(self.handle.bulkWrite)
        help(self.handle.interruptWrite)
        sent_bytes = self.handle.interruptWrite(XFPS.BULK_OUT_EP,msg,1000)
        print sent_bytes
        if sent_bytes:
            read_bytes = self.handle.interruptRead(0x81,sent_bytes);
            print read_bytes

xfps = XFPS()
xfps.open()
xfps.my_bulk_write()



More information about the Tutor mailing list