X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=dialog.pl;h=0308a80c2aa195fe6d61a3e21f84bc9c8c9301fe;hb=b0ee4211e860bd53dd9c709d4f93cb5b01e2ae8e;hp=8f50b5ee7b0c45f4f34b2ab388b1971e465a8626;hpb=b2a928bba7f65a157ed44f700c5a5a2a0f7af15c;p=vrDialog diff --git a/dialog.pl b/dialog.pl index 8f50b5e..0308a80 100755 --- 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); @@ -12,24 +14,71 @@ $port->parity('none'); $port->stopbits(1); $port->handshake('none'); -# disable timeouts $port->read_char_time(500); $port->read_const_time(1000); +sub as_hex { + my @out; + foreach my $str ( @_ ) { + my $hex = uc unpack( 'H*', $str ); + $hex =~ s/(..)/$1 /g if length( $str ) > 2; + $hex =~ s/\s+$//; + push @out, $hex; + } + return join(' | ', @out); +} + +sub hex2bytes { + my $bytes; + $bytes .= pack('C', hex('0x' . $_)) foreach split(/\s+/,join(' ',@_)); + 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; - $bytes .= pack('C', hex('0x' . $_)) foreach split(/\s+/,$hex); + my $bytes = hex2bytes( $hex ); warn "# $desc\n"; - warn ">> $hex ",dump( $bytes ); +retry: + warn ">> ",as_hex( $bytes ); + crc( $bytes ); $port->write( $bytes ); - my $len = $port->read(1); - warn "<< len: ",ord($len); - my $data = $port->read(ord($len) - 1); - warn "<< ",dump($data); + my $data = $port->read(1); + warn "<< len: ",ord($data) if $debug; + + goto retry if ord($data) == 0; + + $data .= $port->read(ord($data) - 1); + crc($data); + warn "<< ",as_hex($data); } v '07 02 00 00 00 04 C4', "hardware version"; +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)';