PyUSB receives a continuous stream of data from the sensor

I have a device connected via usb and I use pyUSB to interact with the data.

This is what my code 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 on a mouse reader, but the data that I get does not make sense to me:

array('B', [80, 3])
array('B', [80, 2])
array('B', [80, 3])
array('B', [80, 2])

I assume that he reads only part of what is actually provided? I tried to install maxpacketsize more, but nothing.

+4
source share
2 answers

pyUSB sends and receives data in string format. The data you receive is ASCII codes. You need to add the following line to correctly read the data 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 a string. This should solve your problem.

+2

Python, . python script , , , . UC 64 . , . , 64 (10 ), , .

# Initialization
rxBytes = array.array('B', [0]) * (64 * 10)
rxBuffer = array.array('B')

# Get new samples
hid_dev.read(endpoint.bEndpointAddress, rxBytes)
rxBuffer.extend(rxBytes)

, .

+1

All Articles