开发者

Using STDIN in a Linux init script

I'm currently work开发者_如何学JAVAing 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜