在 Linux 初始化脚本中使用 STDIN

发布于 2024-12-01 09:21:06 字数 432 浏览 2 评论 0原文

我目前正在开发一个需要读取键盘输入的嵌入式系统(实际上 - 它需要读取键盘模拟的条形码扫描仪)。

我使用 SheevaPlug 和 Debian Squeeze 作为硬件部分,并且使用一个非常基本的 init 脚本来启动负责执行业务逻辑的 python 程序。

正是该脚本需要读取键盘输入。

我们已经开发了基本上是全功能计算机的开发版本,带有屏幕和其他东西,它工作得相当不错 - 但它并不是真的可以接受 - 但在这里我并没有真正理解出了什么问题(尽管我承认我有点预期如此)。


所以我想知道当 init 启动它时如何以某种方式将键盘连接到我的脚本的 STDIN。

或者,如果有人知道一个 python 库,它可以让我完全绕过问题并直接从 input/eventX 读取,我会很感兴趣。



提前致谢

I'm currently working on an embedded system that needs to read keyboard input (Actually - it needs to read a keyboard-emulated barcode scanner).

I'm using a SheevaPlug with Debian Squeeze for the hardware part, and I'm using a pretty basic init script that fires a python program that's in charge of doing the business logic.

It's that script that needs to read keyboard input.

We've worked on development versions that were basically full featured computers, with a screen and stuff, and it worked pretty OK - but it's not really acceptable -, but here I don't really grasp what's wrong (Although I admit that I kinda expected it).

So I'd like to know how it would be possible to somehow connect the keyboard to my script's STDIN when init starts it.

Alternatively, I'd be interested if someone knows a python library that would allow me to bypass the problem entirely and read directly from input/eventX.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-12-08 09:21:06

尝试这个(但要小心,可以更改 /dev/input/event0 来代替重新插入多个 USB HID 设备):

import struct

inputDevice = "/dev/input/event0" #keyboard on my system
inputEventFormat = 'iihhi'
inputEventSize = 16

file = open(inputDevice, "rb") # standard binary file input
event = file.read(inputEventSize)
while event:
  (time1, time2, type, code, value) = struct.unpack(inputEventFormat, event)
  print type,code,value
  event = file.read(inputEventSize)
file.close()


        def getUSBHIDs(self):
    s=getExecOutput('cat /proc/bus/input/devices')
    events=[]
    for i in range(len(s)):
        m=re.search('^.*Handlers=kbd.*event(?P<name>[0-9]+).*
,s[i])
        if m:
            events+=['/dev/event'+m.group('name')]
    return events

try this (but be carefull /dev/input/event0 can be changed in place of repluging multiple USB HID-devices):

import struct

inputDevice = "/dev/input/event0" #keyboard on my system
inputEventFormat = 'iihhi'
inputEventSize = 16

file = open(inputDevice, "rb") # standard binary file input
event = file.read(inputEventSize)
while event:
  (time1, time2, type, code, value) = struct.unpack(inputEventFormat, event)
  print type,code,value
  event = file.read(inputEventSize)
file.close()


        def getUSBHIDs(self):
    s=getExecOutput('cat /proc/bus/input/devices')
    events=[]
    for i in range(len(s)):
        m=re.search('^.*Handlers=kbd.*event(?P<name>[0-9]+).*
,s[i])
        if m:
            events+=['/dev/event'+m.group('name')]
    return events
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文