Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import usb.core
- import usb.util
- def get_device(vendor, product):
- device = (vendor<<16)+product
- dev = usb.core.find(idVendor=vendor, idProduct=product)
- if not dev:
- raise ValueError('Device not found')
- for configuration in dev:
- # we need to detach any kernel driver from the device
- # be greedy: reclaim all device interfaces from the kernel
- for interface in configuration:
- ifnum = interface.bInterfaceNumber
- if not dev.is_kernel_driver_active(ifnum):
- continue
- try:
- dev.detach_kernel_driver(ifnum)
- except usb.core.USBError, e:
- pass
- dev.set_configuration()
- return dev
- def open_device(vendor=0x403, product=0x6011, interface=1):
- DEV_OUT_REQTYPE = (usb.util.CTRL_TYPE_VENDOR << 5) | \
- usb.util.CTRL_RECIPIENT_DEVICE | \
- usb.util.CTRL_OUT
- SIO_RESET = 0 # Reset the port
- SIO_RESET_SIO = 0
- TIMEOUT = 5000
- usb_dev = get_device(vendor, product)
- usb_dev.ctrl_transfer(DEV_OUT_REQTYPE,
- SIO_RESET,
- SIO_RESET_SIO,
- interface, '', TIMEOUT)
- return usb_dev
- def main():
- print open_device()
- if __name__ == '__main__':
- # PYTHONPATH=. pyusb100a0/bin/python snippet.py
- # PYTHONPATH=. pyusb100a1/bin/python snippet.py
- main()
Advertisement
Add Comment
Please, Sign In to add comment