Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyUSB get a continuous stream of data from sensor

Tags:

python

usb

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.

like image 426
Stupid.Fat.Cat Avatar asked Mar 27 '15 01:03

Stupid.Fat.Cat


1 Answers

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.

like image 79
KiranCP Avatar answered Sep 23 '22 15:09

KiranCP