How to get data from a barcode reader using raw data mode and pyUSB?

I need to read barcode data using a USB barcode reader (raw data mode). I already know that I can configure the reader in keyboard mode, but this just does not meet my requirement, because I will use 4 readers at a time, and the text will overlap.

I am new to python and I tried to research it myself to no avail. I got this idea through the documentation, and I really don't know what is wrong with it.

Here is an example of the code I still found:

import sys import usb.core import usb.util # got these using the command lsusb -vv VENDOR_ID = 0x4b4 PRODUCT_ID = 0x100 DATA_SIZE = 1 device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID) if device is None: sys.exit("Could not find Id System Barcode Reader.") if device.is_kernel_driver_active(0): try: device.detach_kernel_driver(0) except usb.core.USBError as e: sys.exit("Could not detatch kernel driver: %s" % str(e)) #not really sure if these are correct configuration. try: cfg = device.get_active_configuration() for i in cfg: for x in i: x = x device.set_configuration() except usb.core.USBError as e: sys.exit("Could not set configuration: %s" % str(e)) data = [] swiped = False #i can't print the data when i try to read a barcode data = device.read(x.bEndpointAddress, x.wMaxPacketSize, 0, 10000) print data 

After running this and checking the barcode, I get this error.

 Traceback (most recent call last): File "barcodesensor.py", line 37, in <module> data = device.read(x.bEndpointAddress, x.wMaxPacketSize, 0, 10000) File "/usr/local/lib/python2.6/dist-packages/usb/core.py", line 654, in read self.__get_timeout(timeout) File "/usr/local/lib/python2.6/dist-packages/usb/backend/libusb10.py", line 559, in intr_read timeout) File "/usr/local/lib/python2.6/dist-packages/usb/backend/libusb10.py", line 641, in __read timeout)) File "/usr/local/lib/python2.6/dist-packages/usb/backend/libusb10.py", line 403, in _check raise USBError(_str_error[ret], ret, _libusb_errno[ret]) usb.core.USBError: [Errno 110] Operation timed out. 

I am ready to sacrifice through PayPal to anyone who can help me in obtaining raw data and converting the format to a string. Thanks in advance.

EDIT: How to get the correct data from a barcode and convert it to a readable string format?

+4
source share
3 answers

Retrieving text data from a USB payload is vendor-specific. Therefore, you will need to deal with it yourself, although it is not too difficult.

As for the exception you are getting, try turning on debug logging, run your script again and post the content to pyusb.log .

To enable debugging, set a couple of environment variables as described in the What's wrong section . tutorial .

For Linux / Mac:

 $ export PYUSB_DEBUG_LEVEL=debug $ export PYUSB_LOG_FILENAME=pyusb.log 

For Windows:

 > set PYUSB_DEBUG_LEVEL=debug > set PYUSB_LOG_FILENAME=pyusb.log 
0
source

The reason you cannot get debugging is because the environment variable PYUSB_DEBUG is not PYUSB_DEBUG_LEVEL.

Try the following:

 export PYUSB_DEBUG=debug export PYUSB_LOG_FILENAME=pyusb.log python yourscript.py nano pyusb.log 
0
source

You can try installing pyusb from git mentioned below

https://github.com/walac/pyusb#installing-pyusb-on-gnulinux-systems

The above program worked simply by decreasing the arguments in device.read ()

replace data = device.read (x.bEndpointAddress, x.wMaxPacketSize, 0, 1000) with data = device.read (x.bEndpointAddress, x.wMaxPacketSize)

0
source

All Articles