#!/usr/bin/perl
#
# util to pass command to dacs board
# > ./get_dacs.pl /dev/ttyUSB0 h
#
# may need to do "sudo apt-get install libdevice-serialport-perl"
use Device::SerialPort;

# get device dir to use
($device, $other) = @ARGV;
if($device eq "") {
  print "usage: get_dacs2.pl device ( /dev/tty??? for dac )\n";
  exit(0);
}

# do first - if port down - will exit and do nothing...
my $port = Device::SerialPort->new("$device");
$port->user_msg(ON);
$port->databits(8);
$port->baudrate(19200);
$port->parity("none");
$port->stopbits(1);
$port->write_settings;

# get start time of run
$date = `date +'%Y-%m-%d|%H:%M:%S'`;
chomp $date;
print "$date|";

# needed to clear buffer
$port->lookclear; 

# do send command - get  digitial in 
$port->write("s\n");
# get results and print it
do_print () ;

# optional command...
## do toggle of output
#$port->write("o~\n");
#do_print () ;

print "\n";
exit;

###### subs ########
sub do_print {
  while(1) {
    my $byte=$port->read(1);
    if ( $byte eq ">") {
       last;
    }
    if ( $byte eq "\r") {
       $byte=" ";
    }
    if ( $byte eq "\n") {
       $byte="";
    }
    print "$byte";
  }
}


