#!/usr/bin/perl -w
#
# prologix.pl

use strict;
use vars qw( $OS_win $ob $port );
use POSIX qw(setsid);
use Getopt::Std;
use Device::SerialPort;
use Time::HiRes qw(usleep);

#----------
# some variables that need to be declared
my $quiet = "1";
my $count = 0;
my $lockfile;
my $reading;
my $temp1;
my $temp2;
my $tmp1 = 0;
my $tmp2 = 0;
my $bat;
my $ob;

#----------
# handle signals and errors -- mainly to clear lockfile on termination
sub sig_handler {
	if (-e $lockfile) {unlink $lockfile;}
	close LOG;
}

# there's got to be a better way!
$SIG{'HUP'} = 'sig_handler';
$SIG{'INT'} = 'sig_handler';
$SIG{'KILL'} = 'sig_handler';
$SIG{'STOP'} = 'sig_handler';
$SIG{'TERM'} = 'sig_handler';
#----------
sub write_gpib {
	my $cmd = shift;
	$ob->write($cmd . "\r\n");
	usleep 1000;
	}

sub read_gpib {
	my $gotit = "";
	$ob->are_match("\n");
	$ob->lookclear;
	until ("" ne $gotit) {
      		$gotit = $ob->lookfor;
     		die "Aborted without match\n" unless (defined $gotit);
      		usleep 10000;
		}
	return $gotit;
	}

sub set_address {
	my $address = shift;
	write_gpib("++addr " . $address);
	usleep 1000;
	write_gpib("++addr");
	my $result = read_gpib();
	if ($result <> $address) die "Couldn't set address\n";
	}

#----------
# display usage
my $opt_string = 'dh:f:p:';

sub usage() {
print STDERR << "EOF";

usage: $0 [-adh] -f logfile -i log_interval -p serial_port
-d	: run as daemon
-h	: this (help) message
-f	: logfile using full pathname;
	  for output to console, use "-f -"
-p	: serial port ("ttyS1")

EOF
}

#----------
# daemonize
sub daemonize {
	chdir '/' or die "Can't chdir to /: $!";
	umask 0;
	open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
	open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
	open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
	defined(my $pid = fork) or die "Can't fork: $!";
	exit if $pid;
	setsid or die "Can't start a new session: $!";
}
#----------

getopts( "$opt_string", \my %opt ) or usage() and exit;

# print usage
usage() and exit if $opt{h};

# run as daemon?
&daemonize if $opt{d};

# set variables to command line params
my $portbase = $opt{p};
my $logfile = $opt{f};

#----------
# set up serial port
my $port = "/dev/" . $portbase;
$lockfile = "/var/lock/LCK.." . $portbase;
$ob = Device::SerialPort->new ($port,$quiet,$lockfile);
die "Can't open serial port $port!\n" unless ($ob);

$ob->baudrate(115200)	|| die "fail setting baud rate";
$ob->parity("none")	|| die "fail setting parity";
$ob->databits(8)	|| die "fail setting data bits";
$ob->stopbits(1)	|| die "fail setting stop bits";
$ob->write_settings || die "no settings";
usleep 100;

#----------
# set up logfile
open (LOG, ">>$logfile") ||
	die "Can't open logfile $logfile!\n";
# set nonbuffered mode
select(LOG), $| = 1;

#----------

my $cmd;
my $command;
my $eol = "\r\n";
my $gotit;

$ob->are_match("\n");    # possible end strings
$ob->lookclear;                  # empty buffers

$cmd = "++auto 1";
$command = $cmd . $eol;
$ob->write($command);
usleep 1000;

$cmd = "++addr 2";
$command = $cmd . $eol;
$ob->write($command);
usleep 1000;

$cmd = "++addr";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "address = %s\n", $gotit;                # input BEFORE the match

$cmd = "M2S4";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "offset = %s\n", $gotit;                # input BEFORE the match

$cmd = "M0S4";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "phase = %s\n", $gotit;                # input BEFORE the match

$cmd = "S22S4";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "noise = %s\n", $gotit;                # input BEFORE the match

$cmd = "S23S4";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "gain = %s\n", $gotit;                # input BEFORE the match

$cmd = "S24S4";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "time constant = %s\n", $gotit;                # input BEFORE the match

$cmd = "S26S4";
$command = $cmd . $eol;
$ob->write($command);

$gotit = "";
until ("" ne $gotit) {
      $gotit = $ob->lookfor;	# poll until data ready
      die "Aborted without match\n" unless (defined $gotit);
      usleep 10000;				# polling sample time
  }
printf "status bits = %s\n", $gotit;                # input BEFORE the match





$ob->close if (defined($ob));
