b0e630d5a7b6e8ab072d8858469caaeccf7dfda8
[vrDialog] / dialog.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6 use Device::SerialPort qw(:STAT);
7
8 my $port = Device::SerialPort->new('/dev/ttyUSB0');
9 $port->baudrate(9600);
10 $port->databits(8);
11 $port->parity('none');
12 $port->stopbits(1);
13 $port->handshake('none');
14
15 $port->read_char_time(500);
16 $port->read_const_time(1000);
17
18 sub as_hex {
19         my @out;
20         foreach my $str ( @_ ) {
21                 my $hex = uc unpack( 'H*', $str );
22                 $hex =~ s/(..)/$1 /g if length( $str ) > 2;
23                 $hex =~ s/\s+$//;
24                 push @out, $hex;
25         }
26         return join(' | ', @out);
27 }
28
29 sub hex2bytes {
30         my $bytes;
31         $bytes .= pack('C', hex('0x' . $_)) foreach split(/\s+/,join(' ',@_));
32         return $bytes;
33 }
34
35 sub v {
36         my ($hex,$expect,$desc) = @_;
37         my $bytes = hex2bytes( $hex );
38         warn "# $desc\n";
39         warn ">> ",as_hex( $bytes );
40         $port->write( $bytes );
41
42         my $data = $port->read(1);
43         warn "<< len: ",ord($data);
44         $data .= $port->read(ord($data) - 1);
45         warn "<< ",as_hex($data);
46         warn "?? $expect\n" if $expect;
47
48 }
49
50 #v '07 02 00 00 00 04 C4', "hardware version";
51
52 #v '07 0A 00 00 00 04 44', 'software version';
53
54 v '07 00 01 00 06 0E EA', 'temperatures';
55