python-3.x - python 3,尝试从多个 HID 输入读取,Raspberry Pi

标签 python-3.x input usb hid keylogger

我有一个条形码扫描仪连接到我的 RasPi,没有任何 tty,这意味着没有显示器的 headless 。换句话说,数字输入的键盘记录器。该扫描仪可读取 GTIN 或 EAN 等数字条形码。它有效,脚本在启动时由 sh 启动。我使用的脚本如下所示:

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”),这两个设备中的每一个都可以单独使用上面的脚本。

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

这是我的方法,对我来说似乎合乎逻辑,但行不通。没有错误消息,它只是什么也不做。我做错了什么?

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

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

感谢您的帮助。

最佳答案

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

<强> http://python-evdev.readthedocs.io/en/latest/tutorial.html (对于多个设备,请参阅从多个设备读取事件)

https://khanhicetea.com/post/read_input_from_usb_keyboard_in_linux/是将 evdev 与条形码扫描仪结合使用的代码

关于python-3.x - python 3,尝试从多个 HID 输入读取,Raspberry Pi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227502/

相关文章:

Python shutil.which 不适用于 wsl.exe

javascript - 屏蔽输入插件

java - JNI 代码在 Windows XP 中工作正常,但在 Windows 7 中崩溃

c# - 支持来自不同供应商的多个工业相机?

c++ - WinApi 从 USB 端口获取可用字节数?

python - 如何在提供日期和月份的情况下计算星期几

python-3.x - 无法用 map 替换 pandas 数据框列中的值

python-3.x - Python 中 CPU 使用情况的拆分结果

java - 完全使用 Java 重定向 CMD.exe 进程的输入/输出/错误流

Python 字符串,默认编码和解码(UTF-8?)