-
Notifications
You must be signed in to change notification settings - Fork 1
/
u1241an.pl
executable file
·324 lines (233 loc) · 7.31 KB
/
u1241an.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
#!/usr/bin/perl -w
#==============================================================================
# Agilent U1241AN 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 Time::HiRes qw ( sleep );
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 = 0;
#========================================
# 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');
system('/bin/stty -F ' . $device . ' clocal cs8 cread raw -parenb -cstopb min 0 ignpar') == 0 || die;
#$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;
Agilent U1241AN 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) {
my $debugstr = $str;
$debugstr =~ s/([^?])/sprintf("%02X ",ord($1))/ge;
print "DEBUG: Received: $debugstr\n";
}
return $str;
}
#####
#
# command - Send command, check status, return requested data.
#
#####
sub command {
my $command = shift;
my $timeout = shift;
# Clear the input buffer.
my $garbage = getresponse(0);
if ( $debug && $garbage ) {
$garbage =~ s/\n//g;
$garbage =~ s/\r//g;
print "DEBUG: Garbage collected was \"$garbage\".\n";
}
# Query the meter and get a response.
$port->write("$command\n");
$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);
$returnstring =~ s/\n//g;
$returnstring =~ s/\r//g;
if ( $debug ) { print "DEBUG: Command returned \"$returnstring\".\n"; }
if ( ! $returnstring ) { die "FATAL: Is the meter turned on, and connected?\n"; }
# Check error states, just in case.
$port->write("SYST:ERR?\n");
$port->write_drain;
$garbage = getresponse(0.15);
$garbage =~ s/\n//g;
$garbage =~ s/\r//g;
if ( $garbage =~ m/\+0,\"No err/ ) {
if ( $debug ) { print "DEBUG: Error status was \"$garbage\".\n"; }
} else {
die "FATAL: Meter returned error \"$garbage\". Please check the\n meter's settings and try again.\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/ ) { 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/ ) { return "ADC"; }
when ( /CAP/ ) { return "F"; }
when ( /T1:K CEL/ ) { return "°C"; }
when ( /T1:K FAR/ ) { return "°F"; }
when ( /CPER/ ) { return "%"; }
default { die "FATAL: Unexpected unit of measurement \"$rawunits\".\n"; }
};
}
sub printmeasurement {
my $measure = shift;
# clean up and split measurement data
my @measure = (split ',', $measure);
$measure[1] = "\"". translatemeasurement(command("CONF?", 0.2)) . "\"";
$measure[0] = sprintf("%.10g", $measure[0]);
# Cover 'Offline'
if ($measure[0] eq "9.9e+37") { $measure[0] = "Offline"; }
# If it's CSV, let's give them what they want.
if ( $csv ) {
$measure = "$measure[0],$measure[1]";
print "\"" . strftime("%D %T", localtime) . "\"," . "$measure\n";
} else {
if ( defined $interval ) {
$measure = "$measure[0] $measure[1]";
$measure =~ s/\"//g;
print `clear`;
print "Agilent U1240 Series\n";
print "-----------------------------\n";
print sprintf("%25s","$measure\n");
print "-----------------------------\n";
print sprintf("%30s",strftime("%D %T", localtime) . "\n");
} 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";
}
if ( defined $interval && $interval < 1 ) { $interval = 1 };
while ( 1 ) {
# It should only take 0.3 seconds to get the next reading
# including errors, if any.
my $debugstr = sleep ( $interval - 0.675 );
if ( $debug ) { print "DEBUG: Slept $debugstr seconds.\n"; }
printmeasurement(command('FETC?', 0.15));
}
} else {
printmeasurement(command('FETC?', 0.15));
}