Thanks, that did it, I also added error message checking.
FINAL PROGRAM
class DelcomUSBDevice(object):
'''
Class for talking to Delcom Products generation 2 USB device
902670/902770
the 16 GPIO device
'''
# Device Constants
VENDOR_ID = 0x0FC5 #: Delcom Product ID
PRODUCT_ID = 0xB080 #: Delcom Product ID
INTERFACE_ID = 0 #: The interface we use
def __init__(self):
'''
Constructor
'''
self.deviceDescriptor = DeviceDescriptor(self.VENDOR_ID,
self.PRODUCT_ID,
self.INTERFACE_ID)
self.device = self.deviceDescriptor.getDevice()
if self.device:
print 'Found Delcom Device'
self.conf = self.device.configurations[0]
self.intf = self.conf.interfaces[0][0]
else:
print >> sys.stderr, "Cable isn't plugged in"
sys.exit(0)
def open(self):
""" Open the Delcom Interface """
try:
self.handle = self.device.open()
self.handle.detachKernelDriver(0)
except usb.USBError, err:
if str(err).find('could not detach kernel driver from
interface') >= 0:
print 'The in-kernel-HID driver has already been
detached'
else:
print >> sys.stderr, err
#self.handle.setConfiguration(self.conf)
self.handle.claimInterface(self.intf) # Interface 0
def close(self):
""" Release Delcom interface """
try:
self.handle.releaseInterface()
print 'released interface'
except Exception, err:
print >> sys.stderr, err
def getManufactureName(self):
""" Manufacturer of device """
return self.handle.getString(self.device.iManufacturer,30)
def getProductName(self):
""" Product name of device """
return self.handle.getString(self.device.iProduct,30)
def writeData(self, data):
""" Write data to device:
0x21 = REQ_TYPE: DIR = Host to Device
REQ_TYPE: TYPE = Class
REQ_TYPE: REC = Interface
0x09 = REQUEST: HID-Set Report
data = Command sent to Delcom device
0x0365 = VALUE: 0x65 = ReportID = 101 = MajorCMD
VALUE: 0x03 = Report Type = Feature Report
0x0000 = Interface number = 0
100 = timeout 100mS
"""
sent = self.handle.controlMsg(0x21,
0x09,
data,
0x0365,
0x0000,
100)
print 'Bytes written %s', sent
class DeviceDescriptor(object):
'''
Class for defining the USB device
'''
def __init__(self, vendor_id, product_id, interface_id):
'''
Constructor
'''
self.vendor_id = vendor_id
self.product_id = product_id
self.interface_id = interface_id
def getDevice(self):
'''
Return the device corresponding to the device descriptor if it
is
available on the USB bus. Otherwise return None. Note that the
returned device has yet to be claimed or opened.
'''
# Find all the USB busses
busses = usb.busses()
for bus in busses:
for device in bus.devices:
if device.idVendor == self.vendor_id and \
device.idProduct == self.product_id:
return device
return None
#main method
def main():
delcomUSBDevice = DelcomUSBDevice()
delcomUSBDevice.open()
print 'Manufacturer: ', delcomUSBDevice.getManufactureName()
print 'Product: ', delcomUSBDevice.getProductName()
msg = "\x65\x0C\x01\x00\x00\x00\x00\x00" # Turn on LED
#msg = "\x65\x0C\x00\x01\x00\x00\x00\x00" # Turn Off LED
delcomUSBDevice.writeData(msg)
delcomUSBDevice.close()
#allows use as a module or stand-alone script
if __name__ == '__main__': main()
On Sun, 2009-05-17 at 12:48 +0200, Thomas Reitmayr wrote:
> Ed,
> what happens if you just catch the exception from your detach command?
> Eg.
> try:
> self.handle.detachKernelDriver(0)
> except usb.USBError, err:
> print >> sys.stderr, err
>
> Otherwise, if the kernel driver is already detached, you will not claim
> the interface.
> Regards,
> -Thomas
>
>
> Am Samstag, den 16.05.2009, 21:34 -0700 schrieb ed:
> > When I first run my program everything is OK. That is I can detach the
> > kernel interface and then claim the interface and then release
> > the
> > interface.
> >
> > TERMINAL OUTPUT
> > e...@ed-desktop:~/Desktop$ sudo ./DelcomUSBDevice.py
> > Found Delcom Device
> > Claiming interface
> > Manufacturer: Delcom Products Inc.
> > Product: USB IO Controller
> > Bytes written %s 8
> > Sleep 2 seconds
> > close done
> >
> > But if I run the program a 2nd time or more I get two errors.
> > 1. could not detach kernel driver from interface 0: No data
> > available
> > 2. No interface claimed
> >
> > TERMINAL OUTPUT
> > e...@ed-desktop:~/Desktop$ sudo ./DelcomUSBDevice.py
> > Found Delcom Device
> > could not detach kernel driver from interface 0: No data
> > available
> > Manufacturer: Delcom Products Inc.
> > Product: USB IO Controller
> > Bytes written %s 8
> > Sleep 2 seconds
> > No interface claimed
> > e...@ed-desktop:~/Desktop$
> >
> >
> > Any clues to why this is happening?
> >
> >
> > MY PROGRAM
> >
> > class DelcomUSBDevice(object):
> >
> >
> > # Device Constants
> > VENDOR_ID = 0x0FC5 #: Delcom Product ID
> > PRODUCT_ID = 0xB080 #: Delcom Product ID
> > INTERFACE_ID = 0 #: The interface we use
> >
> > def __init__(self):
> > '''
> > Constructor
> > '''
> > self.deviceDescriptor = DeviceDescriptor(self.VENDOR_ID,
> >
> > self.PRODUCT_ID,
> >
> > self.INTERFACE_ID)
> > self.device = self.deviceDescriptor.getDevice()
> > if self.device:
> > print 'Found Delcom Device'
> > self.conf = self.device.configurations[0]
> > self.intf = self.conf.interfaces[0][0]
> > else:
> > print >> sys.stderr, "Cable isn't plugged in"
> > sys.exit(0)
> >
> >
> > def open(self):
> > """ Open the Delcom Interface """
> > try:
> > self.handle = self.device.open()
> > # Detach from kernel driver on interface 0 otherwise
> > # we will get device is busy
> > # when we try and claim the interface because the
> > kernel
> > # attached to the in-kernel-HID driver.
> > # Need to do only once, if you try 2nd time will get
> > # message
> > # "Could not detach kernel driver from interface 0:
> > # no data available. Can't figure out a way to
> > # only do this once.
> > self.handle.detachKernelDriver(0)
> > self.handle.setConfiguration(self.conf)
> > print 'Claiming interface'
> > self.handle.claimInterface(self.intf) # Interface 0
> > except usb.USBError, err:
> > print >> sys.stderr, err
> >
> >
> > def close(self):
> > """ Release Delcom interface """
> > try:
> > #self.handle.reset()
> > self.handle.releaseInterface()
> > print 'close done'
> > except Exception, err:
> > print >> sys.stderr, err
> >
> >
> > def getManufactureName(self):
> > """ Manufacturer of device """
> > return
> > self.handle.getString(self.device.iManufacturer,30)
> >
> >
> > def getProductName(self):
> > """ Product name of device """
> > return self.handle.getString(self.device.iProduct,30)
> >
> >
> > def writeData(self, data):
> > self.bytesWritten = self.handle.controlMsg(0x21,
> > #REQ_TYPE
> > 0x09,
> > #REQUEST:
> > data,
> > #BUFFER:
> > 0x0365,
> > #VALUE:
> > 0x0000,
> > #INDEX:
> > 100)
> > #TIMEOUT
> >
> > print 'Bytes written %s', self.bytesWritten
> >
> >
> >
> > class DeviceDescriptor(object):
> > '''
> > Class for defining the USB device
> > '''
> >
> > def __init__(self, vendor_id, product_id, interface_id):
> > '''
> > Constructor
> > '''
> > self.vendor_id = vendor_id
> > self.product_id = product_id
> > self.interface_id = interface_id
> >
> > def getDevice(self):
> > '''
> > Return the device corresponding to the device descriptor
> > if it
> > is available on the USB bus. Otherwise return None.
> > Note that
> > the returned device has yet to be claimed or opened.
> > '''
> > # Find all the USB busses
> > busses = usb.busses()
> > for bus in busses:
> > for device in bus.devices:
> > if device.idVendor == self.vendor_id and
> > device.idProduct == self.product_id:
> > return device
> > return None
> >
> >
> >
> >
> >
> >
> >
> > #main method
> > def main():
> > delcomUSBDevice = DelcomUSBDevice()
> > delcomUSBDevice.open()
> > print 'Manufacturer: ', delcomUSBDevice.getManufactureName()
> > print 'Product: ', delcomUSBDevice.getProductName()
> > msg = "\x65\x0C\x01\x00\x00\x00\x00\x00" # Turn on LED
> > #msg = "\x65\x0C\x00\x01\x00\x00\x00\x00" # Turn Off LED
> > delcomUSBDevice.writeData(msg)
> > print 'Sleep 2 seconds'
> > time.sleep(2)
> > delcomUSBDevice.close()
> >
> > #allows use as a module or stand-alone script
> > if __name__ == '__main__': main()
> >
> >
> >
> > Thanks
> > Ed
> >
> >
> >
> >
> >
> > ------------------------------------------------------------------------------
>
>
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Pyusb-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyusb-users