-
Notifications
You must be signed in to change notification settings - Fork 1
/
bk2831.pl
executable file
·345 lines (248 loc) · 7.9 KB
/
bk2831.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/perl -w
#==============================================================================
# BK Precision 2831E/5491B 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);
use feature qw{ switch };
no if $] >= 5.018, warnings => "experimental::smartmatch";
# 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;
# cheating - used to shorten interval time.
my $func;
my $func2;
# 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;
BK Precision 2831E/5491B 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 {
my $timeout = shift;
# We need time for the meter to respond.
sleep $timeout;
my ($count,$got)=$port->read(1);
my $str = $got;
my $cnt = $count;
while ($count > 0) {
($count,$got)=$port->read(1);
$str .= $got;
$cnt += $count;
}
# Ensuring good reception.
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;
my $timeout = 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;
};
# Query the meter and get a response.
$port->write("$command\r");
$port->write_drain;
if ( $debug ) { print "DEBUG: Command \"$command\" sent. Awaiting response.\n"; }
# Fetch the returned data, keep trying if it returns nothing.
my @returnstring = getresponse($timeout);
# Verify that the comand actually ran successfully.
$port->write("SYSTEM:ERROR?\r");
$port->write_drain;
my @errorstring = getresponse(1);
my $errorstring = join('',@errorstring);
$errorstring =~ s/\n//g;
if ( ! @errorstring ) { die "FATAL: Is the meter connected, and turned on?\n"; }
if ( $errorstring eq "NO ERROR!" ) {
if ( $debug ) { print "DEBUG: Command \"$command\" OK\n"; }
} else {
die "FATAL: Meter unexpectedly returned \"$errorstring\" in response to command \"$command\".\n";
}
my $returnstring = join('',@returnstring);
$returnstring =~ s/\n//g;
if ( $debug ) { print "DEBUG: Command returned \"$returnstring\".\n"; }
return $returnstring;
}
#####
#
# translatemeasurement - Translate meter output into real units.
# printmeasurement - Pretty prints the retrieved measurement.
#
#####
sub translatemeasurement {
my $rawunits = shift;
if ( $debug ) { print "DEBUG: Raw measurement text is \"$rawunits\".\n"; }
# Translate from machine-speak to better symbols.
given ( $rawunits ) {
when ( /volt:ac/ ) { return "VAC"; }
when ( /volt:dc/ ) { return "VDC"; }
when ( /diod/ ) { return "VDC"; }
when ( /freq/ ) { return "HZ"; }
when ( /per/ ) { return "Sec"; }
when ( /res/ ) { return "Ohm"; }
when ( /cont/ ) { return "Ohm"; }
when ( /curr:ac/ ) { return "AAC"; }
when ( /curr:dc/ ) { return "ADC"; }
default { die "FATAL: Unexpected unit of measurement \"$rawunits\".\n"; }
};
}
sub printmeasurement {
my $measure = shift;
# clean up and split measurement data
my @measure = (split ',', $measure);
# truncate any special characters at the end of the returned string.
if ($measure[1]) {
# $measure[3] = "\"" . translatemeasurement(command("FUNC2?", 1)) . "\"";
$measure[3] = "\"$func2\"";
$measure[2] = sprintf("%.5g", $measure[1]);
# $measure[1] = "\"" . translatemeasurement(command("FUNC?", 1)) . "\"";
$measure[1] = "\"$func\"";
$measure[0] = sprintf("%.5g", $measure[0]);
} else {
# $measure[1] = "\"" . translatemeasurement(command("FUNC?", 1)) . "\"";
$measure[1] = "\"$func\"";
$measure[0] = sprintf("%.5g", $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 "BK Precision 2831E / 5491B\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 ( $interval < 4 ) { die "FATAL: The interval between readings needs to\n at least 4 seconds.\n\n"; }
# Initial meter queries, two seconds, for units:
$func = translatemeasurement(command("FUNC?", 1));
$func2 = translatemeasurement(command("FUNC2?", 1));
if ( defined $interval ) {
if ( $debug ) {
print "DEBUG: Supposed to sleep for $interval seconds.\n";
}
$interval -= 4 - 1;
if ( $interval < 0 ) { $interval = 0; }
while ( 1 ) {
printmeasurement(command('FETCH?', 2));
sleep ( $interval );
}
} else {
printmeasurement(command('FETCH?', 2));
}