Instrument/HP34420A.pm untested

filter: Statementstmt 0.0% 0 / 203 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 384 CC 44 SCAR 39.7
CC
44
Cov
0%
configure_voltage_dc_trigger
210.0
_configure_voltage_dc
72.0
autozero
30.0
CRAP 53.0
1 package Lab::Instrument::HP34420A;
2 #ABSTRACT: HP/Agilent 34420A digital multimeter
3 $Lab::Instrument::HP34420A::VERSION = '3.899';
4 use v5.20;
5  
6 use strict;
7 use Scalar::Util qw(weaken);
8 use Lab::Instrument;
9 use Carp;
10 use Data::Dumper;
11 use Lab::Instrument::Multimeter;
12  
13 our @ISA = ("Lab::Instrument::Multimeter");
14  
15 our %fields = (
16 supported_connections => ['GPIB'],
17  
18 # default settings for the supported connections
19 connection_settings => {
20 gpib_board => 0,
21 gpib_address => undef,
22 },
23  
24 device_settings => {
25 pl_freq => 50,
26 },
27  
28 device_cache => {
29  
30 # TO DO: add range and resolution + get/setter
31 }
32  
33 );
34  
35 sub new {
36 my $proto = shift;
37 my $class = ref($proto) || $proto;
38 my $self = $class->SUPER::new(@_);
39 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
40 return $self;
41 }
42  
43 #
44 # first, all internal stuff
45 #
46  
47 #
48 # all methods that fill in general Multimeter methods
49 #
50  
51 sub _display_clear {
52 my $self = shift;
53 $self->connection()->Write( command => "DISPlay:TEXT:CLEar" );
54 }
55  
56 sub _id {
57 my $self = shift;
58 return $self->query('*IDN?');
59 }
60  
61 sub _get_value {
62 my $self = shift;
63 my $value = $self->query('READ?');
64 chomp $value;
65 return $value;
66 }
67  
68 sub _device_init {
69 my $self = shift;
70  
71 }
72  
73 #
74 # all methods that are called directly
75 #
76  
77 sub get_resistance {
78 my $self = shift;
79 my ( $range, $resolution ) = @_;
80  
81 $range = "DEF" unless ( defined $range );
82 $resolution = "DEF" unless ( defined $resolution );
83  
84 my $cmd
85 = sprintf( "MEASure:SCALar:RESIStance? %s,%s", $range, $resolution );
86 my $value = $self->query($cmd);
87 return $value;
88 }
89  
90 sub get_4wresistance {
91 my $self = shift;
92 my ( $range, $resolution ) = @_;
93  
94 $range = "DEF" unless ( defined $range );
95 $resolution = "DEF" unless ( defined $resolution );
96  
97 my $cmd
98 = sprintf( "MEASure:SCALar:FRESIStance? %s,%s", $range, $resolution );
99 my $value = $self->query($cmd);
100 return $value;
101 }
102  
103 sub get_voltage_dc {
104 my $self = shift;
105 my ( $range, $resolution ) = @_;
106  
107 $range = "DEF" unless ( defined $range );
108 $resolution = "DEF" unless ( defined $resolution );
109  
110 my $cmd = sprintf( "MEASure:VOLTage:DC? %s,%s", $range, $resolution );
111 my $value = $self->query($cmd);
112 return $value;
113 }
114  
115 sub get_voltage_ac {
116 my $self = shift;
117 my ( $range, $resolution ) = @_;
118  
119 $range = "DEF" unless ( defined $range );
120 $resolution = "DEF" unless ( defined $resolution );
121  
122 my $cmd = sprintf( "MEASure:VOLTage:AC? %s,%s", $range, $resolution );
123 my $value = $self->query($cmd);
124 return $value;
125 }
126  
127 sub get_current_dc {
128 my $self = shift;
129 my ( $range, $resolution ) = @_;
130  
131 $range = "DEF" unless ( defined $range );
132 $resolution = "DEF" unless ( defined $resolution );
133  
134 my $cmd = sprintf( "MEASure:CURRent:DC? %s,%s", $range, $resolution );
135 my $value = $self->query($cmd);
136 return $value;
137 }
138  
139 sub get_current_ac {
140 my $self = shift;
141 my ( $range, $resolution ) = @_;
142  
143 $range = "DEF" unless ( defined $range );
144 $resolution = "DEF" unless ( defined $resolution );
145  
146 my $cmd = sprintf( "MEASure:CURRent:AC? %s,%s", $range, $resolution );
147 my $value = $self->query($cmd);
148 return $value;
149 }
150  
151 sub beep {
152 my $self = shift;
153 $self->write("SYSTem:BEEPer");
154 }
155  
156 sub get_error {
157 my $self = shift;
158 my $error = $self->query("SYST:ERR?");
159 if ( $error !~ /\+0,/ ) {
160 if ( $error =~ /^(\+[0-9]*)\,\"?(.*)\"?$/ ) {
161 return ( $1, $2 ); # ($code, $message)
162 }
163 else {
164 Lab::Exception::DeviceError->throw(
165 error =>
166 "Reading the error status of the device failed in Instrument::HP34401A::get_error(). Something's going wrong here.\n",
167 );
168 }
169 }
170 else {
171 return undef;
172 }
173 }
174  
175 sub get_status {
176 my $self = shift;
177  
178 # This is to be implemented with code that queries the status bit
179  
180 my $request = shift;
181 my $status = {};
182  
183 (
184 $status->{NOT_USED1}, $status->{NOT_USED2}, $status->{NOT_USED3},
185 $status->{CORR_DATA}, $status->{MSG_AVAIL}, $status->{EVNT},
186 $status->{SRQ}, $status->{NOT_USED4}
187 ) = $self->connection()->serial_poll();
188 return $status->{$request} if defined $request;
189 return $status;
190 }
191  
192 sub set_display_state {
193 my $self = shift;
194 my $value = shift;
195  
196 if ( $value == 1 || $value =~ /on/i ) {
197 $self->write( "DISP ON", @_ );
198 }
199 elsif ( $value == 0 || $value =~ /off/i ) {
200 $self->write( "DISP OFF", @_ );
201 }
202 else {
203 Lab::Exception::CorruptParameter->throw(
204 "set_display_state(): Illegal parameter.\n");
205 }
206 }
207  
208 sub set_display_text {
209 my $self = shift;
210 my $text = shift;
211 if ( $text
212 !~ /^[A-Za-z0-9\ \!\#\$\%\&\'\(\)\^\\\/\@\;\:\[\]\,\.\+\-\=\<\>\?\_]*$/
213 ) { # characters allowed by the 3458A
214 Lab::Exception::CorruptParameter->throw(
215 "set_display_text(): Illegal characters in given text.\n");
216 }
217 $self->write("DISP:TEXT $text");
218  
219 $self->check_errors();
220 }
221  
222 sub reset {
223 my $self = shift;
224 $self->connection()->Write( command => "*CLS" );
225 $self->connection()->Write( command => "*RST" );
226  
227 # $self->connection()->InstrumentClear($self->instrument_handle());
228 }
229  
230 sub wait_done {
231 my $self = shift;
232  
233 # wait until currently running program is finished.
234  
235 while ( !$self->get_status()->{"EVNT"} ) {
236 sleep 1;
237 }
238  
239 }
240  
241 sub autozero {
242 my $self = shift;
243 my $enable = shift;
244 my $az_status = undef;
245 my $command = "";
246  
247 if ( !defined $enable ) {
248  
249 # read autozero setting
250 $command = "ZERO:AUTO?";
251 $az_status = $self->query( $command, error_check => 1 );
252 }
253 else {
254 if ( $enable =~ /^ONCE$/i ) {
255 $command = "ZERO:AUTO ONCE";
256 }
257 elsif ( $enable =~ /^(ON|1)$/i ) {
258 $command = "ZERO:AUTO ONCE";
259 }
260 elsif ( $enable =~ /^(OFF|0)$/i ) {
261 $command = "ZERO:AUTO OFF";
262 }
263 else {
264 Lab::Exception::CorruptParameter->throw( error =>
265 "HP34401A::autozero() can be set to 'ON'/1, 'OFF'/0 or 'ONCE'. Received '${enable}'\n"
266 );
267 }
268 $self->write( $command, error_check => 1 );
269 }
270  
271 return $az_status;
272 }
273  
274 sub _configure_voltage_dc {
275 my $self = shift;
276 my $range = shift; # in V, or "AUTO", "MIN", "MAX"
277 my $tint = shift; # integration time in sec, "DEFAULT", "MIN", "MAX"
278 my $res_cmd = shift;
279  
280 if ( $range eq 'AUTO' || !defined($range) ) {
281 $range = 'DEF';
282 }
283 elsif ( $range =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
284  
285 #$range = sprintf("%e",abs($range));
286 }
287 elsif ( $range !~ /^(MIN|MAX)$/ ) {
288 Lab::Exception::CorruptParameter->throw( error =>
289 "Range has to be set to a decimal value or 'AUTO', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
290 );
291 }
292  
293 if ( $tint =~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
294  
295 # Convert seconds to PLC (power line cycles)
296 $tint *= $self->pl_freq();
297 }
298 elsif ( $tint !~ /^(MIN|MAX|DEFAULT)$/ ) {
299 Lab::Exception::CorruptParameter->throw( error =>
300 "Integration time has to be set to a positive value or 'AUTO', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
301 );
302 }
303  
304 if ( !defined($res_cmd) ) {
305 $res_cmd = '';
306 }
307  
308 # do it
309 $self->write( "CONF:VOLT:DC ${range} ${res_cmd}", error_check => 1 );
310 $self->write( "VOLT:DC:NPLC ${tint}", error_check => 1 )
311 if $res_cmd eq
312 ''; # integration time implicitly set through resolution
313 }
314  
315 sub configure_voltage_dc_trigger {
316 my $self = shift;
317 my $tint = shift
318 ; # integration time in sec, Default is 10PLC*pl_freq(), "MIN" = 0.02PLC , "MAX" = 200PLC
319 my $range = shift; # in V, or "DEF"(Default), "MIN", "MAX"
320 my $count = shift; # Measurment count, Default = 1
321 my $delay = shift; # in seconds, Default = 'MIN'
322 my $res_cmd = shift
323 ; # Resolution. Decimal (not number of digits!). NOT VALID FOR AC MEASUREMENT
324  
325 ### Check the parameters for errors
326  
327 $count = 1 if !defined($count);
328 Lab::Exception::CorruptParameter->throw(
329 error => "Sample count has to be an integer between 1 and 512\n" )
330 if ( $count !~ /^[0-9]*$/ || $count < 1 || $count > 512 );
331  
332 $delay = 0 if !defined($delay);
333 Lab::Exception::CorruptParameter->throw(
334 error => "Trigger delay has to be a positive decimal value\n" )
335 if ( $count !~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ );
336  
337 if ( !defined($tint) ) {
338 $tint = 10;
339 }
340 elsif ( $tint =~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
341  
342 # Convert seconds to PLC (power line cycles)
343 $tint *= $self->pl_freq();
344 if ( $tint > 200 || $tint < 0.02 ) {
345 Lab::Exception::CorruptParameter->throw( error =>
346 "Integration time out of bounds (int. time = $tint) in HP34401A::configure_voltage_dc()\n"
347 );
348 }
349 }
350 elsif ( $tint !~ /^(MIN|MAX)$/ ) {
351 Lab::Exception::CorruptParameter->throw( error =>
352 "Integration time has to be set to a positive value, 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
353 );
354 }
355  
356 if ( !defined($range) ) {
357 $range = 'DEF';
358 }
359 elsif ( $range =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
360 $range = sprintf( "%e", abs($range) );
361 }
362 elsif ( $range !~ /^(MIN|MAX)$/ ) {
363 Lab::Exception::CorruptParameter->throw( error =>
364 "Range has to be set to a decimal value or 'AUTO', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
365 );
366 }
367  
368 if ( !defined($res_cmd) ) {
369 $res_cmd = '';
370 }
371  
372 $self->write( "CONF:VOLT:DC ${range} ${res_cmd}", error_check => 1 );
373 $self->write( "VOLT:DC:NPLC ${tint}", error_check => 1 )
374 if $res_cmd eq
375 ''; # integration time implicitly set if resolution given
376  
377 $self->write("*ESE 1");
378 $self->write("*CLS");
379  
380 $self->write("TRIG:SOURce BUS");
381 $self->write("SAMPle:COUNt $count");
382 $self->write("TRIG:DELay $delay");
383  
384 }
385  
386 sub trigger {
387 my $self = shift;
388 $self->write("INIT");
389  
390 $self->write("*TRG");
391 $self->write("*OPC");
392  
393 }
394  
395 sub fetch {
396 my $self = shift;
397  
398 my $value = $self->query("FETCh?");
399  
400 chomp $value;
401  
402 my @valarray = split( ",", $value );
403  
404 return @valarray;
405 }
406  
407 sub trigger_read {
408 my $self = shift;
409 my $args = undef;
410 if ( ref $_[0] eq 'HASH' ) { $args = shift }
411 else { $args = {@_} }
412  
413 #$args->{'timeout'} = $args->{'timeout'} || $self->timeout();
414  
415 $self->write("INIT");
416 $self->write("*TRG");
417 my $value = $self->query( "FETCh?", $args );
418  
419 chomp $value;
420  
421 my @valarray = split( ",", $value );
422  
423 return @valarray;
424 }
425  
426 sub scroll_message {
427 use Time::HiRes (qw/usleep/);
428 my $self = shift;
429 my $message = shift
430 || " Lab::Measurement - designed to make measuring fun! ";
431 for my $i ( 0 .. ( length($message) - 12 ) ) {
432 $self->display_text( sprintf "%12.12s", substr( $message, $i ) );
433 usleep(100000);
434 }
435 $self->display_clear();
436 }
437  
438 1;
439  
440 __END__