Instrument/Agilent34410A.pm untested

filter: Statementstmt 0.0% 0 / 315 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 37 totaltotal 0.0% 0 / 578 CC 47 SCAR 32.7
CC
47
Cov
0%
get_data
110.0
_check_range
56.0
config_measurement
56.0
CRAP 26.4
1 package Lab::Instrument::Agilent34410A;
2 #ABSTRACT: HP/Agilent/Keysight 34410A or 34411A digital multimeter
3 $Lab::Instrument::Agilent34410A::VERSION = '3.899';
4 use v5.20;
5  
6  
7  
8 use warnings;
9 use strict;
10  
11 use Time::HiRes qw (usleep);
12 use Lab::Instrument;
13 use Lab::Instrument::Multimeter;
14 use Lab::SCPI qw(scpi_match);
15 use Data::Dumper;
16 our @ISA = ("Lab::Instrument::Multimeter");
17  
18 our %fields = (
19 supported_connections => [ 'VISA_GPIB', 'GPIB', 'DEBUG', 'DUMMY' ],
20  
21 # default settings for the supported connections
22 connection_settings => {
23 gpib_board => 0,
24 gpib_address => undef,
25 timeout => 2,
26 read_default => "device",
27 termchar => "\n"
28 },
29  
30 device_settings => {
31 pl_freq => 50,
32 },
33  
34 device_cache => {
35 'function' => undef,
36 'range' => undef,
37 'nplc' => undef,
38 'resolution' => undef,
39 'tc' => undef,
40 'bw' => undef,
41 },
42  
43 device_cache_order =>
44 [ 'function', 'range', 'nplc', 'resolution', 'tc', 'bw' ],
45  
46 );
47  
48  
49 sub new {
50 my $proto = shift;
51 my $class = ref($proto) || $proto;
52 my $self = $class->SUPER::new(@_);
53 $self->_construct($class);
54  
55 # Perform initial device clear.
56 $self->clear();
57  
58 return $self;
59 }
60  
61  
62 sub get_value {
63 my ( $self, $tail ) = _init_getter(@_);
64  
65 return $self->request( ":read?", $tail );
66 }
67  
68  
69 sub get_error {
70 my $self = shift;
71 chomp( my $err = $self->query("SYSTem:ERRor?") );
72 my ( $err_num, $err_msg ) = split ",", $err;
73 $err_msg =~ s/\"//g;
74 return ( $err_num, $err_msg );
75 }
76  
77  
78 sub reset { # basic
79 my $self = shift;
80 $self->write("*RST");
81 $self->reset_device_cache();
82 }
83  
84  
85 # to be moved into Lab::Instrument::Multimeter??
86 sub assert_function {
87 my $self = shift;
88 my $keyword = shift;
89  
90 my $function = $self->get_function( { read_mode => 'cache' } );
91 if ( scpi_match( $function, $keyword ) == 0 ) {
92 Lab::Exception::CorruptParameter->throw(
93 "invalid function '$function': allowed choices are: $keyword");
94 }
95 return $function;
96 }
97  
98 # ------------------------------- SENSE ---------------------------------
99  
100 my $valid_functions
101 = 'current[:dc]|current:ac|voltage[:dc]|voltage[:ac]|resistance|fresistance';
102  
103  
104 sub set_function { # basic
105 my $self = shift;
106  
107 my ( $function, $tail ) = $self->_check_args_strict( \@_, ['function'] );
108  
109 if ( not scpi_match( $function, $valid_functions ) ) {
110 Lab::Exception::CorruptParameter->throw( error =>
111 "Agilent 34410A:\n\nAgilent 34410A:\nunexpected value for FUNCTION in sub set_function. Expected values are VOLTAGE:DC, VOLTAGE:AC, CURRENT:DC, CURRENT:AC, RESISTANCE or FRESISTANCE.\n"
112 );
113 }
114  
115 $self->write( "FUNCTION '$function'", $tail );
116  
117 }
118  
119  
120 sub get_function {
121 my $self = shift;
122  
123 # read from cache or from device?
124 my ($tail) = $self->_check_args( \@_, [] );
125  
126 # read from device:
127 my $function = $self->query( "FUNCTION?", $tail );
128 if ( $function =~ /([\w:]+)/ ) {
129 return $1;
130 }
131  
132 # FIXME: throw here?
133 }
134  
135  
136 sub _invalid_range {
137 my $range = shift;
138 Lab::Exception::CorruptParameter->throw( error =>
139 "set_range: unexpected value '$range' for RANGE. Expected values are for CURRENT, VOLTAGE and RESISTANCE mode -3...+3A, 0.1...1000V or 0...1e9 Ohms respectivly"
140 );
141 }
142  
143 sub _check_range {
144 my $function = shift;
145 my $range = shift;
146  
147 if ( scpi_match( $function, 'voltage[:dc]|voltage:ac' ) ) {
148 if ( abs($range) > 1000 ) {
149 _invalid_range($range);
150 }
151 }
152 elsif ( scpi_match( $function, 'current[:dc]|current:ac' ) ) {
153 if ( abs($range) > 3 ) {
154 _invalid_range($range);
155 }
156 }
157 elsif ( scpi_match( $function, 'resistance|fresistance' ) ) {
158 if ( $range < 0 or $range > 1e9 ) {
159 _invalid_range($range);
160 }
161 }
162 else {
163 Lab::Exception::CorruptParameter->throw( error =>
164 "set_range: Unexpected function '$function'. Expected values are VOLTAGE:DC, VOLTAGE:AC, CURRENT:DC, CURRENT:AC, RESISTANCE or FRESISTANCE."
165 );
166 }
167 }
168  
169 sub set_range { # basic
170 my $self = shift;
171  
172 my ( $range, $tail ) = $self->_check_args_strict( \@_, ['range'] );
173 my $function = $self->get_function( { read_mode => 'cache' } );
174  
175 # check if value of paramter 'range' is valid:
176  
177 if ( not scpi_match( $range, 'min|max|def|auto' ) ) {
178 _check_range( $function, $range );
179 }
180  
181 # set range
182 if ( scpi_match( $range, 'min|max|def' )
183 or $range =~ /\b\d+(e\d+|E\d+|exp\d+|EXP\d+)?\b/ ) {
184 $self->write( "$function:RANGE $range", $tail );
185 }
186 elsif ( scpi_match( $range, 'auto' ) ) {
187 $self->write( sprintf( "%s:RANGE:AUTO ON", $function ), $tail );
188 }
189 else {
190 Lab::Exception::CorruptParameter->throw(
191 error => "anything's wrong in sub set_range!!" );
192 }
193 }
194  
195 sub _init_getter {
196 my $self = shift;
197 my ($tail) = $self->_check_args( \@_, [] );
198 return ( $self, $tail );
199 }
200  
201  
202 sub get_range {
203 my ( $self, $tail ) = _init_getter(@_);
204  
205 my $function = $self->assert_function($valid_functions);
206  
207 return $self->query( "$function:RANGE?", $tail );
208 }
209  
210  
211 sub get_autorange {
212 my ( $self, $tail ) = _init_getter(@_);
213 my $function = $self->assert_function($valid_functions);
214  
215 return $self->query("$function:RANGE:AUTO?");
216 }
217  
218 my $valid_dc_functions = 'current[:dc]|voltage[:dc]|resistance|fresistance';
219  
220  
221 sub set_nplc { # basic
222 my $self = shift;
223  
224 my ( $nplc, $tail ) = $self->_check_args_strict( \@_, ['nplc'] );
225  
226 # check if value of paramter 'nplc' is valid:
227 if ( ( $nplc < 0.006 or $nplc > 100 ) and not $nplc =~ /^(min|max|def)$/ )
228 {
229 Lab::Exception::CorruptParameter->throw( error =>
230 "unexpected value for NPLC in sub set_nplc. Expected values are between 0.006 ... 100 power-line-cycles (50Hz)."
231 );
232 }
233  
234 # set nplc:
235 my $function = $self->assert_function($valid_dc_functions);
236  
237 $self->write( "$function:NPLC $nplc", $tail );
238 }
239  
240  
241 sub get_nplc {
242 my ( $self, $tail ) = _init_getter(@_);
243  
244 my $function = $self->assert_function($valid_dc_functions);
245  
246 return $self->query( "$function:NPLC?", $tail );
247 }
248  
249  
250 sub set_resolution { # basic
251 my $self = shift;
252 my ( $resolution, $tail )
253 = $self->_check_args_strict( \@_, ['resolution'] );
254 my $function = $self->assert_function($valid_dc_functions);
255  
256 # check if value of paramter 'resolution' is valid:
257 # FIXME: wiso read_mode 'device' ???
258 my $range = $self->get_range( { read_mode => 'device' } );
259  
260 if ( $resolution < 0.3e-6 * $range
261 and not $resolution =~ /^(min|max|def)$/ ) {
262 Lab::Exception::CorruptParameter->throw( error =>
263 "\nAgilent 34410A:\nunexpected value for RESOLUTION in sub set_resolution. Expected values have to be greater than 0.3e-6*RANGE."
264 );
265 }
266  
267 # switch off autorange function if activated.
268 $self->set_range($range);
269 $self->write( "$function:RES $resolution", $tail );
270  
271 }
272  
273  
274 sub get_resolution {
275 my ( $self, $tail ) = _init_getter(@_);
276  
277 my $function = $self->assert_function($valid_functions);
278  
279 return $self->query( "$function:RES?", $tail );
280 }
281  
282  
283 sub set_tc { # basic
284 my $self = shift;
285  
286 my ( $tc, $tail ) = $self->_check_args_strict( \@_, ['tc'] );
287  
288 my $function = $self->assert_function($valid_dc_functions);
289  
290 # check if value of paramter 'tc' is valid:
291 if ( ( $tc < 1e-4 or $tc > 1 ) and not $tc =~ /^(min|max|def)$/ ) {
292 Lab::Exception::CorruptParameter->throw( error =>
293 "unexpected value for APERTURE in sub set_tc. Expected values are between 1e-4 ... 1 sec."
294 );
295 }
296  
297 # set tc:
298 $self->write( ":$function:APERTURE $tc; APERTURE:ENABLED 1", $tail );
299 }
300  
301  
302 sub get_tc {
303 my ( $self, $tail ) = _init_getter(@_);
304  
305 my $function = $self->assert_function($valid_dc_functions);
306  
307 return $self->query( "$function:APERTURE?", $tail );
308 }
309  
310 my $valid_ac_functions = 'current:ac|voltage:ac';
311  
312  
313 sub set_bw { # basic
314 my $self = shift;
315 my ( $bw, $tail ) = $self->_check_args_strict( \@_, ['bandwidth'] );
316  
317 my $function = $self->assert_function($valid_ac_functions);
318  
319 # check if value of paramter 'bw' is valid:
320 if ( ( $bw < 3 or $bw > 200 ) and not $bw =~ /^(min|max|def)$/ ) {
321 Lab::Exception::CorruptParameter->throw( error =>
322 "\nAgilent 34410A:\nunexpected value for BANDWIDTH in sub set_bw. Expected values are between 3 ... 200 Hz."
323 );
324 }
325  
326 # set bw:
327 $self->write( "$function:BANDWIDTH $bw", $tail );
328 }
329  
330  
331 sub get_bw {
332 my ( $self, $tail ) = _init_getter(@_);
333  
334 my $function = $self->assert_function($valid_ac_functions);
335  
336 return $self->query( "$function:BANDWIDTH?", $tail );
337 }
338  
339 # ----------------------------- TAKE DATA ---------------------------------------------------------
340  
341  
342 sub config_measurement { # basic
343 my $self = shift;
344  
345 # parameter == hash??
346 my ( $function, $nop, $time, $range, $trigger ) = $self->_check_args(
347 \@_,
348 [ 'function', 'nop', 'time', 'range', 'trigger' ]
349 );
350  
351 # check input data
352 if ( not defined $trigger ) {
353 $trigger = 'BUS';
354 }
355 if ( not defined $range ) {
356 $range = 'DEF';
357 }
358 if ( not defined $time ) {
359 Lab::Exception::CorruptParameter->throw( error =>
360 "too view arguments given in sub config_measurement. Expected arguments are FUNCTION, #POINTS, TIME, <RANGE>, <TRIGGERSOURCE>"
361 );
362 }
363  
364 print "--------------------------------------\n";
365 print "Agilent34410A: sub config_measurement:\n";
366  
367 # clear buffer
368 my $points = $self->query( "DATA:POINTS?", { read_mode => 'device' } );
369 if ( $points > 0 ) {
370 $points = $self->connection()
371 ->LongQuery( command => "DATA:REMOVE? $points" );
372 }
373  
374 # set function
375 print "set_function: " . $self->set_function($function) . "\n";
376  
377 # set range
378 print "set_range: " . $self->set_range($range) . "\n";
379  
380 # set integration time
381 my $tc = $time / $nop;
382 print "set_tc: " . $self->set_tc($tc) . "\n";
383  
384 # set auto high impedance (>10GOhm) for VOLTAGE:DC for ranges 100mV, 1V, 10V
385 if ( $function
386 =~ /^(VOLTAGE|voltage|VOLT|volt|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc)$/
387 ) {
388 print "set_auto_high_impedance\n";
389 $self->write("SENS:VOLTAGE:DC:IMPEDANCE:AUTO ON");
390 }
391  
392 # perfome AUTOZERO and then disable
393 if ( $function
394 =~ /^(CURRENT|current|CURR|curr|CURRENT:DC|current:dc|CURR:DC|curr:dc|VOLTAGE|voltage|VOLT|volt|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc|RESISTANCE|resistance|RES|res|FRESISTANCE|fresistance|FRES|fres)$/
395 ) {
396 print "set_AUTOZERO OFF\n";
397 $self->write( sprintf( "SENS:%s:ZERO:AUTO OFF", $function ) );
398 }
399  
400 # triggering
401 print "set Trigger Source: " . $self->_set_triggersource("BUS") . "\n";
402 print "set Trigger Count: " . $self->_set_triggercount(1) . "\n";
403 print "set Trigger Delay: " . $self->_set_triggerdelay("MIN") . "\n";
404  
405 print "set Sample Count: " . $self->_set_samplecount($nop) . "\n";
406 print "set Sample Delay: " . $self->_set_sampledelay(0) . "\n";
407  
408 print "init()\n";
409 $self->write("INIT");
410 usleep(5e5);
411  
412 print "Agilent34410A: sub config_measurement complete\n";
413 print "--------------------------------------\n";
414  
415 }
416  
417  
418 sub trg { # basic
419 my $self = shift;
420 $self->write("*TRG");
421 }
422  
423  
424 sub get_data { # basic
425 my $self = shift;
426 my $data;
427 my @data;
428  
429 # parameter == hash??
430 my ($readings) = $self->_check_args( \@_, ['readings'] );
431  
432 if ( not defined $readings ) { $readings = "ALL"; }
433  
434 if ( $readings >= 1 and $readings <= 50000 ) {
435 if ( $readings > $self->query("SAMPLE:COUNT?") ) {
436 $readings = $self->query("SAMPLE:COUNT?");
437 }
438 for ( my $i = 1; $i <= $readings; $i++ ) {
439 my $break = 1;
440 while ($break) {
441 $data = $self->connection()->LongQuery( command => "R? 1" );
442  
443 my $index;
444 if ( index( $data, "+" ) == -1 ) {
445 $index = index( $data, "-" );
446 }
447 elsif ( index( $data, "-" ) == -1 ) {
448 $index = index( $data, "+" );
449 }
450 else {
451 $index
452 = ( index( $data, "-" ) < index( $data, "+" ) )
453 ? index( $data, "-" )
454 : index( $data, "+" );
455 }
456 $data = substr( $data, $index, length($data) - $index );
457 if ( $data != 0 ) { $break = 0; }
458 else { usleep(1e5); }
459 }
460 push( @data, $data );
461 }
462 if ( $readings == 1 ) { return $data; }
463 else { return @data; }
464 }
465 elsif ( $readings eq "ALL" or $readings = "all" ) {
466  
467 # wait until data are available
468 $self->wait();
469 $data = $self->connection()->LongQuery( command => "FETC?" );
470  
471 @data = split( ",", $data );
472 return @data;
473 }
474 else {
475 Lab::Exception::CorruptParameter->throw( error =>
476 "unexpected value for number of readINGS in sub get_data. Expected values are from 1 ... 50000 or ALL."
477 );
478 }
479  
480 }
481  
482  
483 sub abort { # basic
484 my $self = shift;
485 $self->write("ABOR");
486 }
487  
488  
489 sub wait { # basic
490 my $self = shift;
491  
492 while (1) {
493 if ( $self->active() ) { usleep(1e3); }
494 else { last; }
495 }
496  
497 return 0;
498  
499 }
500  
501  
502 sub active { # basic
503 my $self = shift;
504  
505 my $status = sprintf(
506 "%.15b",
507 $self->query( "STAT:OPER:COND?", { read_mode => 'device' } )
508 );
509 my @status = split( "", $status );
510 if ( $status[5] == 1 && $status[10] == 0 ) {
511 return 0;
512 }
513 else {
514 return 1;
515 }
516  
517 }
518  
519 # ------------------ TRIGGER and SAMPLE settings ---------------------- #
520  
521 sub _set_triggersource { # internal
522 my $self = shift;
523  
524 # parameter == hash??
525 my ($source) = $self->_check_args( \@_, ['trigger_source'] );
526  
527 if ( not defined $source ) {
528 $source = $self->query( sprintf("TRIGGER:SOURCE?") );
529  
530 $self->{config}->{triggersource} = $source;
531 return $source;
532 }
533  
534 if ( $source =~ /^(IMM|imm|EXT|ext|BUS|bus|INT|int)$/ ) {
535 $source = $self->query(
536 sprintf( "TRIGGER:SOURCE %s; SOURCE?", $source ) );
537 $self->{config}->{triggersource} = $source;
538 return $source;
539 }
540 else {
541 Lab::Exception::CorruptParameter->throw( error =>
542 "\nAgilent 34410A:\nunexpected value for TRIGGER_SOURCE in sub _set_triggersource. Expected values are:\n IMM --> immediate trigger signal\n EXT --> external trigger\n BUS --> software trigger signal via bus\n INT --> internal trigger signal\n"
543 );
544 }
545  
546 }
547  
548 sub _set_triggercount { # internal
549 my $self = shift;
550  
551 # parameter == hash??
552 my ($count) = $self->_check_args( \@_, ['trigger_count'] );
553  
554 if ( not defined $count ) {
555 $count = $self->query( sprintf("TRIGGER:COUNT?") );
556  
557 $self->{config}->{triggercount} = $count;
558 return $count;
559 }
560  
561 if ( $count >= 0 or $count <= 50000 ) {
562 $count
563 = $self->query( sprintf( "TRIGGER:COUNT %d; COUNT?", $count ) );
564 $self->{config}->{triggercount} = $count;
565 return $count;
566 }
567 else {
568 Lab::Exception::CorruptParameter->throw( error =>
569 "\nAgilent 34410A:\nunexpected value for COUNT in sub _set_triggercount. Expected values are between 1 ... 50.000\n"
570 );
571 }
572  
573 }
574  
575 sub _set_triggerdelay { # internal
576 my $self = shift;
577  
578 # parameter == hash??
579 my ($delay) = $self->_check_args( \@_, ['trigger_delay'] );
580  
581 if ( not defined $delay ) {
582 $delay = $self->query( sprintf("TRIGGER:DELAY?") );
583  
584 $self->{config}->{triggerdely} = $delay;
585 return $delay;
586 }
587  
588 if ( $delay =~ /^(min|max|def)$/i or $delay >= 0 or $delay <= 3600 ) {
589 $delay = $self->query("TRIGGER:DELAY $delay; DELAY?");
590 $self->{config}->{triggerdely} = $delay;
591 return $delay;
592 }
593 elsif ( $delay =~ /^(AUTO|auto)$/ ) {
594 $delay = $self->query("TRIGGER:DELAY:AUTO ON; AUTO?");
595 $self->{config}->{triggerdely} = "AUTO ON";
596 return "AUTO ON";
597 }
598 else {
599 Lab::Exception::CorruptParameter->throw( error =>
600 "\nAgilent 34410A:\nunexpected value for DELAY in sub _set_triggerdelay. Expected values are between 1 ... 3600, or 'MIN = 0', 'MAX = 3600' or 'AUTO'\n"
601 );
602 }
603 }
604  
605 sub _set_samplecount { # internal
606 my $self = shift;
607  
608 # parameter == hash??
609 my ($count) = $self->_check_args( \@_, ['sample_count'] );
610  
611 if ( not defined $count ) {
612 $count = $self->query( sprintf("SAMPLE:COUNT?") );
613  
614 $self->{config}->{samplecount} = $count;
615 return $count;
616 }
617  
618 elsif ( $count < 0 or $count >= 50000 ) {
619 Lab::Exception::CorruptParameter->throw( error =>
620 "\nAgilent 34410A:\nunexpected value for COUNT in sub _set_samplecount. Expected values are between 1 ... 50.000\n"
621 );
622 }
623 else {
624 $count = $self->query( sprintf( "SAMPLE:COUNT %d; COUNT?", $count ) );
625 $self->{config}->{samplecount} = $count;
626 return $count;
627 }
628 }
629  
630 sub _set_sampledelay { # internal
631 my $self = shift;
632  
633 # parameter == hash??
634 my ($delay) = $self->_check_args( \@_, ['sample_delay'] );
635  
636 if ( not defined $delay ) {
637 $delay = $self->query( sprintf("SAMPLE:TIMER?") );
638  
639 $self->{config}->{sampledelay} = $delay;
640 return $delay;
641 }
642  
643 if ( $delay =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
644 $delay = $self->query( sprintf( "SAMPLE:TIMER %s; TIMER?", $delay ) );
645 $self->write("SAMPLE:SOURCE TIM");
646 $self->{config}->{samplecount} = $delay;
647 return $delay;
648 }
649 elsif ( $delay >= 0 or $delay <= 3600 ) {
650 $delay
651 = $self->query( sprintf( "SAMPLE:TIMER %.5f; TIMER?", $delay ) );
652 $self->write("SAMPLE:SOURCE TIM");
653 $self->{config}->{samplecount} = $delay;
654 return $delay;
655 }
656  
657 else {
658 Lab::Exception::CorruptParameter->throw( error =>
659 "\nAgilent 34410A:\nunexpected value for DELAY in sub _set_sampledelay. Expected values are between 1 ... 3600, or 'MIN = 0', 'MAX = 3600'\n"
660 );
661 }
662  
663 }
664  
665 # ------------------------------- DISPLAY and BEEPER --------------------------------------------
666  
667  
668 sub display_text { # basic
669 my $self = shift;
670  
671 # parameter == hash??
672 my ($text) = $self->_check_args( \@_, ['display_text'] );
673  
674 if ($text) {
675 $self->write(qq(DISPlay:TEXT "$text"));
676 }
677 else {
678 $text = $self->query(qq(DISPlay:TEXT?));
679 $text =~ s/\"//g;
680 }
681 return $text;
682 }
683  
684  
685 sub display_on { # basic
686 my $self = shift;
687 $self->write("DISPLAY ON");
688 }
689  
690  
691  
692 sub display_off { # basic
693 my $self = shift;
694 $self->write("DISPLAY OFF");
695 }
696  
697  
698 sub display_clear { # basic
699 my $self = shift;
700 $self->write("DISPLAY:TEXT:CLEAR");
701 }
702  
703  
704 sub beep { # basic
705 my $self = shift;
706 $self->write("SYSTEM:BEEPER");
707 }
708  
709 1;
710  
711 __END__