LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2015, 01:27 PM   #1
atcom
LQ Newbie
 
Registered: Feb 2015
Posts: 3

Rep: Reputation: Disabled
Question BASH script to parse data from /dev/xxx and output to command


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
 
Old 02-27-2015, 06:27 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
maybe you should try a pipe

Code:
$ mkfifo testpipe
$ while read -r line; do printf "%s\n" "$line"; done <testpipe &
[1] 2992
$ printf 'Line1\nLine2\n' >testpipe
$ Line1
Line2
jobs
[1]+  Done

Last edited by NevemTeve; 02-27-2015 at 06:28 AM.
 
Old 02-27-2015, 10:18 AM   #3
atcom
LQ Newbie
 
Registered: Feb 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hmm, not sure I follow that at all!

My problem is that hamlib demands a /dev/tty type argument for it's output when it's started, and I need my bash script to process that data line by line.
 
Old 02-27-2015, 10:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
I admit I thought hamlib is a set of libraries, not an executable program...
 
Old 02-28-2015, 10:59 AM   #5
atcom
LQ Newbie
 
Registered: Feb 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Finally did it this way:
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Bash script - output of command into array. systemlordanubis Programming 2 05-02-2014 11:22 PM
[SOLVED] how to parse a data file using bash script rjuny Programming 11 08-20-2012 08:09 PM
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 12:51 PM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 01:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration