SOLVED: BASH script to parse data from /dev/xxx and output to command

Discussion in 'Programming/Scripts' started by toner, Feb 27, 2015.

  1. toner

    toner New Member

    I'm trying to find a way to have a bash script read data that another piece of software is sending to a /dev port (such as tty/pts/usb/serial). Specifically I'm looking to grab output from "hamlib" (which apparently only sends to certain /dev ports, NOT a txt file!) and parse the output for use with servoblaster. My script works like a charm when the input is a text file, but I've been unable to find a way to link it back to hamlib. A piped FIFO buffer would probably be great if I could make it work...

    Here's the script:
    Code:
    #!/bin/bash
    
    fil=/usr/src/testdata
    #fil=/dev/tty20
    
    function hamlibmon()
    {
    
    while read line
    do
    echo $line | grep -q EL
    if [ $? == 0 ]; then
    elpos=`echo $line | cut -d L -f2 | cut -d . -f1`
    elpos=$(echo "scale=3; $elpos*1.1+50" | bc)
    echo 0=$elpos > /dev/servoblaster
    fi
    done < $fil
    
    }
    
    while true
    do
    hamlibmon
    done
    
    fi
    
     
  2. toner

    toner New Member

    Ended up using /dev/vcs20 to read data sent to /dev/tty20:
    Code:
    #!/bin/bash
    
    #Written 2015-02-28 by VE6WK with assistance from AI6GS
    #This script assumes hamlib started with: rotctld -m 202 -r /dev/tty20
    
    azservnum=0      #Azimuth servo number for Servoblaster
    azservstart=50   #Starting position for azimuth servo, usually 50
    azservend=250    #Ending position for azimuth servo, usually 250
    elservnum=1      #Elevation servo number for Servoblaster
    elservstart=50   #Starting position for elevation servo, usually 50
    elservend=250    #Ending position for elevation servo, usually 250
    
    azfactor=$(echo "scale=1; ($azservend-$azservstart)/360" | bc)
    elfactor=$(echo "scale=1; ($elservend-$elservstart)/180" | bc)
    
    fil=/tmp/rotcmd.txt
    
    function hamlibmon()
    {
    
    tail -c 350 /dev/vcs20 > /tmp/rotcmd.txt
    sed -i -e '$a\' /tmp/rotcmd.txt
    
      while read line
      do
        echo $line | grep -q EL
        if [ $? == 0 ]; then
        azpos=`echo $line  | cut -d Z -f2 | cut -d . -f1`
        azpos=$(echo "scale=1; $azpos*$azfactor+$azservstart" | bc)
        elpos=`echo $line  | cut -d L -f2 | cut -d . -f1`
        elpos=$(echo "scale=1; $elpos*$elfactor+$elservstart" | bc)
        echo $azservnum=$azpos > /dev/servoblaster
        echo $elservnum=$elpos > /dev/servoblaster
        fi
      done < $fil
    
    }
    
    while true
    do
            hamlibmon
            sleep .5
    done
    
    fi
    
    
     
    till likes this.

Share This Page