pythonlinuxevdev

How to (reliably) read USB barcode scanner in embedded (headless) Linux?


I need to interface generic USB scanners (not a single, defined, model) with an embedded Linux device (mips32/MT7620 running Linux 3.18, if it matters).

All scanners operate in "keyboard emulation mode" and, sure enough, if plugged into a desktop Linux send their data directly as keyboard input to console.

This does not happen on device (i.e.: I do not see any char unto the serial debug console, which makes sense as it misses all X input subsystem).

All scanners present themselves as Input device on /dev/input/event0.

My current attempt is using python evdev; code is quite straightforward:

import asyncio
import evdev

edev = evdev.InputDevice('/dev/input/event0')
async for event in edev.async_read_loop():
    if event.type == evdev.ecodes.EV_KEY and event.value == 1:
        handle_event(event)

Note: this is just an extracted snippet; I can post a runnable example, if needed.

This is essentially this answer (I'm using asyncio, can that be a problem?) and it seems to work, but actually loses events.

If barcode scanner sends events back-to back I seem to start losing events quite early (after about 16 events <= 8 chars!).

If I can insert between-char delay (~1ms is enough) then everything works as expected, but that's not an option on many scanners.

What am I missing?


Solution

  • For whoever is in the same predicament: I was unable to find a Pure Python working solution.

    I resorted to a very simple "C" program (most of it are tables):

    #include <stdio.h>
    #include <fcntl.h>
    #include <linux/input.h>
    #include <unistd.h>
    #include <signal.h>
    #include <stdlib.h>
    
    void INThandler() {
        exit(0);
    }
    
    char ttab[] = {
         0,  27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',  /* Backspace */
      '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\n',        /* Enter key */
         0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',   0,        /* Left shift */
      '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',   0,                  /* Right shift */
      '*',
        0,  /* Alt */
      ' ',  /* Space bar */
        0,  /* Caps lock */
        0,  /* 59 - F1 key ... > */
        0,   0,   0,   0,   0,   0,   0,   0,
        0,  /* < ... F10 */
        0,  /* 69 - Num lock*/
        0,  /* Scroll Lock */
        0,  /* Home key */
        0,  /* Up Arrow */
        0,  /* Page Up */
      '-',
        0,  /* Left Arrow */
        0,
        0,  /* Right Arrow */
      '+',
        0,  /* 79 - End key*/
        0,  /* Down Arrow */
        0,  /* Page Down */
        0,  /* Insert Key */
        0,  /* Delete Key */
        0,   0,   0,
        0,  /* F11 Key */
        0,  /* F12 Key */
        0,  /* All other keys are undefined */
    };
    
    
    char ntab[] = {
        0,  27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',   /* Backspace */
     '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\n',         /* Enter key */
        0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',   0,         /* Left shift */
     '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',   0,                   /* Right shift */
      '*',
        0,  /* Alt */
      ' ',  /* Space bar */
        0,  /* Caps lock */
        0,  /* 59 - F1 key ... > */
        0,   0,   0,   0,   0,   0,   0,   0,
        0,  /* < ... F10 */
        0,  /* 69 - Num lock*/
        0,  /* Scroll Lock */
        0,  /* Home key */
        0,  /* Up Arrow */
        0,  /* Page Up */
      '-',
        0,  /* Left Arrow */
        0,
        0,  /* Right Arrow */
      '+',
        0,  /* 79 - End key*/
        0,  /* Down Arrow */
        0,  /* Page Down */
        0,  /* Insert Key */
        0,  /* Delete Key */
        0,   0,   0,
        0,  /* F11 Key */
        0,  /* F12 Key */
        0,  /* All other keys are undefined */
    };
    
    char stab[] = {
        0,  27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0,      /* Backspace */
        0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}',   0,         /* Enter key */
        0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',   0,'\n',         /* Left shift */
        0, 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',   0,                   /* Right shift */
      '*',
        0,  /* Alt */
      ' ',  /* Space bar */
        0,  /* Caps lock */
        0,  /* 59 - F1 key ... > */
        0,   0,   0,   0,   0,   0,   0,   0,
        0,  /* < ... F10 */
        0,  /* 69 - Num lock*/
        0,  /* Scroll Lock */
        0,  /* Home key */
        0,  /* Up Arrow */
        0,  /* Page Up */
      '-',
        0,  /* Left Arrow */
        0,
        0,  /* Right Arrow */
      '+',
        0,  /* 79 - End key*/
        0,  /* Down Arrow */
        0,  /* Page Down */
        0,  /* Insert Key */
        0,  /* Delete Key */
        0,   0,   0,
        0,  /* F11 Key */
        0,  /* F12 Key */
        0,  /* All other keys are undefined */
    };
    
    int main() {
        char devname[] = "/dev/input/event0";
        int device = open(devname, O_RDONLY);
        struct input_event ev;
        int shift = 0;
        char line[4096], *p = line;
    
        signal(SIGINT, INThandler);
        fputs("starting\n", stdout);
        //fputs("starting\n", stderr);
        while (1) {
            read(device, &ev, sizeof(ev));
            if (ev.type == 1) {
                if (ev.code == 42)
                    shift = ev.value;
                else if (ev.value) {
                    //printf("Key: %i State: %i\n", ev.code, ev.value);
                    char *t = shift? stab: ntab;
                    char ch = t[ev.code];
                    //printf("Key: %02d State: %d [%c]\n", ev.code, ev.value, ch);
                    if (ch == '\n') {
                        *p = '\0';
                        fputs(line, stdout); fputc('\n', stdout); fflush(stdout);
                        //fputs(line, stderr); fputc('\n', stderr); fflush(stderr);
                        p = line;
                    } else
                        *p++ = ch;
                }
            }
        }
    }
    

    ... driven from Python:

    async def handle_events(self):
        log.debug("handle_events(%s): st start", self.dev)
        while True:
            if self.hid is None:
                log.debug("handle_events(%s): checking...", self.dev)
                self.hid = await asyncio.create_subprocess_exec(
                    '/usr/local/bin/readEvents',
                    stdout=asyncio.subprocess.PIPE
                )
                try:
                    out = await asyncio.wait_for(self.hid.stdout.readline(), 10)
                except asyncio.TimeoutError:
                    log.debug("handle_events(%s): timeout on startup message", self.dev)
                    self.hid.terminate()
                    self.hid = None
                else:
                    out = out.strip()
                    if out != b'starting':
                        log.debug("handle_events(%s): bad startup message: %s", self.dev, out)
                        self.hid.terminate()
                        self.hid = None
                    else:
                        log.debug("handle_events(%s): startup msg received", self.dev)
            else:
                if self.hid.stdout.at_eof():
                    log.info("handle_events(%s): killing subprocess", self.dev)
                    rc = await self.hid.wait()
                    log.info("handle_events(%s): subprocess exited with status %s", self.dev, rc)
                    self.hid = None
                    await asyncio.sleep(10)
            if self.hid is not None:
                line = await self.hid.stdout.readline()
                if not line:
                    log.error('handle_events: readline() ERROR:')
                    self.hid.terminate()
                    self.hid = None
                else:
                    line = line.strip()
                    log.info('handle_events: received line: "%s"', line)
                    await self.process_line(line)
    

    I'm sure Python driver can be simplified. This is verbatim from my (working) application.

    HiH!