5 use Data::Dump qw(dump);
6 use Device::SerialPort qw(:STAT);
8 my $debug = $ENV{DEBUG} || 0;
10 my $port = Device::SerialPort->new('/dev/ttyUSB0');
11 $port->baudrate(9600);
13 $port->parity('none');
15 $port->handshake('none');
17 $port->read_char_time(500);
18 $port->read_const_time(1000);
22 foreach my $str ( @_ ) {
23 my $hex = uc unpack( 'H*', $str );
24 $hex =~ s/(..)/$1 /g if length( $str ) > 2;
28 return join(' | ', @out);
33 $bytes .= pack('C', hex('0x' . $_)) foreach split(/\s+/,join(' ',@_));
40 for my $i ( 0 .. length($data) - 2 ) {
42 $crc = ( $crc << 1 ) ^ 25;
47 my $byte = ord(substr($data,$i,1));
49 # warn "## ", as_hex(chr($byte)), " crc = ", $crc, " ", as_hex(chr($crc));
51 # ignore checksum errors for FF, we will calulate them!
52 if ( chr($crc) ne substr($data,-1,1) && substr($data,-1,1) ne "\xFF" ) {
53 warn "CRC error for ",as_hex($data), " calulated ", as_hex(chr($crc));
56 return substr($data,0,-1) . chr($crc);
60 my ($bytes,$desc) = @_;
63 warn ">> ",as_hex( $bytes );
65 $port->write( $bytes );
67 my $data = $port->read(1);
68 warn "<< len: ",ord($data) if $debug;
70 goto retry if ord($data) == 0;
72 $data .= $port->read(ord($data) - 1);
74 warn "<< ",as_hex($data);
77 while ( $o < length($data) - 2 ) {
78 my $n = unpack('n', substr($data,$o,2));
79 $n = -( 0xffff - $n ) if $n & 0x8000;
80 warn "## $o = ",$n, "\t",$n / 16,"\n";
89 v_bytes( hex2bytes( $hex ), $desc );
93 v '07 02 00 00 00 04 C4', "hardware version";
95 v '07 0A 00 00 00 04 44', 'software version';
97 v '07 00 01 00 06 0E EA', 'temperatures';
99 v '07 00 00 00 26 0A A6', 'last errros';
102 #v '07 00 00 00 26 00 AC', 'last errros (with invalid length)';
104 foreach my $reg ( @ARGV ) {
105 warn "# send user $reg\n";
107 crc( hex2bytes( "07 00 00 00 $reg 01 FF" ) )