Skip to content

Instantly share code, notes, and snippets.

@lbrutti
Created February 15, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrutti/eec99a977b8c7c2eb39bccd831267b37 to your computer and use it in GitHub Desktop.
Save lbrutti/eec99a977b8c7c2eb39bccd831267b37 to your computer and use it in GitHub Desktop.
Perl script for fetching data from Campbell scientific CR200X data logger
#!/usr/bin/perl
# Requirements: Device::SerialPort 0.12 (from cpan)
#
# Description: This perl script is for logging of data from a serial
# port, to a specified logfile. The logfile can then be parsed with
# other programs for reporting purposes.
#
# EXAMPLE:
# perl -w serlog.pl test .log /dev/ttyS0 9600
#
#
use Device::SerialPort 0.12;
$LOGPREFIX = shift; # data file prefix
$LOGSUFFIX = shift; # data file suffix
$PORT = shift; # port to watch
$BAUD = shift; # baud rate
#intervallo di log: recupera gli ultimi 6 record,
#corrispondenti all'ultima ora di logging
$RECORD = 0;
#
#
# Serial Settings
#
#
print STDERR "Serial device: $PORT\n";
print STDERR "Baud rate $BAUD\n";
print STDERR "Parity: none, bits: 8, handshake: none\n";
$ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$ob->baudrate($BAUD) || die "failed setting baudrate";
$ob->parity("none") || die "failed setting parity";
$ob->databits(8) || die "failed setting databits";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings || die "no settings";
$ob->dtr_active(Yes);
$ob->rts_active(Yes);
$ob->read_char_time(0); # don't wait for each character
$ob->read_const_time(900); # 0.9 second per unfulfilled "read" call
#
## Send a string to the port
#
#
##put the CR200x in terminal mode
#
$ob->write("\r\r\r\r\r");
sleep 1;
#$ob->read(1000);
#sleep 1;
#
##loop to fetch last six table entries
#
while ($RECORD < 6) {
$ob->lookclear;
$ob->write("\r\r\r");
$ob->write("6\r");
sleep 1;
$RECORD ++;
}
$ob->lookclear;
#
# open the logfile, and Port
open(DEV, "+<$PORT") || die "Cannot open $PORT: $_";
#
# Loop forver, logging data to the log file
#
#decommentare while per ripristinare
$UNDONE = 1;
while ($UNDONE){ # print input device to file
if (! defined $LOGFILE) {
$DATE = `date +%F_%H.%M.%S`;
chomp $DATE;
$LOGFILE = $LOGPREFIX . $DATE . $LOGSUFFIX; # file name to output to
open(LOG,">>${LOGFILE}") ||die "can't open log file $LOGFILE for append: $!\n";
select(LOG), $| = 1; # set nonbufferd mode
select(STDOUT), $| = 1; # set nonbufferd mode
print STDERR "\nLogging to $LOGFILE\n\n";
}
my $line = <DEV>;
if (defined $line) {
print LOG $line;
} else {
my $currDate = `date +%F`;
chomp $currDate;
if ($DATE !~ "$currDate") {
print STDERR "Rotating log file: $LOGFILE\nprev date: $DATE new date: $currDate\n";
undef $LOGFILE ;
#close LOG;
}
$UNDONE=0;
}
}
print "LOOKCLEAR INVOKED\n";
$ob->lookclear || print "CLEAR FAILED";
print "chiudo\n";
$ob->close || print "CAN'T CLOSE PORT" ;
undef $ob;
#Read more: http://aplawrence.com/BGarlock/logger.html#ixzz0wGdW009e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment