Hola, estoy trabajando con un equipo Universal Time interval Counter SR620, se debe controlar por puerto serial sr-232, me pasaron un ejemplo hecho en PERL, pero no entiendo mucho el programa, me he leido algunos tutoriales, pero aun me falta, les pido por favor me ayudan a comentar el siguiente codigo para comprenderlo mejor, de antemano muchas gracias.
#!/usr/bin/perl –w
#
#makes a time intervalo measurement with a sr620 counter
use strict;
use Geotopt
use Fcntl qw(:DEFAULT :flock);
@_ = split ‘/’, $0;
my $PROG_NAME = pop @_;
my $VERSION;
my $version;
my $help;
my $samples=2;
my $trigger_level = -99.;
my $impedance=’’;
my $verbose;
my $device=””;
my $cmd;
my $answer;
my $imp_value;
my ( $mean, $rel, $std, $min, $max );
my ( $year, $month, $day, $hour, $minu, $sec );
Getopt::Long::Configure (‘no_ignore_case_always’);
unless ( Getopt::Long::GetOptions ( ‘help’ => \$help,
‘device=s’ => \$device,
‘samples=f’ => \$samples,
‘trigger=f’ => \$trigger_level,
‘imp=s’ => \$impedance,
‘V’ => \$version,
‘v’ => \$verbose )) {
die “usage: $PROG_NAME -h (for a short help! )\n”;
}
( $help ) && help_die();
( $version ) && die “$PROG_NAME - $VERSION\n”;
# check input parameter
( $device eq “” ) and die “FATAL: define device with option –d !!\n”;
( -c $device ) or die “FATAL: device \”$device\” doesn\’t exist !!\n”;
$impedance =~ y/A-Z/a-z/;
if ( $impedance eq ‘50’ ) {
$imp_value = 0;
} elsif ( $impedance eq ‘1m’ ) {
$imp_value = 1;
} elsif ( $impedance eq ‘uhf’ ) {
$imp_value = 2;
} else {
die “FATAL: -I input impedance not 50|1M|UHF !!\n”;
}
if ( ( $trigger_level < -5. ) or ( $trigger_level > 5. ) ) {
die “FATAL: -t trigger level not in range [-5.0 .. 5.0] !!\n”;
}
if ( $samples > 5e6 ) {
die “FATAL: -s max number of samples = 5e6 !!\n”;
}
if ( $samples < 1 ) {
die “FATAL: -s min number of samples = 1 !!\n”;
}
my $chk_samples = $samples;
while ( $chk_samples >= 10 ) {
$chk_samples /= 10;
}
if ( ( $chk_samples != 1 ) and ( $chk_samples != 2 ) and ( $chk_samples != 5 ) ) {
die “FATAL: -s number of samples not in step 1,2 or 5 !!\n”;
}
# open serial device
my $timeout = ($samples+2) * 40;
if ( $timeout > 255 ) {
$timeout = 255
}
system ( “stty 19200 raw – hupcl ignbrk –onlcr –iexten –echo –echoe –echonl
-echoctl –echoke –echok min 0 time $timeout –crtscts <$device”) and die “Can\’t initialize \”$device\”!\n”;
open SR620, “+<$device” or die “Can\’t open device \”$device\”!\n”;
flock ( SR620, LOCK_EX|LOCK_NB ) || die “FATAL: Device \”$device\” already in use!\n”;
#initialize sr620 and do measurement
#$cmd = sprintf “*rst; locl 1; term 1,0; levl 1,1.2; armm 1; size %f; term 2,%d; levl 2,%f; mode 0; jttr 0”, $samples, $imp_value, $trigger_level;
do {
open FILE, “>>fg5_offset.dat” or die “FATAL: Cant open file!!”;
$cmd = sprintf “locl 1; term 1,0; levl 1,1.2; arm 1; size %f; term 2,%d; levl 2,%f;
mode 0; jttr 0”, $samples, $imp_value, $trigger_level;
( $verbose ) && print “CMD: $cmd\n”;
print SR620 “$cmd\n”;
$cmd =sprintf “autm 0; meas? 0; meas? 1; meas? 2; meas? 3”;
( $verbose ) && print “CMD: $cmd\n”;
print SR620 “$cmd\n”;
$answer = <SR620>;
$cmd = sprintf “xall?”;
( $verbose ) && print “CMD: $cmd\n”;
print SR620 “$cmd\n”;
$answer = <SR620>;
if ( ( defined $answer ) and ( $answer =~ /^\d|-/ ) ) {
( $verbose ) && print $answer;
( $mean, $rel, $std, $min, $max ) = split /,/, $answer;
if ( defined $mean ) {
($sec,$minu,$hour,$day,$month,$year) = gmtime(time);
printf “%04d.%02d.%02d %02d:%02d:%02d %.3f %.0f\n”, $year+1900, $month+1, $day, $hour, $minu, $sec, $mean*1e9, $std*1e12;
printf FILE “%04d.%02d.%02d %02d:%02d:%02d %.3f %.0f\n”, $year+1900, $month+1, $day, $hour, $minu, $sec, $mean*1e9, $std*1e12;
}
} else {
print “Timeout!!\n”;
}
$cmd = sprintf “locl 0; size 1; autm 1”;
print SR620 “$cmd\n”;
close FILE;
} while (1);
close SR620;
sub help_die {
printf STDERR “usage: $PROG_NAME\n\t-h ( for a short help )\n”;
printf STDERR “\t-d serial device\n”;
printf STDERR “\t-s number of samples\n”;
printf STDERR “\t-t trigger level of input B\n”;
printf STDERR “\t-I input impedance of input [50|1M|UHF] B\n”;
printf STDERR “\t-v verbose\n”;
die “\t-V print program version\n”;
}