我需要使用 USB 条码阅读器(原始数据模式)读取条码数据。我已经知道我可以使用键盘模式设置阅读器,但它不符合我的要求,因为我将同时使用 4 个阅读器并且文本会重叠。
我是 python 新手,我尝试自己研究它无济于事。我通过文档得到了这些想法,我真的不知道它有什么问题。
这是我到目前为止提出的示例代码:
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
运行此程序并尝试条形码后,我收到此错误。
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.
我愿意通过 PayPal 向任何可以帮助我获取原始数据并将格式转换为字符串的人捐款。提前致谢。
编辑:如何从条形码中获取正确的数据并将其转换为可读的字符串格式?