psu.pl: generate changed data in new
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 25 Jun 2019 15:11:45 +0000 (17:11 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 2 Jul 2019 14:13:53 +0000 (16:13 +0200)
psu.pl

diff --git a/psu.pl b/psu.pl
index 0d087d7..3c2be90 100755 (executable)
--- a/psu.pl
+++ b/psu.pl
@@ -7,6 +7,8 @@ use Data::Dump qw(dump);
 my $port = '/dev/ttyUSB1';
 $port = '/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0';
 
+my $debug = $ENV{DEBUG} || 0;
+
 my $ob = new Device::SerialPort ($port)
                  || die "Can't open $port: $!\n";
 
@@ -15,7 +17,7 @@ $ob->parity("none")   || die "fail setting parity";
 $ob->databits(8)       || die "fail setting databits";
 $ob->stopbits(1)       || die "fail setting stopbits";
 $ob->handshake("none") || die "fail setting handshake";
-$ob->debug(1);
+$ob->debug($debug);
 
 my $InBytes = 255;
 
@@ -32,15 +34,21 @@ my $s;
 
 while ( 1 ) {
        my ($count_in, $string_in) = $ob->read($InBytes);
-       print "<< $count_in << ",dump($string_in),"\n" if $count_in > 0;
+       print "<< $count_in << ",dump($string_in),"\n" if $debug && $count_in > 0;
        $got .= $string_in;
        if ( $got =~ m/(OK|E!)\r\n/ ) {
-               warn "# got = ",dump( $got );
+               warn "# got = ",dump( $got ) if $debug;
+               my $new;
                foreach ( split(/[\r\n]+/,$got) ) {
                        my ($k,$v) = split(/:\s+/,$_,2);
-                       $s->{$k} = $v;
+                       if ( ! defined $v ) {
+                               warn "SKIP: [$_]\n" if $debug;
+                       } else {
+                               $new->{$k} = $v if ! defined $s->{$k} || $s->{$k} ne $v;
+                               $s->{$k} = $v;
+                       }
                }
-               warn "# s = ",dump( $s );
+               warn "# new = ",dump( $new );
                $got = '';
                if ( @commands ) {
                        $ob->write( shift(@commands) . "\n" );