added symbol hints
[vrDialog] / dialog.pl
index ee20fd1..0308a80 100755 (executable)
--- a/dialog.pl
+++ b/dialog.pl
@@ -5,6 +5,8 @@ use strict;
 use Data::Dump qw(dump);
 use Device::SerialPort qw(:STAT);
 
+my $debug = $ENV{DEBUG} || 0;
+
 my $port = Device::SerialPort->new('/dev/ttyUSB0');
 $port->baudrate(9600);
 $port->databits(8);
@@ -32,20 +34,41 @@ sub hex2bytes {
        return $bytes;
 }
 
+sub crc {
+       my $data = shift;
+       my $crc = 0;
+       for my $i ( 0 .. length($data) - 2 ) {
+               if ( $crc & 0x80 ) {
+                       $crc = ( $crc << 1 ) ^ 25;
+               } else {
+                       $crc = $crc << 1;
+               }
+               $crc = $crc & 0xff;
+               my $byte = ord(substr($data,$i,1));
+               $crc = $crc ^ $byte;
+#              warn "## ", as_hex(chr($byte)), " crc = ", $crc, " ", as_hex(chr($crc));
+       }
+       if ( chr($crc) ne substr($data,-1,1) ) {
+               warn "CRC error for ",as_hex($data), " calulated ", as_hex(chr($crc));
+       }
+}
+
 sub v {
        my ($hex,$desc) = @_;
        my $bytes = hex2bytes( $hex );
        warn "# $desc\n";
 retry:
        warn ">> ",as_hex( $bytes );
+       crc( $bytes );
        $port->write( $bytes );
 
        my $data = $port->read(1);
-       warn "<< len: ",ord($data);
+       warn "<< len: ",ord($data) if $debug;
 
        goto retry if ord($data) == 0;
 
        $data .= $port->read(ord($data) - 1);
+       crc($data);
        warn "<< ",as_hex($data);
 
 }
@@ -56,3 +79,6 @@ v '07 0A 00 00 00 04 44', 'software version';
 
 v '07 00 01 00 06 0E EA', 'temperatures';
 
+v '07 00 00 00 26 0A A6', 'last errros';
+
+v '07 00 00 00 26 00 AC', 'last errros (with invalid length)';