python 3,尝试从多个HID输入读取,Raspberry Pi [英] python 3, try to read from multiple HID inputs, Raspberry Pi

查看:314
本文介绍了python 3,尝试从多个HID输入读取,Raspberry Pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有任何tty的条形码扫描仪连接到我的RasPi,这意味着没有显示器就没有头了.换句话说,是用于数字输入的键盘记录程序.该扫描仪读取数字条形码,例如GTIN或EAN.它可以工作,该脚本由启动时的sh启动.我使用的脚本如下所示:

I have a barcode scanner connected to my RasPi without any tty, which means headless without a monitor. In other words, a keylogger for number inputs. This scanner reads numerical barcodes like GTIN or EAN. It works, the script is started by sh on boot. The script I use looks like that:

import sys

tStr = ''
while 1:
        fp = open('/dev/hidraw3', 'rb')
        buffer = fp.read(8)
        for c in buffer:
                if c:
                        if c == 40 or c == 88: # [ENTER-key]
                                function_to_handle_result (tStr)
                                tStr = ''
                        elif c == 98 or c == 39:
                                c = 0
                                tStr = tStr + str(c)
                        elif c > 29 and c < 39:
                                c = c - 29
                                tStr = tStr + str(c)
                        elif c > 88 and c < 98:
                                c = c - 88
                                tStr = tStr + str(c)

现在,我希望用户能够手动输入数字,以防条形码损坏和/或无法读取,并连接了数字键盘.如果我知道虚拟文件及其编号,例如"/dev/hidraw3",那么这两个设备中的每一个都可以单独与上面的脚本一起工作.

Now I want the user to be able to input numbers manually, in case that the barcode is damaged and/or not readable, and connected a numeric keyboard. Each of these two devices work separately with the script above, if I know the virtual file and its number, like '/dev/hidraw3'.

现在,我想组合输入以便能够在一个脚本和一个函数中访问值,并且我想猜测正确的hidraw-path.

Now I want to combine the inputs to be able to access the values in one script and one function, and I want to guess the right hidraw-path.

这是我的方法,对我来说似乎合乎逻辑,但不起作用.没有错误消息,它什么也不做.我在做什么错了?

This is my approach, which seems logical to me, but doesn't work. There is no error message, it just does nothing. What am I doing wrong ?

import sys
from pathlib import Path

t = ''

def handle_c(c):
        global t
        if c == 40 or c == 88:
                function_to_handle_result (t)
                t = ''
        elif c == 98 or c == 39:
                c = 0
                t = t + str(c)
        elif c > 29 and c < 39:
                c = c - 29
                t = t + str(c)
        elif c > 88 and c < 98:
                c = c-88
                t = t + str(c)
        return

hid = {}
f = {}
b = {}
c = {}
while 1:
        for i in range(10):
                hid[i] = '/dev/hidraw'+str(i) # guessing path
                if Path(hid[i]).exists(): # check if path exists
                        f[i] = open(hid[i], 'rb')
                        b[i] = f[i].read(8)
                        for c[i] in b[i]:
                                if c[i]:
                                        handle_c(c[i])

在较早的方法中,我没有像这里那样使用动态变量,结果相同,它什么也不做.

In earlier approaches I did not use dynamical variables like here, with the same result, it does nothing.

感谢您的帮助.

推荐答案

,您可以使用python-evdev来访问数字键盘(以及条形码扫描仪).它是linux evdev接口的python实现,它基于输入设备生成的事件,即HID(

you can use python-evdev for accessing the numeric keyboard ( and also the barcode scanner ). it is a python implementation of the linux evdev interface that is based on events generated by the input devices, i.e. HID ( https://en.wikipedia.org/wiki/Evdev )

http://python-evdev.readthedocs.io/zh_CN/latest/tutorial.html (有关多个设备的信息,请参见从多个设备读取事件)

是https://khanhicetea.com/post/read_input_from_usb_keyboard_in_linux/的使用代码使用条形码扫描仪的em> evdev

in https://khanhicetea.com/post/read_input_from_usb_keyboard_in_linux/ is code for using evdev with barcode scanner

这篇关于python 3,尝试从多个HID输入读取,Raspberry Pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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