Python PyUsb条形码扫描仪
我正试图使用Ubuntu中的Honeywell USB 3310g扫描仪从条形码或qrcode输出字符串。我有libusb和一个名为metro-usb()的库,它们使我的扫描仪能够工作。如果我打开gedit并扫描,我成功地获得了qrcode数据!我需要能够以实用的方式构建它,并将数据发送到web服务 我已经下载并安装了pyusb,这似乎是我研究中最好的解决方案。我有一个基于我在网上找到的东西的脚本,它等待扫描并成功地给我扫描结果。但是,我不确定如何读取数据。我得到了一系列毫无意义的数据。我也不熟悉USB编程或Python。有人能告诉我从USB条形码扫描中获取数据字符串的最佳方法吗?我很有可能知道剩下的。另外,当我试图设置_配置时,我总是要注释掉一个错误,以使程序运行。我总是收到一个错误,说资源正忙…不确定这是否与此有关..我已从web上获取此脚本,并从lsusb命令获取产品ID和供应商ID。当我用注释掉的set_配置运行此程序时,我得到:Python PyUsb条形码扫描仪,python,ubuntu,pyusb,Python,Ubuntu,Pyusb,我正试图使用Ubuntu中的Honeywell USB 3310g扫描仪从条形码或qrcode输出字符串。我有libusb和一个名为metro-usb()的库,它们使我的扫描仪能够工作。如果我打开gedit并扫描,我成功地获得了qrcode数据!我需要能够以实用的方式构建它,并将数据发送到web服务 我已经下载并安装了pyusb,这似乎是我研究中最好的解决方案。我有一个基于我在网上找到的东西的脚本,它等待扫描并成功地给我扫描结果。但是,我不确定如何读取数据。我得到了一系列毫无意义的数据。我也不熟
Waiting to read...
扫描时:
0x0 0x0 0xb 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x17 0x0 0x0 0x0 0x0 0x0
Traceback (most recent call last):
File "reader.py", line 81, in <module>
print lecture_code_barre()
File "reader.py", line 76, in lecture_code_barre
lecture+=NO_SCAN_CODE[data[n+2]]
KeyError: 11
0x0 0x0 0xb 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x17 0x0 0x0 0x0 0x0 0x0 0x0 0x0
回溯(最近一次呼叫最后一次):
文件“reader.py”,第81行,在
打印讲座代码
文件“reader.py”,第76行,在课程代码中
讲座+=无扫描代码[数据[n+2]]
关键错误:11
以下是py脚本:
import sys
import usb.core
import usb.util
def lecture_code_barre():
VENDOR_ID = 0x0c2e
PRODUCT_ID = 0x0b61
#VENDOR_ID = 0x04b4
#PRODUCT_ID = 0x0100
DATA_SIZE = 16 # 224
NO_SCAN_CODE = {0x1E:'1', 0x1F:'2', 0x20:'3', 0x21:'4', 0x22:'5', 0x23:'6', 0x24:'7'
, 0x25:'8', 0x26:'9', 0x27:'0', 0x28:''} # 28=enter
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): # On détache le périphérique du kernel, plus d'envoi sur stdin
try:
device.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detatch kernel driver: %s" % str(e))
#try:
# device.set_configuration()
# device.reset()
#except usb.core.USBError as e:
# sys.exit("Could not set configuration: %s" % str(e))
endpoint = device[0][(0,0)][0]
data = []
lu = False
print "Waiting to read..."
lecture=''
while 1:
try:
data += device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
if not lu:
print "Waiting to read..."
lu = True
except usb.core.USBError as e:
if e.args == (110,'Operation timed out') and lu:
if len(data) < DATA_SIZE:
print "Lecture incorrecte, recommencez. (%d bytes)" % len(data)
print "Data: %s" % ''.join(map(hex, data))
data = []
lu = False
continue
else:
for n in range(0,len(data),16):
print ' '.join(map(hex,data[n:n+16]))
lecture+=NO_SCAN_CODE[data[n+2]]
break # Code lu
return lecture
if __name__ == '__main__':
print lecture_code_barre()
导入系统
导入usb.core
导入usb.util
def讲座_代码_barre():
供应商标识=0x0c2e
产品标识=0x0b61
#供应商标识=0x04b4
#产品标识=0x0100
数据大小=16#224
无扫描代码={0x1E:'1',0x1F:'2',0x20:'3',0x21:'4',0x22:'5',0x23:'6',0x24:'7'
,0x25:'8',0x26:'9',0x27:'0',0x28:''}#28=输入
device=usb.core.find(idVendor=VENDOR\u ID,idProduct=PRODUCT\u ID)
如果设备为无:
sys.exit(“找不到Id系统条形码读取器”)
如果device.is_kernel_driver_active(0):#位于内核的驱动程序上,加上标准的环境
尝试:
设备.分离内核驱动程序(0)
除了usb.core.USBError作为e:
sys.exit(“无法删除内核驱动程序:%s”%str(e))
#尝试:
#设备。设置_配置()
#设备重置()
#除了usb.core.USBError作为e:
#系统退出(“无法设置配置:%s”%str(e))
端点=设备[0][(0,0)][0]
数据=[]
lu=错误
打印“等待阅读…”
讲座=“”
而1:
尝试:
数据+=设备.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
如果不是lu:
打印“等待阅读…”
lu=真
除了usb.core.USBError作为e:
如果e.args==(110,'操作超时')和lu:
如果长度(数据)<数据大小:
打印“讲座不正确,重新开始(%d字节)”%len(数据)
打印“数据:%s”%''。连接(映射(十六进制,数据))
数据=[]
lu=错误
持续
其他:
对于范围内的n(0,len(数据),16):
打印“”。连接(映射(十六进制,数据[n:n+16]))
讲座+=无扫描代码[数据[n+2]]
中断#代码lu
复课
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印讲座代码
带循环的设备的输出:
....
print device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
...
parallels@ubuntu:~/Documents/pyusb$ sudo python scan.py
<usb.core.Device object at 0x7fa5f34df610>
array('B', [0, 0, 38, 0, 0, 0, 0, 0])
array('B', [2, 0, 0, 0, 0, 0, 0, 0])
array('B', [2, 0, 11, 0, 0, 0, 0, 0])
array('B', [2, 0, 22, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 81, 0, 0, 0, 0, 0])
array('B', [2, 0, 0, 0, 0, 0, 0, 0])
array('B', [2, 0, 24, 0, 0, 0, 0, 0])
array('B', [2, 0, 14, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 81, 0, 0, 0, 0, 0])
array('B', [0, 0, 11, 0, 0, 0, 0, 0])
array('B', [0, 0, 23, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 23, 0, 0, 0, 0, 0])
array('B', [0, 0, 19, 0, 0, 0, 0, 0])
array('B', [2, 0, 0, 0, 0, 0, 0, 0])
array('B', [2, 0, 51, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 56, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 56, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 55, 0, 0, 0, 0, 0])
array('B', [0, 0, 5, 0, 0, 0, 0, 0])
array('B', [0, 0, 21, 0, 0, 0, 0, 0])
array('B', [0, 0, 18, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 17, 0, 0, 0, 0, 0])
array('B', [0, 0, 5, 0, 0, 0, 0, 0])
array('B', [0, 0, 18, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 18, 0, 0, 0, 0, 0])
array('B', [0, 0, 14, 0, 0, 0, 0, 0])
array('B', [0, 0, 55, 0, 0, 0, 0, 0])
array('B', [0, 0, 17, 0, 0, 0, 0, 0])
array('B', [0, 0, 8, 0, 0, 0, 0, 0])
array('B', [0, 0, 23, 0, 0, 0, 0, 0])
array('B', [0, 0, 81, 0, 0, 0, 0, 0])
array('B', [0, 0, 22, 0, 0, 0, 0, 0])
array('B', [0, 0, 8, 0, 0, 0, 0, 0])
array('B', [0, 0, 21, 0, 0, 0, 0, 0])
array('B', [0, 0, 25, 0, 0, 0, 0, 0])
array('B', [0, 0, 12, 0, 0, 0, 0, 0])
array('B', [0, 0, 6, 0, 0, 0, 0, 0])
array('B', [0, 0, 8, 0, 0, 0, 0, 0])
。。。。
打印设备.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
...
parallels@ubuntu:~/Documents/pyusb$sudo python scan.py
数组('B',[0,0,38,0,0,0,0,0])
数组('B',[2,0,0,0,0,0,0,0])
数组('B',[2,0,11,0,0,0,0,0])
数组('B',[2,0,22,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,81,0,0,0,0,0])
数组('B',[2,0,0,0,0,0,0,0])
数组('B',[2,0,24,0,0,0,0,0])
数组('B',[2,0,14,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,81,0,0,0,0,0])
数组('B',[0,0,11,0,0,0,0,0])
数组('B',[0,0,23,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,23,0,0,0,0,0])
数组('B',[0,0,19,0,0,0,0,0])
数组('B',[2,0,0,0,0,0,0,0])
数组('B',[2,0,51,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,56,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,56,0,0,0,0,0])
数组('B',[0,0,26,0,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,26,0,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,26,0,0,0,0,0,0])
数组('B',[0,0,55,0,0,0,0,0,0])
数组('B',[0,0,5,0,0,0,0,0])
数组('B',[0,0,21,0,0,0,0,0])
数组('B',[0,0,18,0,0,0,0,0])
数组('B',[0,0,26,0,0,0,0,0,0])
数组('B',[0,0,17,0,0,0,0,0])
数组('B',[0,0,5,0,0,0,0,0])
数组('B',[0,0,18,0,0,0,0,0])
数组('B',[0,0,0,0,0,0,0,0])
数组('B',[0,0,18,0,0,0,0,0])
数组('B',[0,0,14,0,0,0,0,0,0])
数组('B',[0,0,55,0,0,0,0,0,0])
数组('B',[0,0,17,0,0,0,0,0])
数组('B',[0,0,8,0,0,0,0,0])
数组('B',[0,0,23,0,0,0,0,0])
数组('B',[0,0,81,0,0,0,0,0])
数组('B',[0,0,22,0,0,0,0,0])
数组('B',[0,0,8,0,0,0,0,0])
数组('B',[0,0,21,0,0,0,0,0])
数组('B',[0,0,25,0,0,0,0,0])
数组('B',[0,0,12,0,0,0,0,0])
数组('B',[0,0,6,0,0,0,0,0])
数组('B',[0,0,8,0,0,0,0,0])
您收到的错误来自此行:
lecture+=NO_SCAN_CODE[data[n+2]]
和数据[n+2]=11
,这相当于执行以下操作
NO_SCAN_CODE[11]
NO\u SCAN\u code
是只包含键值的字典[30、31、32、33、34、35、36、37、38、39、40]
,而不是11
您收到此错误的事实意味着您收到了一个usb.core.USBError
,并且len(data)>=data\u SIZE
如果我调试这个,我会添加更多的打印语句。我建议试试这个简单的首字母
# Initialise variables
VENDOR_ID = 0x0c2e
PRODUCT_ID = 0x0b61
# Set up device
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
print device
# Do more setup things here
# detach_kernel_driver and set_configuration ?
# Perhaps try these in combination with reading the usb.core documentation, and see what happens?
# Loop 10 times (to start with - try more later?)
for i in range(10):
# Don't catch any errors, just print what the device is returning
print device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
f = open('/dev/ttyACM0')
print f.read(13)