[Tutor] Pyusb: get data via USB sent from mouse
andré palma
andrefsp at gmail.com
Sat Mar 7 16:35:05 CET 2009
Hi folks, I'm new on pyusb programming and to learn how to get data i'm
trying to get data sent from my mouse.
I've download a program called usbview( http://www.kroah.com/linux/usb/
) to display the device descriptors of any USB device pluged to my
computer.
I made a peace of code that was supposed to get the data sent from my
mouse but when i try to run it there is an error saying that the device
is busy =S
Code:
=======================================================================================
import sys
import usb
import time
import struct
import array
import math
class DeviceDescriptor(object) :
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 getDevice(self) :
busses = usb.busses()
for bus in busses:
for device in bus.devices :
if device.idVendor == self.vendor_id :
if device.idProduct == self.product_id :
return device
return None
class PlugUSBDevice(object) :
PLUG_VENDOR_ID = 0x1c4f # mouse vendor id
PLUG_PRODUCT_ID = 0x0003 # mouse product id
PLUG_INTERFACE_ID = 0 # mouse interface number
PLUG_INTERRUPT_IN_EP = 0x81L # mouse end point address
def __init__(self) :
self.device_descriptor =
DeviceDescriptor(PlugUSBDevice.PLUG_VENDOR_ID,
PlugUSBDevice.PLUG_PRODUCT_ID,
PlugUSBDevice.PLUG_INTERFACE_ID)
self.device = self.device_descriptor.getDevice()
self.handle = None
def open(self) :
self.device = self.device_descriptor.getDevice()
self.handle = self.device.open()
self.handle.claimInterface(self.device_descriptor.interface_id)
def close(self) :
self.handle.releaseInterface()
def getDataPacket(self, bytesToGet) :
return self.handle.interruptRead(PlugUSBDevice.PLUG_INTERRUPT_IN_EP,
bytesToGet, 15)
if __name__ == "__main__":
device = PlugUSBDevice()
device.open()
print device.getDataPacket(4)
The error:
************************************************************
File "readusb.py", line 61, in <module>
device.open()
File "readusb.py", line 50, in open
self.handle.claimInterface(self.device_descriptor.interface_id)
usb.USBError: could not claim interface 0: Device or resource busy
************************************************************
==================================================================================================
What am i doing wrong? =S
More information about the Tutor
mailing list