#!/usr/bin/perl -w
#
# hp3456a.pl
# version 0.9 --  6 September 2005
#
# Record voltage readings from HP 3456A DMM.
# 
# Copyright 2004-2005 by John R. Ackermann  N8UR (jra@febo.com)
#
# This program may be copied, modified, distributed and used for 
# any legal purpose provided that (a) the copyright notice above as well
# as these terms are retained on all copies; (b) any modifications that 
# correct bugs or errors, or increase the program's functionality, are 
# sent via email to the author at the address above; and (c) such 
# modifications are made subject to these license terms.

use strict;
use POSIX qw(setsid);
use Getopt::Std;
use Time::HiRes qw(usleep);
use LinuxGpib;
use DateTime;
use n8ur qw(trim squash collapse round);
use n8ur_gpib qw(checkSRQ serviceSRQ logline);

##########################
my $board = "gpib1";
my $device = "hp3456a";
##########################

#----------
# handle signals and errors -- mainly to clear lockfile on termination
sub sig_handler {
	sleep 2;
	close LOG;
	exit(0);
}

# 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';
#----------

#----------
# display usage
my $opt_string = 'h:a:f:t:s:';

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

usage: $0 [-h] [-a samples to average] -f logfile

-a      : samples to average -- default 1
-h      : this (help) message
-t	: timetag format (mjd|iso|none); default mjd
-f      : logfile using full pathname;
          for output to console, use "-f -"

EOF
}

#----------

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

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

# set variables to command line params

my $samples = 1;
if ($opt{a}) {
        $samples = $opt{a};
        }

my $sm = "";
if ($opt{s}) {
        $sm = $opt{s};
        }

my $tags = "mjd";
if ($opt{t}) {
        $tags = lc($opt{t});
        }

my $logfile = $opt{f};

#----------
# initialize variables
my $sum;
my $tmp;
my $command;
my $gpib_status;

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


#----------
# initialize devices
my $brd = LinuxGpib::ibfind($board);
my $dev = LinuxGpib::ibfind($device);

# clear
LinuxGpib::ibclr($dev);
usleep(1000000);

# intialize 3456A:

# DCV, autorange off, range 100V, autozero off, internal trigger 
$command = "F1 R5 Z0 T1";
LinuxGpib::ibwrt($dev,$command,length($command));
usleep(50000);
# filter off, display 5 digits, integrate 1 PLC
$command = "FL0 5STG 1STI";
LinuxGpib::ibwrt($dev,$command,length($command));
#usleep(50000);
# trigger hold, set SRQ mask
#$command = $sm;
#LinuxGpib::ibwrt($dev,$command,length($command));
#usleep(500000);

#----------

# write header line
my $dt = DateTime->now;
my $current_iso = $dt->ymd('-') . 'T' . $dt->hms(':');
my $current_mjd = $dt->mjd;
printf LOG "# Run started at %s (MJD %11.6F).\n",$current_iso,$current_mjd;
print LOG "# Voltage from $device.  Average by $samples.\n";

#----------

my $status;
my $device_status;
my $result = 0;

$device_status = 0x00;
$status = 0x8000;

# main loop
while (1) {
#	LinuxGpib::ibtrg($dev);
	usleep(100);
	$status = LinuxGpib::ibrd($dev,$result,14);
	$result = substr($result,1,11);
	print LOG $result,"\n";
}	

#----------

exit 0;
