7 use Device::SerialPort;
9 use Mojo::IOLoop::Stream;
11 my $devname = "/dev/davis";
13 #my $ob = Serial->new ($devname, 19200) || die;
14 my $ob = Device::SerialPort->new ($devname) || die;
16 $ob->user_msg(1); # misc. warnings
17 $ob->error_msg(1); # hardware and data errors
21 #$ob->parity_enable(1); # for any parity except "none"
24 $ob->handshake('none');
25 $ob->read_const_time(15000); # ultimate timeout (15 seconds)
26 $ob->write_settings||die"setting failed";
28 $ob->read_char_time(0);
29 $ob->read_const_time(300);
33 $ob->write("\n"); # initial wak
39 $ob->write("\n"); # wake for real
40 my ($count, $result) = $ob->read(10); # read up to 10 chars
41 if ($result eq "\n\r") {
42 print "awoke on attempt $attempts :)\n";
45 print "wake error on attempt $attempts :(\n";
48 die("failed to wake device - tried $attempts times\n") unless $attempts<6;
51 $ob->write("STRMON\n");
55 $SIG{TERM} = $SIG{INT} = sub {++$end; print "\nending $end\n";};
59 # print "end: $end\n";
61 my ($count, $l) = $ob->read(255);
68 my @l = split /[\cJ\cM]/, $l;
69 my ($wspeed, $wdir, $val1, $val2, $sort, $batt);
75 my ($k, $v) = $_ =~ /^(\d)\s+=\s+([\dabcdef]+)/;
76 next unless defined $k && defined $v;
80 if (($h & 0xf0) == 0x80) {
82 } elsif (($h & 0xf0) == 0xa0) {
84 } elsif (($h & 0xf0) == 0xe0) {
86 } elsif (($h & 0xf0) == 0x50) {
89 $sort = 'W'; # wind only
91 if ($h & 0x08) { # new battery required
97 $wdir = int($h * 360 / 255);
102 $val2 = (($h >> 4) << 8) | $val2;
103 } elsif ($sort eq 'T') {
104 $val2 = ($h << 8) | $val2;
106 $val2 = ($val2 << 8) | $h;
111 printf("%02X ", $_) for (@hex);
113 printf "$sort$batt %3d %3d %0.1f\n", $wspeed, $wdir, (($val2/160) - 32) * 5/9;
114 } elsif ($sort eq 'H') {
115 printf "$sort$batt %3d %3d %0.1f\n", $wspeed, $wdir, $val2/10;
116 } elsif ($sort eq 'P') {
117 printf "$sort$batt %3d %3d %0.2f\n", $wspeed, $wdir, $val2 / 1000;
118 } elsif ($sort eq 'R') {
119 printf "$sort$batt %3d %3d $val1\n", $wspeed, $wdir;
120 } elsif ($sort eq 'W') {
121 printf "$sort$batt %3d %3d\n", $wspeed, $wdir, $val2;
128 $ob->write("STRMOFF\n");