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
|
#!/usr/bin/env perl
# -*- mode: cperl; -*-
# Basic Atlas Scientific EZO probe smoke test.
use 5.40.1;
use strict;
use warnings;
use Test2::V0;
plan tests => 3;
use Device::SerialPort;
use Time::HiRes qw(usleep);
my $port = '/dev/ttyUSB0';
my $baud = 9600;
my $serial = Device::SerialPort->new($port)
or BAIL_OUT("Cannot open serial port: $port");
$serial->baudrate($baud);
$serial->databits(8);
$serial->parity('none');
$serial->stopbits(1);
$serial->read_char_time(0);
$serial->read_const_time(100);
note("Testing EZO probe on $port at $baud baud");
my $info = ezo_command($serial, 'I', 500_000);
note("I => " . printable($info));
ok($info ne '', 'probe is reachable');
like($info, qr/(?:\?I,|EZO|PH|EC|DO|ORP)/i, 'probe is identifiable');
my $reading = ezo_command($serial, 'R', 1_500_000);
note("R => " . printable($reading));
like(
$reading,
qr/OK/,
'probe returns a single reading'
);
sub ezo_command {
my ($serial, $command, $wait_us) = @_;
drain_serial($serial);
my $written = $serial->write("$command\r");
return '' unless defined $written && $written > 0;
usleep($wait_us);
my $reply = '';
while (1) {
my ($count, $buffer) = $serial->read(255);
last unless $count;
$reply .= $buffer;
}
return clean_reply($reply);
}
sub drain_serial {
my ($serial) = @_;
while (1) {
my ($count, undef) = $serial->read(255);
last unless $count;
}
}
sub clean_reply {
my ($reply) = @_;
$reply
=~ s/\r/\n/gr
=~ s/\n+/\n/gr
=~ s/^\n|\n$//g;
return $reply;
}
sub printable {
my ($value) = @_;
return $value eq '' ? '<no response>' : $value;
}
|