#!/usr/bin/perl -w
#
# 5370-freq.pl
# version 0.6 -- 21 December 2003
#
# Record frequency readings from HP 5370b counter
# 
# Copyright 2003 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;

#----------
# 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:g:n:f:';

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

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

-a	: samples to average -- default 1
-g	: gate time (GT1, GT2, GT3, GT4)
-h	: this (help) message
-n	: nominal frequency; readings will be normalized against this
-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;
my $gate;
my $nominal;
my $logfile;

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

$gate = "GT4";
if ($opt{g}) {
	$gate = $opt{g}
	}

$nominal = 0;
if ($opt{n}) {
	$nominal = $opt{n};
	}

$logfile = $opt{f};

#----------

# device name from /etc/gpib.conf
my $device = "counter";

#----------
# initialize variables
my $reading;
my $freq;
my $bytecount;
my $counter;
my $badcount;
my $sum;
my $command;
my $gpib_status;

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

#----------
# initialize counter
my $dev = LinuxGpib::ibfind($device);
# clear
LinuxGpib::ibclr($dev);
# set for frequency
$command = "FN3" . $gate . "MD2";
LinuxGpib::ibwrt($dev,$command,length($command));
usleep(500);
#----------
# main loop
$counter = 1;
$sum = 0;
$badcount = 0;

while (1) {
	$command = "MR";
        LinuxGpib::ibwrt($dev,$command,length($command));
        usleep(500);

	# wait until SRQ 
        $gpib_status = LinuxGpib::ibwait($dev,(0x800 | 0x4000));
                if ($gpib_status & 0x4000) { ;}
        # get reading
        LinuxGpib::ibrd($dev,$reading,22);
        usleep(500);

	if (substr($reading,0,3) eq "FRQ") {
		$sum += substr($reading,5,17); 
		if ($counter == $samples) {
			# print routine
			$freq = $sum/$counter;
			if ($nominal != 0) {
				$freq = ($freq - $nominal) * (1/$nominal);
				}
			my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
		  	= gmtime;
			printf LOG 
		  	"%4.4u-%2.2u-%2.2uT%2.2u:%2.2u:%2.2u %s\n",
		  	$year+1900,$mon+1,$mday,$hour,$min,$sec,$freq;
			$counter = 1;
			$sum = 0;
		} else {
			$counter++;
		}
	}
}	
#----------

exit 0;
