I am using PyUSB in Python as I will have to listen an USB port to retrieve data from an electronic card. For the moment, I have to train myself by reading direct input from a small keyboard (USB-connected) connected to a Raspberry-Pi. Of course, I do not want to read the typed String, I expect to get ASCII codes for example. I just don't get how I could read input from my USB-keyboard.
I already found some snippets :
import usb.core
import usb.util
VENDOR_ID = 0x0922
PRODUCT_ID = 0x8003
# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
idProduct=PRODUCT_ID)
# use the first/default configuration
device.set_configuration()
# first endpoint
endpoint = device[0][(0,0)][0]
# read a data packet
attempts = 10
data = None
while data is None and attempts > 0:
try:
data = device.read(endpoint.bEndpointAddress,
endpoint.wMaxPacketSize)
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
attempts -= 1
continue
print data
Either I get the error 16 "Device is busy" or nothing at all if I uncomment the following line "device.set_configuration()" which causes the "Device is busy" exception... (I did replace VENDOR_ID and PRODUCT_ID with my keyboard's ids)
is_kernel_driver_active
anddetach_kernel_driver
methods of the usb device class. The libusb documentation discusses this a bit. – Grane