I have a device that's connected through usb and I'm using pyUSB to interface with the data.
This is what my code currently looks like:
import usb.core
import usb.util
def main():
    device = usb.core.find(idVendor=0x072F, idProduct=0x2200)
    # use the first/default configuration
    device.set_configuration()
    # first endpoint
    endpoint = device[0][(0,0)][0]
    # read a data packet
    data = None
    while True:
        try:
            data = device.read(endpoint.bEndpointAddress,
                               endpoint.wMaxPacketSize)
            print data
        except usb.core.USBError as e:
            data = None
            if e.args == ('Operation timed out',):
                continue
if __name__ == '__main__':
  main()
It is based off the mouse reader, but the data that I'm getting isn't making sense to me:
array('B', [80, 3])
array('B', [80, 2])
array('B', [80, 3])
array('B', [80, 2])
My guess is that it's reading only a portion of what's actually being provided? I've tried settign the maxpacketsize to be bigger, but nothing.
pyUSB sends and receives data in string format. The data which you are receiving is ASCII codes. You need to add the following line to read the data properly in the code.
data = device.read(endpoint.bEndpointAddress,
                           endpoint.wMaxPacketSize)
RxData = ''.join([chr(x) for x in data])
print RxData
The function chr(x) converts ASCII codes to string. This should resolve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With