PyUsb USB条码扫描器(PyUsb USB Barcode Scanner)

编程入门 行业动态 更新时间:2024-10-24 15:19:47
PyUsb USB条码扫描器(PyUsb USB Barcode Scanner)

我正在尝试使用Ubuntu中的Honeywell USB 3310g扫描仪从条形码或qrcode输出字符串。 我有libusb和一个名为metro-usb( http://gitorious.org/other/metro-usb )的库,它使我的扫描仪能够工作。 如果我打开gedit并扫描,我成功地获得了qrcode数据! 我需要能够以实用的方式构建它并将数据发送到Web服务。

我已经下载并且实际上是pyusb,这似乎是我研究中最好的解决方案。 我有一个脚本基于我在网上找到的等待扫描的东西,并成功地给我一个扫描结果。 但是,我不确定如何读取数据。 我得到了一组没有任何意义的数据。 我也不熟悉USB编程或Python。 任何人都可以指导我从USB条形码扫描中获取数据串的最佳方法吗? 我最有可能找出其余部分。 此外,我收到一个错误,我总是要注释,以便在我尝试set_configuration时运行该程序。 我总是得到一个错误,说资源很繁忙...不确定这是否与它有任何关系..我已经从网上获得了这个脚本,以及我的lsusb命令中的产品ID和供应商ID。 当我在注释掉set_configuration的情况下运行时,我得到:

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

这是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()

具有循环的设备的输出:

.... 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])

I'm trying to output a string from a barcode or qrcode using a Honeywell USB 3310g scanner in Ubuntu. I have libusb and a library called metro-usb (http://gitorious.org/other/metro-usb) which are enabling my scanner to work. If i open gedit and scan, I successfully get the qrcode data fine! I need to be able to build this pragmatically and send the data to a web service.

I've downloaded and instally pyusb and this seems to be the best solution best on my research. I have a script based on things i've found on the web that waits for the scan and successfully gives me a result on the scan even. however, I am unsure how to read the data. I get an array of data that doesn't really make any sense. I'm not familiar with USB programming or Python either. Can anyone direct me to the best way to simply get the string of data from the USB barcode scan? I can most-likely figure out the rest. Also, I am getting an error that I always have to comment out to get the program to run when I try to set_configuration. I always get a Error saying Resource is Busy...not sure if this has anything to do with it..I've gotten this script off the web, and the Product ID and Vendor ID from my lsusb command. When i run this with the set_configuration commented out, I get:

Waiting to read...

When scanned:

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

Here is the py script:

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()

Output from device with looping:

.... 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])

最满意答案

获取快速入门指南 ,扫描“USB Serial”模式条形码,然后“保存”条形码以使此设置永久化。 现在您的3310g处于serail仿真模式,请注意新的/ dev / ttyACM0或/ dev / ttyUSB0设备。 使用python中的简单文件操作读取串行端口:

f = open('/dev/ttyACM0') print f.read(13)

Grab a Quick Start Guide, scan "USB Serial"-mode barcode, then "Save" barcode to make this setting permanent. Now your 3310g is in serail emulation mode, note new /dev/ttyACM0 or /dev/ttyUSB0 device. Read serial port with simple file operations from python:

f = open('/dev/ttyACM0') print f.read(13)

更多推荐

本文发布于:2023-08-07 22:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条码扫描器   USB   PyUsb   Barcode   Scanner

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!