如何在Raspberry Pi 4中将USB条形码扫描仪输入到python中 [英] How get the usb barcode scanner input into python in Raspberry pi 4

查看:118
本文介绍了如何在Raspberry Pi 4中将USB条形码扫描仪输入到python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取python中的USB条形码读取器输入,所以我使用了这段代码

I am trying the get a usb barcode reader input in python so I used this code

#thanks to https://stackoverflow.com/questions/19732978/how-can-i-get-a-string-from-hid-device-in-python-with-evdev
#thanks to https://raspberrypi.stackexchange.com/questions/82418/how-to-route-barcode-tty-input-to-python

from evdev import InputDevice, categorize, ecodes  

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', 57: u' ', 100: u'RALT'
}

capscodes = {
    0: None, 1: u'ESC', 2: u'!', 3: u'@', 4: u'#', 5: u'$', 6: u'%', 7: u'^', 8: u'&', 9: u'*',
    10: u'(', 11: u')', 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',  57: u' ', 100: u'RALT'
}

def readBarcode(devicePath):

    dev = InputDevice(devicePath)
    dev.grab() # grab provides exclusive access to the device

    x = ''
    caps = False

    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            data = categorize(event)  # Save the event temporarily to introspect it
            if data.scancode == 42:
                if data.keystate == 1:
                    caps = True
                if data.keystate == 0:
                    caps = False

            if data.keystate == 1:  # Down events only
                if caps:
                    key_lookup = u'{}'.format(capscodes.get(data.scancode)) or u'UNKNOWN:[{}]'.format(data.scancode)  # Lookup or return UNKNOWN:XX
                else:
                    key_lookup = u'{}'.format(scancodes.get(data.scancode)) or u'UNKNOWN:[{}]'.format(data.scancode)  # Lookup or return UNKNOWN:XX


                if (data.scancode != 42) and (data.scancode != 28):
                    x += key_lookup

                if(data.scancode == 28):
                    return(x)

readBarcode("/home/pi/.local/lib/python3.7/site-packages/evdev/device.py")

我收到此错误

回溯(最近通话最近): init 中的文件"/home/pi/.local/lib/python3.7/site-packages/evdev/device.py",第125行fd = os.open(dev,os.O_RDWR | os.O_NONBLOCK)FileNotFoundError:[错误2]没有这样的文件或目录:'/dev/input/ev'

Traceback (most recent call last):   File "/home/pi/.local/lib/python3.7/site-packages/evdev/device.py", line 125, in init     fd = os.open(dev, os.O_RDWR | os.O_NONBLOCK) FileNotFoundError: [Errno 2] No such file or directory: '/dev/input/ev'

是什么导致此错误,我该如何解决?

What is causing this error and how can I fix it?

推荐答案

devicePath应该是RPi上设备(即条形码扫描仪)的路径.看来您已经将其设置为一个名为device.py的文件.尝试按照说明此处以获得可用设备的列表,然后使用该设备路径作为devicePath.

devicePath should be a path to the device (i.e. barcode scanner) on your RPi. It seems that you have set this to some file called device.py. Try following the instructions Here to get a list of available devices then use that device path as devicePath.

如果查看我引用的页面上的示例,您会发现devicePath应该看起来像/dev/input/event1之类.不是文件路径.

If you look at the examples on the page I referenced, you'll see that a devicePath should look like /dev/input/event1 or something. Not a file path.

这篇关于如何在Raspberry Pi 4中将USB条形码扫描仪输入到python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆