Results 1 to 5 of 5

Thread: Redirect to X11 :0

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Beans
    6

    Redirect to X11 :0

    Hi all,
    I'm trying to get the input from a USB barcode scanner to be entered into a webform.

    I've tried the softwedge package, which works, but only supports 1D barcodes. I need to read in 2D barcodes; as soon as I try with softwedge, softwedge crashes.

    I wrote a (very) little shell script to spit out the scan:
    Code:
    while true; do
        cat /dev/ttyUSB1
    done
    Works great for the shell or any process I run within that tty. What I really want to do is run my little script but have it put the scan into ANY process that is in focus (like the webform field in Firefox).

    If I can't, my next thing would be to try to dig into softwedge and see if I can figure out how to make it work with 2D barcodes. Problem is, I'm not very good with C, and I know nothing about X programming.

    This is on Ubuntu 14.04.

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Redirect to X11 :0

    I guess some combination of a command-line X clipboard manipulation tool like xclip or xsel with an input automation tool like xdotool or xmacroplay may do the trick.
    Last edited by schragge; January 30th, 2015 at 12:45 PM.

  3. #3
    Join Date
    Apr 2011
    Beans
    6

    Re: Redirect to X11 :0

    xdotool is an awesome find, thanks schragge. It's kind of slow but, using xdotool I was able to turn my script into a system-wide keypress event generator for my barcode scanner. Here's my new script, if anyone is interested.

    read_ttyUSB1.sh:
    Code:
    #!/usr/bin/env bash
    stty 57600 sane -echo -echok -icrnl -ixon -icanon -opost -onlcr time 3 min 0 < /dev/ttyUSB1
    while true; do
        cat /dev/ttyUSB1 | sed -e 's/\(.\)/\1 /g' |\
                   sed s/,/comma/g | \
                   sed s/@/at/g |\
                   while read LINE; do
            LINE=`echo $LINE Return`
            xdotool key $LINE 2>/dev/null
        done
    done
    Put that in the background with "read_ttyUSB1.sh &" and it just works!

    xdotool doesn't parse all keys directly, hence the sed conversions for comma and at. I may run into other problems like that down the road but should be able to take care of them.

    Also, my scanner doesn't have a speaker so I'm planning on adding a beep from the PC speaker.

  4. #4
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Redirect to X11 :0

    You can put several sed commands into one sed call:
    Code:
    sed 's/./& /g; s/,/comma/g; s/@/at/g; s/$/Return/' /dev/ttyUSB1
    Last edited by schragge; January 30th, 2015 at 05:20 PM.

  5. #5
    Join Date
    Apr 2011
    Beans
    6

    Re: Redirect to X11 :0

    Nice, didn't know that. Streamlining is good, I'll update it with that later. In the meantime, I added a beep feature. Turns out there isn't a PC speaker on this all-in-one kiosk I'm using, but there are Alsa speakers. So here's my solution for that (Thanks for the original idea go to Ryne Everett on StackExchange):

    Code:
    #!/usr/bin/env bash
    _alarm() {
      ( \speaker-test --frequency $1 --test sine )&
      pid=$!
      \sleep 0.${2}s
      \kill -9 $pid
    }
    
    
    stty 57600 sane -echo -echok -icrnl -ixon -icanon -opost -onlcr time 3 min 0 < /dev/ttyUSB1
    while true; do
        cat /dev/ttyUSB1 | sed -e 's/\(.\)/\1 /g' |\
                   sed s/,/comma/g | \
                   sed s/@/at/g |\
                   while read LINE; do
            if [ "$LINE" != "" ]; then
                LINE=`echo $LINE Return`
                xdotool key $LINE 2> /dev/null
                if [ "`echo $LINE | grep '^at'`" != "" ]; then
                    _alarm 1200 120 &> /dev/null
                fi
            fi
        done 2> /dev/null
    done

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •