#!/usr/bin/perl

# script for logging knock_cyl_t and realtime variables
# usage: log_knock.pl logfile [serial port]
#    ex: log_knock.pl capture.log /dev/tty0

use Device::SerialPort;

if (@ARGV < 1) {
    print "Usage: log_knock.pl logfile [serial port]\n";
    exit;
}

$LOGFILE = $ARGV[0];
$PORT = $ARGV[1];

if ($PORT eq "") {
    $PORT = "/dev/ttyUSB0";
}

$ob = Device::SerialPort->new ($PORT) || die "Can't open $PORT: $!";
$ob->baudrate(9600)   || 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";

open(LOG,">${LOGFILE}")
    ||die "Can't open $LOGFILE: $!\n";

open(DEV, "<$PORT") 
    || die "Can't open $PORT: $_";

select(LOG), $| = 1; # set nonbuffered mode

$pass=$ob->write("byeAManmde84byeAman");

print "Starting logging...\n";

while($_ = <DEV>){
    print LOG $_;
}

undef $ob;
