1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#!/usr/bin/env perl
use strict;
use Device::SerialPort;
sub get_line($) {
my $port = shift;
my $ret = "";
while (1) {
my ( $count, $byte ) = $port->read(1);
if ( $count > 0 ) {
$ret .= $byte;
return $ret if $byte eq "\n";
}
else {
return $ret;
}
}
}
sub display($$$$$$) {
my ( $t, $h, $show_t, $show_h, $int, $mrtg ) = @_;
if ( $int or $mrtg ) {
$t = int( $t * 100 );
$h = int( $h * 10 );
}
if ($mrtg) {
if ($show_t) {
print "$t\n";
print "0\n";
print "long time\n";
print "temperature\n";
}
if ($show_h) {
print "$t\n";
print "0\n";
print "long time\n";
print "humidity\n";
}
}
else {
print "$t\n" if $show_t;
print "$h\n" if $show_h;
}
}
my $keypad_port = "/dev/ttyS1";
my $keypad = new Device::SerialPort($keypad_port)
|| die "can't open $keypad_port\n";
$keypad->baudrate(115200);
$keypad->parity("none");
$keypad->databits(8);
$keypad->stopbits(1);
$keypad->stty_icanon(0);
$keypad->read_char_time(0);
$keypad->read_const_time(1000);
my $mrtg = 0;
my $temp = 0;
my $humid = 0;
my $int = 0;
for my $opt (@ARGV) {
$mrtg = 1 if $opt eq '-m';
$temp = 1 if $opt eq '-t';
$humid = 1 if $opt eq '-h';
$int = 1 if $opt eq '-i';
}
for ( my $tries = 0 ; $tries < 4 ; ++$tries ) {
my $line = get_line($keypad);
chomp $line;
chomp $line;
if ( $line =~ /\$SNTHD,([-.0-9]+),([-.0-9]+)/ ) {
display( $1, $2, $temp, $humid, $int, $mrtg );
exit 0;
}
}
exit 1;
|