381d1e4439aa7ca7556f714e5749bdd458432477
[vrDialog] / dialog.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6 my $debug = $ENV{DEBUG} || 0;
7
8 =for serial_port
9
10 use Device::SerialPort qw(:STAT);
11
12 my $port = Device::SerialPort->new('/dev/ttyUSB0');
13 die "can't open serial port" unless $port;
14 $port->baudrate(9600);
15 $port->databits(8);
16 $port->parity('none');
17 $port->stopbits(1);
18 $port->handshake('none');
19
20 $port->read_char_time(500);
21 $port->read_const_time(1000);
22
23 sub read_serial {
24         my $len = shift;
25         return $port->read( $len );
26 }
27
28 sub write_serial {
29         my $bytes = shift;
30         return $port->write( $bytes );
31 }
32
33 =cut
34
35 # TODO: implement remote serial port
36 # remserial -x 2 -p 23000 -s "9600 raw" /dev/ttyUSB0
37
38 use IO::Socket::INET;
39
40 my $sock = IO::Socket::INET->new(
41         PeerAddr => '192.168.1.148',
42         PeerPort => '23000',
43         Proto    => 'tcp'
44 );
45 die "can't connect" unless $sock;
46
47 sub read_serial {
48         my $len = shift;
49         my $buffer;
50         $sock->read( $buffer, $len );
51         warn "## read $len ",as_hex($buffer) if $debug;
52         return $buffer;
53 }
54
55 sub write_serial {
56         my $bytes = shift;
57         return $sock->print( $bytes );
58 }
59
60
61 sub as_hex {
62         my @out;
63         foreach my $str ( @_ ) {
64                 my $hex = uc unpack( 'H*', $str );
65                 $hex =~ s/(..)/$1 /g if length( $str ) > 2;
66                 $hex =~ s/\s+$//;
67                 push @out, $hex;
68         }
69         return join(' | ', @out);
70 }
71
72 sub hex2bytes {
73         my $bytes;
74         $bytes .= pack('C', hex('0x' . $_)) foreach split(/\s+/,join(' ',@_));
75         return $bytes;
76 }
77
78 sub crc {
79         my $data = shift;
80         my $crc = 0;
81         for my $i ( 0 .. length($data) - 2 ) {
82                 if ( $crc & 0x80 ) {
83                         $crc = ( $crc << 1 ) ^ 25;
84                 } else {
85                         $crc = $crc << 1;
86                 }
87                 $crc = $crc & 0xff;
88                 my $byte = ord(substr($data,$i,1));
89                 $crc = $crc ^ $byte;
90 #               warn "## ", as_hex(chr($byte)), " crc = ", $crc, " ", as_hex(chr($crc));
91         }
92         # ignore checksum errors for FF, we will calulate them!
93         if ( chr($crc) ne substr($data,-1,1) && substr($data,-1,1) ne "\xFF" ) {
94                 warn "CRC error for ",as_hex($data), " calulated ", as_hex(chr($crc));
95         }
96
97         return substr($data,0,-1) . chr($crc);
98 }
99
100 sub v_bytes {
101         my ($bytes,$desc) = @_;
102         warn "# $desc\n";
103 retry:
104         warn ">> ",as_hex( $bytes );
105         crc( $bytes );
106         write_serial( $bytes );
107
108         my $data = read_serial(1);
109         warn "<< len: ",ord($data) if $debug;
110
111         goto retry if ord($data) == 0;
112
113         $data .= read_serial(ord($data) - 1);
114         crc($data);
115         warn "<< ",as_hex($data);
116
117         my $o = 2;
118         while ( $o < length($data) - 2 ) {
119                 my $n = unpack('n', substr($data,$o,2));
120                 $n = -( 0xffff - $n ) if $n & 0x8000;
121                 warn "## $o = ",$n, "\t",$n / 16,"\n";
122                 $o += 2;
123         }
124
125         while ( $o < length($data) - 1 ) {
126                 my $n = unpack('c', substr($data,$o,1));
127                 warn "## $o = ", $n, "\n";
128                 $o += 1;
129         }
130
131         return $data;
132 }
133
134 sub v {
135         my ($hex,$desc) = @_;
136         v_bytes( hex2bytes( $hex ), $desc );
137 }
138
139 if ( ! @ARGV ) {
140         v '07 02 00 00 00 04 C4', "hardware version";
141
142         v '07 0A 00 00 00 04 44', 'software version';
143
144         v '07 00 01 00 06 0E EA', 'temperatures';
145
146         v '07 00 00 00 26 0A A6', 'last errros';
147 }
148
149 #v '07 00 00 00 26 00 AC', 'last errros (with invalid length)';
150
151 foreach my $reg ( @ARGV ) {
152         warn "# send user $reg\n";
153         v_bytes(
154                 crc( hex2bytes( "07 00 00 00 $reg 01 FF" ) )
155         ,"user: $reg");
156 }