Python evdev 和条形码扫描仪



我有以下代码,用于捕获条形码扫描仪的输入。问题在于我想将整个扫描的代码保存在数据库中,但代码的每个字符都打印在新行上:

#!/usr/bin/env python
from evdev import InputDevice, ecodes, list_devices
from select import select
keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
dev = InputDevice("/dev/input/event10")
barcode = ""
while True:
    r,w,x = select([dev], [], [])
    for event in dev.read():
        if event.type == 1 and event.value == 1:
             barcode += (keys[event.code])
    print barcode
# sudo ./bar-code-test.py                                                                           
#0
#00
#00
#000
#001
#1001
#0017
#70017
#00170
#000170
#001708
#8001708
#0017085
#50017085
#00170853
#300170853
#001708531
#1001708531
#0017085316
#60017085316
#00170853160
#000170853160
#001708531601
#1001708531601
#0017085316013
#30017085316013
#00170853160131
#100170853160131
#00170853160131X
#00170853160131X

我是python的新手,所以任何帮助将不胜感激。

找到了一个解决方案。条码扫描器每次扫描码后发送42个扫码(回车键),所以我们需要检查扫码:

#!/usr/bin/env python
from evdev import InputDevice, ecodes, list_devices, categorize
import signal, sys
scancodes = {
    # Scancode: ASCIICode
    0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
    10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
    20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
    30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
    40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
    50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}
barCodeDeviceString = "Metrologic Metrologic Scanner"
devices = map(InputDevice, list_devices())
for device in devices:
    if device.name == barCodeDeviceString:
        dev = InputDevice(device.fn)
def signal_handler(signal, frame):
    print 'Stopping'
    dev.ungrab()
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
dev.grab()
barcode = ""
for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        data = categorize(event)
        if data.keystate == 1 and data.scancode != 42: # Catch only keydown, and not Enter
            if data.scancode == 28:
                print barcode
                barcode = ""
            else:
                barcode += keys[data.scancode]

如果您读取字符串的长度,直到它达到您想要的值或长度,您退出循环并打印出读取的条形码怎么办?

我像这样修改了代码,它只打印了一行:

#!/usr/bin/env python
from evdev import InputDevice, ecodes, list_devices
from select import select
keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
dev = InputDevice("/dev/input/event1")
barcode = ""
while True:
    r,w,x = select([dev], [], [])
    for event in dev.read():
        if event.type == 1 and event.value == 1:
             barcode += (keys[event.code])
    if (len (barcode)) > 13:
    break;

print barcode

我在Arduino Yun中用OpenWrt尝试了它,它工作得很好:

root@Arduino:/mnt/sda1# python pybar.py
7501483187071X

相关内容

  • 没有找到相关文章

最新更新