python读取usb扫码数据_PyUsbUSB条码扫描
我下载并安装了pyusb,这似乎是我研究中最好的解决⽅案。我有⼀个脚本,基于我在⽹上到的东西,等待扫描,并成功地给了我⼀个扫描结果甚⾄。但是,我不确定如何读取数据。我得到了⼀系列毫⽆意义的数据。我也不熟悉USB编程或Python。有谁能告诉我从USB条码扫描中获取数据串的最佳⽅法吗?我很有可能把剩下的都弄清楚。另外,我收到⼀个错误,当我试图设置配置时,我总是要注释掉这个错误才能让程序运⾏。我总是会收到⼀个错误,说资源正忙…不确定这是否与此有关..我已经从web上获取了此脚本,以及lsusb命令中的产品ID 和供应商ID。当我在设置配置被注释掉的情况下运⾏此程序时,我得到:Waiting
扫描时: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
print lecture_code_barre()
File "reader.py", line 76, in lecture_code_barre
lecture+=NO_SCAN_CODE[data[n+2]]
KeyError: 11
这是py脚本:import sys
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 = find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
if device is None:
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)
USBError as e:
#try:
# device.set_configuration()
#USBError as e:
# it("Could not set configuration: %s" % str(e))
endpoint = device[0][(0,0)][0]
data = []
lu = False
print "Waiting "
lecture=''
while 1:
try:
data += ad(endpoint.bEndpointAddress, endpoint.wMaxPacketSize) if not lu:
print "Waiting "
lu = True
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]))
python怎么读取串口数据lecture+=NO_SCAN_CODE[data[n+2]]
break # Code lu
return lecture
if __name__ == '__main__':
print lecture_code_barre()
从具有循环的设备输出:....
ad(endpoint.bEndpointAddress, endpoint.wMaxPacketSize) ...
parallels@ubuntu:~/Documents/pyusb$ sudo python scan.py
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]) arra
y('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, 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])

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。