4

I need to read a barcode data using a usb barcode reader (raw data mode). I already know that I can set the reader with the keyboard mode but it just doesn't fit my requirement because I'll be using 4 readers simultaneously and the text will overlap.

I am new to python and i've tried researching it myself to no avail. I got these idea through the documentation and I really don't know what's wrong with it.

Here's the sample code that i've come up so far:

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 trying out a 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 willing to donate via PayPal to anyone who can help me in obtaining the raw data and converting the format to string. Thanks in advance.

EDIT: How do I obtain the correct data from the barcode and convert it to the readable string format?

Angelica Lim
  • 61
  • 1
  • 6

3 Answers3

0

Extracting the textual data from the USB payload is vendor specific. So you will need to figure that out on your own, though it isn't too complicated.

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

To enable debugging set a couple of environment variables as described in the What's wrong? section of the 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
Ihor Kaharlichenko
  • 5,944
  • 1
  • 26
  • 32
0

The reason you can't get the debug is because the environment variable is PYUSB_DEBUG not PYUSB_DEBUG_LEVEL.

Try this:

export PYUSB_DEBUG=debug
export PYUSB_LOG_FILENAME=pyusb.log

python yourscript.py

nano pyusb.log
Daniel van Flymen
  • 10,931
  • 4
  • 24
  • 39
0

Can try out installing the pyusb from the git mentioned below

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

The above program worked just by reducing the arguments in the device.read()

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