我需要使用USB条形码读取器(原始数据模式)读取条形码数据。我已经知道我可以用键盘模式设置阅读器,但它不符合我的要求,因为我将同时使用4个阅读器并且文本会重叠。如何使用原始数据模式和pyUSB从条形码读取器获取数据?
我是新来的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捐赠给任何能够帮助我获得原始数据并将格式转换为字符串的人。提前致谢。
编辑:如何从条形码获取正确的数据并将其转换为可读的字符串格式?
基本上你的问题是什么? – 2012-08-16 00:42:42
如何从条形码中获取正确的数据并将其转换为可读的字符串格式? – 2012-08-16 00:50:04
请不要用贝宝标记你的问题,除非他们关心使用贝宝API或类似的东西。 – 2012-08-16 01:00:36