Guest User

PyUSB 1.0.0a1 issue (vs. 1.0.0a0)

a guest
Jan 3rd, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import usb.core
  4. import usb.util
  5.  
  6. def get_device(vendor, product):
  7.     device = (vendor<<16)+product
  8.     dev = usb.core.find(idVendor=vendor, idProduct=product)
  9.     if not dev:
  10.         raise ValueError('Device not found')
  11.     for configuration in dev:
  12.         # we need to detach any kernel driver from the device
  13.         # be greedy: reclaim all device interfaces from the kernel
  14.         for interface in configuration:
  15.             ifnum = interface.bInterfaceNumber
  16.             if not dev.is_kernel_driver_active(ifnum):
  17.                 continue
  18.             try:
  19.                 dev.detach_kernel_driver(ifnum)
  20.             except usb.core.USBError, e:
  21.                 pass
  22.     dev.set_configuration()
  23.     return dev
  24.  
  25. def open_device(vendor=0x403, product=0x6011, interface=1):
  26.     DEV_OUT_REQTYPE = (usb.util.CTRL_TYPE_VENDOR << 5) | \
  27.                        usb.util.CTRL_RECIPIENT_DEVICE | \
  28.                        usb.util.CTRL_OUT
  29.     SIO_RESET = 0 # Reset the port
  30.     SIO_RESET_SIO = 0
  31.     TIMEOUT = 5000
  32.     usb_dev = get_device(vendor, product)
  33.     usb_dev.ctrl_transfer(DEV_OUT_REQTYPE,
  34.                           SIO_RESET,
  35.                           SIO_RESET_SIO,
  36.                           interface, '', TIMEOUT)
  37.     return usb_dev
  38.  
  39. def main():
  40.     print open_device()
  41.  
  42. if __name__ == '__main__':
  43.     # PYTHONPATH=. pyusb100a0/bin/python snippet.py
  44.     # PYTHONPATH=. pyusb100a1/bin/python snippet.py
  45.     main()
Advertisement
Add Comment
Please, Sign In to add comment