-
Notifications
You must be signed in to change notification settings - Fork 1
/
fluke45.pl
executable file
·303 lines (221 loc) · 6.59 KB
/
fluke45.pl
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/perl -w
#==============================================================================
# Fluke 45/8808A Data Retrieval and Control Utility
#------------------------------------------------------------------------------
#
# This is a utility for requesting data from a Fluke 89IV, 187/189, or 287/289.
#
# Options:
#
# --csv -c Outputs meter data in CSV format
# --debug -d Shows Debugging Output
# --help -h This text
# --interval -i num Takes a new reading every num seconds
# --port -p /dev/file Reads from filename instead of the
#
#==============================================================================
use strict;
use warnings;
use bytes;
use Getopt::Long;
use Device::SerialPort;
use Data::Dumper;
use POSIX qw(strftime);
# Change these to change default behaviors
my $device = "/dev/ttyUSB0";
my $debug; # = 1;
#========================================
# NOTHING BELOW THIS LINE NEEDS CHANGED.
#========================================
#=====================================
# Global variables and module
# definitions
#=====================================
my $csv;
my $help;
my $interval;
my $port;
# Set flags and options where needed
# from the command line.
GetOptions (
"csv|c" => \$csv,
"debug|d" => \$debug,
"help|h" => \$help,
"interval|i=s" => \$interval,
"port|p=s" => \$device,
) or die "Huh? Type\"$0 --help\" for help.\n"; #"
if ( $help ) { showhelp(); }
#======================================
# Hardware Initialization
#======================================
# It's important that everything be 8-bit bytes.
binmode STDIN, ":bytes";
binmode STDOUT, ":unix";
$port = Device::SerialPort->new($device)
or die "FATAL: Can't open $device: $!\n";
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);
$port->datatype('raw');
$port->dtr_active(0);
$port->rts_active(1);
$port->purge_all;
$port->purge_rx;
$port->purge_tx;
#======================================
# Subroutines
#======================================
#####
#
# showhelp - Show helpful message.
#
#####
sub showhelp {
print STDERR <<HELPDOC;
Fluke 45/8808A Data Retrieval and Control Utility
-----------------------------------------------------------------
Usage:
$0 [OPTIONS]
Options:
--csv -c Outputs meter data in CSV format
--debug -d Shows Debugging Output
--help -h This text
--interval -i num Takes a new reading every num seconds
--port -p /dev/file Reads from filename instead of the
default device, $device.
------------------------------------------------------------------
HELPDOC
exit 0;
}
#####
#
# getresponse - Get a response from the meter, if any.
#
#####
sub getresponse {
# We need time for the meter to respond.
sleep 1;
my ($count,$got)=$port->read(1);
my $str = $got;
my $cnt = $count;
while ($count > 0) {
($count,$got)=$port->read(1);
$str .= $got;
$cnt += $count;
}
if ($debug) {
print "DEBUG: Read $cnt bytes from input buffer.\n";
my $debugstr=$str;
$debugstr =~ s/([^?])/sprintf("%02X ",ord($1))/ge;
print "DEBUG: Received: $debugstr\n";
}
my @outstr = split('', $str);
return @outstr;
}
#####
#
# command - Send command, check status, return requested data.
#
#####
sub command {
my $command = shift;
# Handle the one special case command here.
# By definition, system reset never returns.
if ($command eq '*RST') {
if ( $debug ) { print "DEBUG: System reset requested. Sleeping two seconds.\n"; }
sleep 2;
return '=>';
};
if ( $debug ) { print "DEBUG: Command \"$command\" sent. Awaiting response.\n"; }
# Query the meter and get a response.
$port->write("$command\r");
$port->write_drain;
my @returnstring = getresponse();
# If nothing's returned, nobody is home.
if ( ! @returnstring ) { die "FATAL: Is the meter connected, and turned on?\n"; }
# We need the returned text (if any), and the prompt to
# determine success. variable munging!
my $returnstring = join('',@returnstring);
@returnstring = split('\n',$returnstring);
# How much did it actually return? Some commands
# don't return text, others do.
my $n = @returnstring;
if ( $n == 1 ) {
$returnstring[1] = $returnstring[0];
}
# Ugly hack instead of doing it the right way.
if ( $returnstring[1] =~ /=>/ ) {
if ( $debug ) { print "DEBUG: Command \"$command\" OK\n"; }
} else {
die "FATAL: Meter unexpectedly returned:\n\n$returnstring[0]\nin response to command \"$command\".\n";
}
return $returnstring[0];
}
#####
#
# getmeasurement - Retrieve the primary measurement.
# printmeasurement - Pretty prints the retrieved measurement.
#
#####
sub getmeasurement {
return(command("MEAS?"));
}
sub printmeasurement {
my $measure = shift;
# clean up and split measurement data
$measure =~ s/ /,/g;
my @measure = (split ',', $measure);
# truncate any special characters at the end of the returned string.
if ($measure[3]) {
$measure[3] = "\"" . substr($measure[3],0,-1) . "\"";
$measure[2] = sprintf("%.6g", $measure[2]);
$measure[1] = "\"" . $measure[1] . "\"";
$measure[0] = sprintf("%.6g", $measure[0]);
} else {
$measure[1] = "\"" . substr($measure[1],0,-1) . "\"";
$measure[0] = sprintf("%.6g", $measure[0]);
}
# If it's CSV, let's give them what they want.
if ( $csv ) {
$measure = join (',', @measure);
print "\"" . strftime("%D %T", localtime) . "\"," . "$measure\n";
} else {
if ( $interval ) {
if ($measure[3]) {
$measure = "$measure[0] $measure[1]\t$measure[2] $measure[3]";
} else {
$measure = "$measure[0] $measure[1]";
}
$measure =~ s/,/ \t/g;
print `clear`;
print "Fluke 45 / 8808A\n";
print "-----------------------------\n";
print sprintf("%25s","$measure\n");
print "-----------------------------\n";
print sprintf("%30s",strftime("%D %T", localtime) . "\n");
} else {
if ($measure[3]) {
$measure = "$measure[0] $measure[1]\t$measure[2] $measure[3]";
} else {
$measure = "$measure[0] $measure[1]";
}
print strftime("%D %T", localtime) . "\t$measure\n";
}
}
}
#####
# This should be the main affair
#####
# This is the default behavior.
if ( defined $interval ) {
if ( $debug ) {
print "DEBUG: Supposed to sleep for $interval seconds.\n";
}
while ( 1 ) {
printmeasurement(command('MEAS?'));
sleep ( $interval - 1 );
}
} else {
printmeasurement(command('MEAS?'));
}