#!/bin/bash # not maemo specific. # # usage: u1233 READ? # This script takes commands like SYST:BATT? or FETC? as parameter # and fetches the result from KeySight U1233A via U1173A USB/IR adapter (ttyUSB0) # self=`basename $0` copyleft="(C) 2015-01-24 J.Reiseweber, GPL-V2" version="0.3" USAGE="Usage: $self [[option...] -c [option...]] cmd... # run a series of cmds in a time schedule, format and display results $self cmd # send single cmd to DMM and display result on stdout $self -h # show this helptext options: -i : \$interval in seconds -c : \$count, continuous sampling for samples, 0=infinite -n : \$print_n - print count index left to result (starts at 1) -N \"\" : \$del0 - separator/delimiter after count index, defaults to value of-D -d \"\" : \$dateformat of timestamp right to result, as in (1)date +format -D \"\" : \$del - separator/delimiter in between results and between last result and timestamp, defaults to \"; \" -S \"\" : \$starttime - of the virtual sample with index0. Default: EPOC + N x , most recent -2 \"\" : secondary command(s) to execute immediately after all primary cmds but print result to stderr. May occcur multi -E \"\" : \$errtext to output in case of read error, instead of . When empty, aborts on error -h : help $ denotes an ENV var $self will use when provided. $self -D \"/\" == del=/ $self. Options override ENV vars \n $self Version: $version. $copyleft " #echo -e "$USAGE"; exit #set -e -u # user config IRdrv=/sys/bus/usb-serial/drivers/pl2303; timeout=0.5 parseoptions(){ OPTIONS="s:a:n:vIiqc:" if [ $# -eq 0 ] # Script invoked with no command-line args? then echo -e $USAGE exit $ERROR_OPT fi speed=;accel=;steps=;init=n while getopts "$OPTIONS" Option do case $Option in s ) speed="$OPTARG";; a ) accel="$OPTARG";; n ) steps="$OPTARG";; v ) VERBOSE=true;; i ) init="i";; I ) init="I";; q | c ) break;; * ) echo "unknown option: $OPTARG" echo $USAGE exit $ERROR_OPT;; esac done shift $(($OPTIND - 1)) camopts=$@ #ToDo: checking for valid set of options and option value types and limits, for luxury version } init(){ self=`basename $0` # find device based on name of kernel driver for IR adapter dev=/dev/$(basename ${IRdrv}/ttyUSB*) if [ "$dev" == "/dev/ttyUSB*" ]; then echo -e "${self}: Searching for '${IRdrv}/ttyUSB*'\n${self}: IR adapter not found! ABORTING..." exit 5 fi #why exec? cruft for the simple version, needed to keep filehandle open when we add loops etc exec 4>$dev 5<$dev stty 1:0:cbd:0:3:1c:7f:15:4:5:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0 <&5 #prolly cruft, and didn't help flush the buffer when some other process keeps the file handle open (exec >/dev/bla) read -t 0 void <&5 } ident(){ DMMID=`docmd "*IDN?"`; case $DMMID in Agilent\ Technologies\,U1233A*) #echo "$DMMID"; return; ;; '') echo "$self: DMM off? *IDN returns \"$DMMID\"" return 4; ;; *) echo "$self: Wrong DMM? *IDN returns \"$DMMID\"" return 3; ;; esac; } docmd() { (read -t $timeout result <&5 && echo -n "${result: +0:-1}")& echo "$@" >&4 wait } ################## main ##################### main(){ # TODO: proper parameter parsing and sanitizing init # TODO: fix this lazy botch if [ $# -ne 1 ] ; then # the simple approach like: # while echo "`docmd "$1"`; `date +%H:%M:%S`"; do sleep 1; done; # obviously creates a loop of 1 + x seconds duration (x for runtime of cmds) and thus skips a second every now and then, so we do a lil fancy here ident||exit $?; : ${interval:=1}; : ${count:=0}; : ${starttime:=$(( `date +%s` / interval * interval ))}; # we date back starttime to last event in interval schedule assuming running from epoch : ${del:=; } : ${del0:=$del} : ${dateformat:=%H:%M:%S.%N} for (( nsdelta=0, n=1; n<=count || count==0; n++ )); do # calculate sleep time til next exact point in schedule while :; do nsdelta=$(( ((starttime + n * interval) * 1000000000) + 1000000000000000000 - `date +%s%N` )); # +1000000000000000000 for leading zeroes, 2 less than max bash allows # read unsolicited responses during waiting - instead of the former 'sleep "${nsdelta: +0:-9}.${nsdelta: -9}";' if read -t "${nsdelta: +1:-9}.${nsdelta: -9}" unsolresult <&5; then echo "unsolicited: ${unsolresult: +0:-1}" >&2 # TODO: handle unsol resp in a smarter way than that else break fi done [ -v print_n ] && echo -n "${n}${del0}" for cmd in $@; do echo -n "`docmd "$cmd"`${del}" done date "+${dateformat}" done ##### single command else docmd "$1"; echo fi; } main "$@"