#!/usr/bin/perl
#
# hp3561a.pl
# version 0.91 -- 3 September 2005
#
# Plot display of HP 3561A Spectrum Analyzer
# 
# Copyright 2003-5 by John R. Ackermann  N8UR (jra@febo.com)
# Licensed under the GPL version 2 or later; see the file COPYING
# included with this distribution.  I request, but do not require, that
# any modifications that correct bugs or errors, or increase the program's
# functionality, be sent via email to the author at the address above.

use strict;
use POSIX qw(setsid);
use Getopt::Std;
use Time::HiRes qw(usleep time gettimeofday);
use LinuxGpib;
use Number::Format;
use n8ur qw(trim collapse squash round);
use n8ur_gpib qw(checkSRQ serviceSRQ);

my $j;
my @trace;
my $tracedata;
my $command;
my $time_now;
my $gpib_status;
my $lines;
my $srq;

my $buf = bless( [], "AVPtr" );


my $board = "gpib0";
my $device = "hp3561a";

#----------
# usage and option initialization
my $opt_string = 'hf:';
sub usage() {
print STDERR << "EOF";

usage: $0 [-h] -f filename

-h	: this (help) message

-f	: filename for output PNG

EOF
}

#----------------------
getopts( "$opt_string", \my %opt ) or usage() and exit;
usage() and exit if $opt{h};
usage() and exit if !$opt{f};

# set up datafile
my $datafile = $opt{f};
open (DATA, ">$datafile") ||
	die "Can't open image file $datafile!\n";

# initialize instrument
my $brd = LinuxGpib::ibfind($board) ||
	die "Can't open device $device!\n";
my $dev = LinuxGpib::ibfind($device) ||
	die "Can't open device $device!\n";

# get trace data - normal mode
print "Getting trace...\n";
$command = "dstb";
LinuxGpib::ibwrt($dev,$command,length($command));
usleep 50000;
LinuxGpib::ibrdi($dev,$buf,1028);

print @{$buf};

close DATA;
exit 0;
