Instrument/Agilent34420A.pm untested

filter: Statementstmt 0.0% 0 / 402 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 62 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 799 CC 87 SCAR 46.3
CC
87
Cov
0%
get_value
420.0
set_range
156.0
set_nplc
72.0
CRAP 102.6
1 package Lab::Instrument::Agilent34420A;
2 #ABSTRACT: HP/Agilent/Keysight 34420A or 34421A digital multimeter
3 $Lab::Instrument::Agilent34420A::VERSION = '3.899';
4 use v5.20;
5  
6 use strict;
7 use Lab::Instrument;
8 use Lab::MultiChannelInstrument;
9 use Time::HiRes qw (usleep);
10 use Data::Dumper;
11  
12 our @ISA = ( "Lab::MultiChannelInstrument", "Lab::Instrument" );
13  
14 our %fields = (
15 supported_connections => [ 'VISA', 'VISA_GPIB', 'GPIB', 'DEBUG' ],
16  
17 # default settings for the supported connections
18 connection_settings => {
19 gpib_board => 0,
20 gpib_address => undef,
21 timeout => 2,
22 },
23  
24 device_settings => {
25 pl_freq => 50,
26  
27 channels => {
28 Ch1 => 1,
29 Ch2 => 2
30 },
31 channel_default => 'Ch1',
32 channel => undef
33 },
34  
35 device_cache => {
36 function => undef,
37 range => undef,
38 autorange => undef,
39 nplc => undef,
40 resolution => undef,
41 value => undef,
42 channel => undef
43  
44 # TO DO: add range and resolution + get/setter
45 },
46  
47 device_cache_order => [ 'function', 'range', 'autorange', 'nplc' ],
48  
49 multichannel_shared_cache => [ 'function', 'nplc', 'channel' ],
50  
51 );
52  
53 sub new {
54 my $proto = shift;
55 my $class = ref($proto) || $proto;
56 my $self = $class->SUPER::new(@_);
57 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
58  
59 return $self;
60 }
61  
62 sub _device_init {
63 my $self = shift;
64  
65 $self->write("InPut:FILTer:STATe OFF");
66 }
67  
68 sub get_error {
69 my $self = shift;
70 chomp( my $err = $self->query("SYSTem:ERRor?") );
71 my ( $err_num, $err_msg ) = split ",", $err;
72 $err_msg =~ s/\"//g;
73 my $result;
74 $result->{'err_num'} = $err_num;
75 $result->{'err_msg'} = $err_msg;
76 return $result;
77 }
78  
79 sub reset { # basic
80 my $self = shift;
81 $self->write("*RST");
82 }
83  
84 sub selftest {
85 my $self = shift;
86  
87 # internal selftest:
88 # returns 0 if passed and 1 if failed
89  
90 $self->write("*TST?");
91 usleep(10e6);
92 return $self->read();
93 }
94  
95 # ------------------------------- sense ---------------------------------------------------------
96  
97 sub set_function { # basic
98 my $self = shift;
99  
100 # any parameters given?
101 if ( not defined @_[0] ) {
102 print Lab::Exception::CorruptParameter->new(
103 error => "no values given in " . ref($self) . " \n" );
104 return;
105 }
106  
107 my ( $function, $tail ) = $self->_check_args( \@_, ['function'] );
108  
109 # \nAgilent 34420A:\n
110 # Expected values for function are:\n
111 # voltage:dc, voltage:dc:ratio, voltage:dc:difference, resistance or Fresistance --> to set both input channels\n
112 # sense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\n
113 # sense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n
114 #
115  
116 $function =~ s/\s+//g; #remove all whitespaces
117 $function = "\L$function"
118 ; # transform all uppercase letters to lowercase letters
119  
120 return if $function eq $self->get_function( { read_mode => 'cache' } );
121  
122 if ( $function
123 =~ /^(voltage:dc|voltage|volt:dc|volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
124 or $function =~ /^(resistance|fresistance|res|fres)$/ ) {
125 $function
126 = $self->write( sprintf( "FUNCTION '%s'", $function ), $tail );
127 $self->get_range( { channel => 1 } );
128 $self->get_range( { channel => 2 } )
129 if $function =~ /^(voltage:dc|voltage|volt:dc|volt)$/;
130 $self->get_nplc();
131 $self->get_resolution();
132 }
133 else {
134 Lab::Exception::CorruptParameter->throw( error =>
135 "\nAgilent 34420A:\nunexpected value for FUNCTION in sub set_function. Expected values are:\nvoltage:dc, resistance or Fresistance --> to set both input channels\nsense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\nsense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n"
136 );
137 }
138  
139 }
140  
141 sub get_function {
142 my $self = shift;
143  
144 my ($tail) = $self->_check_args( \@_ );
145  
146 my $function = $self->query( "FUNCTION?", $tail );
147  
148 if ( $function =~ /([\w:]+)/ ) {
149 my $function = $1;
150 $function =~ s/\s+//g; #remove all whitespaces
151 $function = "\L$function"
152 ; # transform all uppercase letters to lowercase letters
153 return $function;
154 }
155  
156 }
157  
158 sub set_range { # basic
159 my $self = shift;
160  
161 # any parameters given?
162 if ( not defined @_[0] ) {
163 print Lab::Exception::CorruptParameter->new(
164 error => "no values given in " . ref($self) . " \n" );
165 return;
166 }
167  
168 my ( $range, $function, $channel, $tail )
169 = $self->_check_args( \@_, [ 'range', 'function', 'channel' ] );
170  
171 if ( not defined $channel ) {
172 $channel = $self->{channel}
173 || $self->get_channel( { read_mode => 'cache' } );
174 }
175  
176 if ( not defined $function ) {
177 $function = $self->get_function( { read_mode => 'cache' }, $tail );
178 }
179 $function =~ s/\s+//g; #remove all whitespaces
180 $function = "\L$function"
181 ; # transform all uppercase letters to lowercase letters
182  
183 # \nAgilent 34420A:\n
184 # Expected values for function are:\n
185 # voltage:dc, voltage:dc:ratio, voltage:dc:difference, resistance or Fresistance --> to set both input channels\n
186 # sense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\n
187 # sense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n
188 #
189 # Expected values for RANGE are:\n
190 # sense1:voltage:dc --> 1mV...100V
191 # sense2:voltage:dc --> 1mV...10V
192 # resistance and Fresistance --> 1...1e6 Ohm
193  
194 # check data
195 if ( $function
196 =~ /^(voltage:dc|voltage|volt:dc|volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
197 ) {
198 if ( $channel == 1 and abs($range) > 100 ) {
199 Lab::Exception::CorruptParameter->throw( error =>
200 "\nAgilent 34420A:\nunexpected value for RANGE in sub set_range. Expected values are for sense1:voltage:dc 1mV...100V and for sense2:voltage:dc 1mV...10V\n"
201 );
202 }
203 if ( $channel == 2 and abs($range) > 10 ) {
204 Lab::Exception::CorruptParameter->throw( error =>
205 "\nAgilent 34420A:\nunexpected value for RANGE in sub set_range. Expected values are for sense1:voltage:dc 1mV...100V and for sense2:voltage:dc 1mV...10V\n"
206 );
207 }
208 }
209 elsif ( $function =~ /^(resistance|fresistance|res|fres)$/ ) {
210 if ( $range < 0 or $range > 1e6 ) {
211 Lab::Exception::CorruptParameter->throw( error =>
212 "\nAgilent 34420A:\nunexpected value for RANGE in sub set_range. Expected values are for resistance and Fresistance mode 1...1e6 Ohm."
213 );
214 }
215 }
216 else {
217 Lab::Exception::CorruptParameter->throw( error =>
218 "\nAgilent 34420A:\nunexpected value for FUNCTION in sub set_range. Expected values are:\nvoltage:dc, voltage, volt, resistance or fresistance \n"
219 );
220 }
221  
222 # check which channel is currently
223  
224 # set range
225 if ( $range =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
226 $self->set_autorange( 'OFF', $function, $tail );
227 return $self->write(
228 sprintf( "SENSE%d:%s:RANGE %s", $channel, $function, $range ),
229 $tail
230 );
231 }
232 elsif ( $range =~ /^(AUTO|auto)$/ ) {
233 return $self->set_autorange( 'ON', $function, $tail );
234 }
235 else {
236  
237 return if ( $range == $self->get_range( { read_mode => 'cache' } ) );
238 $self->set_autorange( 'OFF', $function, $tail );
239 return $self->write(
240 sprintf( "SENSE%d:%s:RANGE %e", $channel, $function, $range ),
241 $tail
242 );
243 }
244  
245 }
246  
247 sub get_range {
248 my $self = shift;
249  
250 my ( $function, $channel, $tail )
251 = $self->_check_args( \@_, [ 'function', 'channel' ] );
252  
253 if ( not defined $channel ) {
254 $channel = $self->{channel}
255 || $self->get_channel( { read_mode => 'cache' } );
256 }
257  
258 if ( not defined $function ) {
259 $function = $self->get_function( { read_mode => 'cache' }, $tail );
260 }
261  
262 if (
263 not( $function
264 =~ /^(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
265 or $function =~ /^(resistance|fresistance|res|fres)$/ )
266 ) {
267 Lab::Exception::CorruptParameter->throw( error =>
268 "\nAgilent 34420A:\nunexpected value for FUNCTION ($function) in sub get_nplc. Expected values are:\nvoltage:dc, resistance or Fresistance --> to set both input channels\nsense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\nsense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n"
269 );
270 }
271 if ( $channel != 1 and $channel != 2 ) {
272 Lab::Exception::CorruptParameter->throw( error =>
273 "Unexpected value for channel. Allowed values are 1 or 2." );
274 }
275  
276 return $self->query(
277 sprintf( "SENSE%d:%s:RANGE?", $channel, $function ),
278 $tail
279 );
280  
281 }
282  
283 sub set_autorange {
284 my $self = shift;
285  
286 # any parameters given?
287 if ( not defined @_[0] ) {
288 print Lab::Exception::CorruptParameter->new(
289 error => "no values given in " . ref($self) . " \n" );
290 return;
291 }
292  
293 my ( $autorange, $function, $channel, $tail )
294 = $self->_check_args( \@_, [ 'autorange', 'function', 'channel' ] );
295  
296 if ( not defined $channel ) {
297 $channel = $self->{channel}
298 || $self->get_channel( { read_mode => 'cache' } );
299 }
300  
301 if ( not defined $function ) {
302 $function = $self->get_function( { read_mode => 'cache' }, $tail );
303 }
304  
305 $autorange =~ s/\s+//g; #remove all whitespaces
306 $autorange = "\U$autorange"
307 ; # transform all lowercase letters to uppercase letters
308 if ( $autorange =~ /ON|OFF/ ) {
309 $self->write(
310 sprintf(
311 "SENSE%d:%s:RANGE:AUTO %s",
312 $channel, $function, $autorange
313 ),
314 $tail
315 );
316 }
317 else {
318 print Lab::Exception::CorruptParameter->new( error =>
319 "unexpected value for AUTORANGE given in sub set_autorange ($autorange). expected values are 'ON' and 'OFF'.\n"
320 );
321 return;
322 }
323  
324 }
325  
326 sub get_autorange {
327 my $self = shift;
328  
329 my ( $function, $channel, $tail )
330 = $self->_check_args( \@_, [ 'function', 'channel' ] );
331  
332 if ( not defined $channel ) {
333 $channel = $self->{channel}
334 || $self->get_channel( { read_mode => 'cache' } );
335 }
336  
337 if ( not defined $function ) {
338 $function = $self->get_function( { read_mode => 'cache' }, $tail );
339 }
340  
341 $self->query(
342 sprintf( "SENSE%d:%s:RANGE:AUTO?", $channel, $function ),
343 $tail
344 );
345  
346 }
347  
348 sub set_nplc { # basic
349 my $self = shift;
350  
351 # any parameters given?
352 if ( not defined @_[0] ) {
353 print Lab::Exception::CorruptParameter->new(
354 error => "no values given in " . ref($self) . " \n" );
355 return;
356 }
357  
358 # Agilent 34420A:\n
359 # Expected values for function are:\n
360 # voltage:dc, voltage:dc:ratio, voltage:dc:difference, resistance or Fresistance --> to set both input channels\n
361 # sense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\n
362 # sense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n
363 #
364 # Agilent 34420A:
365 # unexpected value for NPLC in sub set_nplc.
366 # Expected values are between 0.02 ... 200 power-line-cycles (50Hz).
367  
368 my ( $nplc, $function, $tail )
369 = $self->_check_args( \@_, [ 'nplc', 'function' ] );
370  
371 return if ( $nplc == $self->get_nplc( { read_mode => 'cache' } ) );
372  
373 if ( not defined $function ) {
374 $function = $self->get_function( { read_mode => 'cache' }, $tail );
375 }
376  
377 $function =~ s/\s+//g; #remove all whitespaces
378 $function = "\L$function"
379 ; # transform all uppercase letters to lowercase letters
380 if ( $function =~ /AC|ac/ ) {
381 warn "WARNING: cannot set nplc for "
382 . $self->get_id($tail)
383 . " in ac measurement mode.";
384 return;
385 }
386  
387 if ( $function
388 =~ /^(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
389 or $function =~ /^(resistance|fresistance|res|fres)$/ ) {
390 if ( $nplc >= 0.02 and $nplc <= 200 ) #
391 {
392 return $self->write(
393 sprintf( "%s:NPLC %.3f", $function, $nplc ),
394 $tail
395 );
396 }
397 elsif ( $nplc =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
398 $self->write( sprintf( "%s:NPLC %s", $function, $nplc ), $tail );
399 $self->get_resolution();
400 }
401 else {
402 Lab::Exception::CorruptParameter->throw( error =>
403 "\nAgilent 34420A:\nunexpected value for NPLC in sub set_nplc. Expected values are between 0.02 ... 200 power-line-cycles (50Hz)."
404 );
405 }
406 }
407 else {
408 Lab::Exception::CorruptParameter->throw( error =>
409 "\nAgilent 34420A:\nunexpected value for FUNCTION in sub set_nplc. Expected values are:\nvoltage:dc, resistance or Fresistance --> to set both input channels\nsense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\nsense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n"
410 );
411 }
412  
413 }
414  
415 sub get_nplc { # basic
416 my $self = shift;
417  
418 my ( $function, $tail ) = $self->_check_args( \@_, ['function'] );
419  
420 if ( not defined $function ) {
421 $function = $self->get_function( { read_mode => 'cache' } );
422 }
423  
424 if (
425 (
426 $function
427 =~ /^(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
428 or $function =~ /^(resistance|fresistance|res|fres)$/
429 )
430 ) {
431 return $self->query( sprintf( "%s:NPLC?", $function ) );
432 }
433 else {
434 Lab::Exception::CorruptParameter->throw( error =>
435 "\nAgilent 34420A:\nunexpected value for FUNCTION ($function) in sub get_nplc. Expected values are:\nvoltage:dc, resistance or Fresistance --> to set both input channels\nsense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\nsense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n"
436 );
437 }
438 }
439  
440 sub set_resolution { # basic
441  
442 my $self = shift;
443  
444 # any parameters given?
445 if ( not defined @_[0] ) {
446 print Lab::Exception::CorruptParameter->new(
447 error => "no values given in " . ref($self) . " \n" );
448 return;
449 }
450  
451 # Agilent 34420A:\n
452 # Expected values for FUNCTION are:\n
453 # voltage:dc, voltage:dc:ratio, voltage:dc:difference, resistance or Fresistance --> to set both input channels\n
454 # sense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\n
455 # sense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n
456 #
457 # Agilent 34420A:
458 # unexpected value for resOLUTION
459 # Expected values are between 0.0001xRANGE ... 0.00000022xRANGE
460  
461 my ( $resolution, $function, $tail )
462 = $self->_check_args( \@_, [ 'resolution', 'function' ] );
463  
464 return
465 if (
466 $resolution == $self->get_resolution( { read_mode => 'cache' } ) );
467  
468 if ( not defined $function ) {
469 $function = $self->get_function( { read_mode => 'cache' }, $tail );
470 }
471  
472 $function =~ s/\s+//g; #remove all whitespaces
473 $function = "\L$function"
474 ; # transform all uppercase letters to lowercase letters
475 if ( $function =~ /AC|ac/ ) {
476 warn "WARNING: cannot set resolution for "
477 . $self->get_id()
478 . " in ac measurement mode.";
479 return;
480 }
481  
482 # get resolution:
483 if ( $function
484 =~ /^(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
485 or $function =~ /^(resistance|fresistance|res|fres)$/ ) {
486 my $range
487 = $self->get_range( $function, { read_mode => 'cache' }, $tail );
488 $self->set_autorange( 'OFF', $function )
489 ; # switch off autorange function if activated.
490  
491 if ( $resolution >= 0.0001 * $range
492 and $resolution <= 0.00000022 * $range ) {
493 $self->write(
494 sprintf( "%s:res %.3f", $function, $resolution ),
495 $tail
496 );
497 }
498 elsif ( $resolution =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
499 $self->write(
500 sprintf( "%s:res %s", $function, $resolution ),
501 $tail
502 );
503 }
504 else {
505 Lab::Exception::CorruptParameter->throw( error =>
506 "\nAgilent 34420A:\nunexpected value for resOLUTION in sub set_resolution. Expected values are between 0.0001xRANGE ... 0.0000022xRANGE."
507 );
508 }
509 $self->get_nplc();
510 }
511 else {
512 Lab::Exception::CorruptParameter->throw( error =>
513 "\nAgilent 34420A:\nunexpected value for FUNCTION in sub set_resolution. Expected values are:\nvoltage:dc, resistance or Fresistance --> to set both input channels\nsense1:voltage:dc, sense1:voltage:dc:ratio, sense1:voltage:dc:difference, sense1:resistance or sense1:Fresistance --> to set input channel 1 only\nsense2:voltage:dc, sense2:voltage:dc:ratio, sense2:voltage:dc:difference, sense2:resistance or sense2:Fresistance --> to set input channel 2 only\n"
514 );
515 }
516  
517 }
518  
519 sub get_resolution { # basic
520 my $self = shift;
521  
522 my ( $function, $tail ) = $self->_check_args( \@_, ['function'] );
523  
524 if ( not defined $function ) {
525 $function = $self->get_function( { read_mode => 'cache' } );
526 }
527  
528 if (
529 (
530 $function
531 =~ /^(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
532 or $function =~ /^(resistance|fresistance|res|fres)$/
533 )
534 ) {
535 return $self->query( sprintf( "%s:resOLUTION?", $function ), $tail );
536 }
537 }
538  
539 sub set_channel { # basic
540 my $self = shift;
541 my ( $terminal, $tail ) = $self->_check_args( \@_, ['channel'] );
542  
543 my $function = $self->get_function( { read_mode => 'cache' }, $tail );
544 $function =~ s/\s+//g; #remove all whitespaces
545 $function = "\L$function"
546 ; # transform all uppercase letters to lowercase letters
547  
548 if ( $function
549 =~ /(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt)/
550 ) {
551 if ( $self->get_channel( { read_mode => 'cache' }, $tail )
552 == $terminal ) {
553 return;
554 }
555 elsif ( ( $terminal == 1 or $terminal == 2 ) ) {
556 $self->write(
557 sprintf( "ROUTE:TERMINALS FRONT%d", $terminal ),
558 $tail
559 );
560 }
561 else {
562 Lab::Exception::CorruptParameter->throw( error =>
563 "\nAgilent 34420A:\nunexpected value for CHANNEL in sub set_channel. Expected values are:\n 1 --> for channel 1\n 2 --> for channel 2\n"
564 );
565 }
566 }
567 else {
568 Lab::Exception::CorruptParameter->throw(
569 error => "Can't route CHANNEL in resistance/Fresistance mode." );
570 }
571  
572 }
573  
574 sub get_channel {
575 my $self = shift;
576 my ($tail) = $self->_check_args( \@_ );
577  
578 my $function = $self->get_function( { read_mode => 'cache' }, $tail );
579 $function =~ s/\s+//g; #remove all whitespaces
580 $function = "\L$function"
581 ; # transform all uppercase letters to lowercase letters
582  
583 if ( $function
584 =~ /(voltage:dc|voltage|volt:dc|volt|sense1:voltage:dc|sense1:voltage|sense1:volt:dc|sense1:volt|sense2:voltage:dc|sense2:voltage|sense2:volt:dc|sense2:volt)/
585 ) {
586 my $result = $self->query("ROUTE:TERMINALS?");
587 if ( $result eq "FRON" ) {
588 return 1;
589 }
590 elsif ( $result eq "FRON2" ) {
591 return 2;
592 }
593 }
594 else {
595 Lab::Exception::CorruptParameter->throw(
596 error => "Can't route CHANNEL in resistance/Fresistance mode." );
597 }
598  
599 }
600  
601 sub set_averaging { # to be implemented
602 my $self = shift;
603 my $value = shift;
604  
605 #INPut:FILTer
606 #:STATe {OFF|ON}
607 #:TYPE {ANAlog | DIGital | BOTH}
608 #:DIGital:resPonse {SLOW|MEDium|FAST}
609 #:DIGital:PRECharge {ON | OFF}
610 }
611  
612 # ----------------------------- TAKE DATA ---------------------------------------------------------
613  
614 sub get_value { # basic
615 my $self = shift;
616  
617 # parameter == hash??
618 my ( $channel, $function, $range, $nplc, $resolution, $tail )
619 = $self->_check_args(
620 \@_,
621 [ 'channel', 'function', 'range', 'nplc', 'resolution' ]
622 );
623  
624 my ( $int_time, $int_mode );
625  
626 if ( defined $nplc ) {
627 $int_mode = "nplc";
628 $int_time = $nplc;
629 }
630 elsif ( defined $resolution ) {
631 $int_mode = "res";
632 $int_time = $resolution;
633 }
634 elsif ( defined $resolution and defined $nplc ) {
635 Lab::Exception::CorruptParameter->throw(
636 error => "NPLC and resolution may not be defined concurrently." );
637 }
638  
639 if ( not defined $channel ) {
640 $channel = $self->{channel}
641 || $self->get_channel( { read_mode => 'cache' } );
642 }
643  
644 $self->set_channel( $channel, $tail );
645  
646 if ( not defined $function ) {
647 $function = $self->get_function( { read_mode => 'cache' }, $tail );
648 }
649  
650 $function =~ s/\s+//g; #remove all whitespaces
651 $function = "\L$function"
652 ; # transform all uppercase letters to lowercase letters
653  
654 if (
655 not $function
656 =~ /^(voltage:dc|voltage|volt:dc|volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff|resistance|resisitance|res|res|Fresistance|fresistance|fres|fres)$/
657 ) {
658 Lab::Exception::CorruptParameter->throw( error =>
659 "\nAgilent 34420A:\nunexpected value for FUNCTION in sub get_value. Expected values are voltage:dc|voltage|volt:dc|volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff|resistance|resisitance|res|res|Fresistance|fresistance|fres|fres"
660 );
661 }
662  
663 # check input parameter
664  
665 if ( not defined $range ) {
666 $range = $self->get_range( { read_mode => 'cache' }, $tail );
667 }
668  
669 $range =~ s/\s+//g; #remove all whitespaces
670 if ( $function =~ $function
671 =~ /^(voltage:dc|voltage|volt:dc|volt|voltage:dc:ratio|voltage:ratio|volt:dc:ratio|volt:ratio|voltage:dc:diff|voltage:diff|volt:dc:diff|volt:diff)$/
672 ) {
673 if ( abs($range) > 100
674 and not $range =~ /^(MIN|min|MAX|max|DEF|def|AUTO|auto)$/ ) {
675 Lab::Exception::CorruptParameter->throw( error =>
676 "\nAgilent 34420A:\nunexpected value for RANGE in sub get_value. Expected values are for voltage and resistance mode 0.1...100V or 0...1e6 Ohms respectivly"
677 );
678 }
679 }
680 elsif ( $function
681 =~ /^(resistance|resisitance|res|res|Fresistance|fresistance|fres|fres)$/
682 ) {
683 if ( abs($range) > 1e6
684 and not $range =~ /^(MIN|min|MAX|max|DEF|def|AUTO|auto)$/ ) {
685 Lab::Exception::CorruptParameter->throw( error =>
686 "\nAgilent 34420A:\nunexpected value for RANGE in sub get_value. Expected values are for voltage and resistance mode 0.1...100V or 0...1e6 Ohms respectivly"
687 );
688 }
689 }
690  
691 $int_mode =~ s/\s+//g; #remove all whitespaces
692 $int_mode = "\L$int_mode"
693 ; # transform all uppercase letters to lowercase letters
694 if ( $int_mode =~ /resolution|res/ ) {
695 $int_mode = "res";
696 if ( $int_time < 0.22e-6 * $range
697 and not $int_time =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
698 Lab::Exception::CorruptParameter->throw( error =>
699 "unexpected value for RESOLUTION in sub get_value. Expected values are from 0.22e-6xRANGE ... 30e-6xRANGE."
700 );
701 }
702 }
703 elsif ( $int_mode eq "nplc" ) {
704 if ( ( $int_time < 0.02 or $int_time > 200 )
705 and not $int_time =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
706 Lab::Exception::CorruptParameter->throw( error =>
707 "unexpected value for NPLC in sub get_value. Expected values are from 0.02 ... 200."
708 );
709 }
710 }
711 elsif ( defined $int_time ) {
712 $int_mode = 'nplc';
713 if ( ( $int_time < 0.02 or $int_time > 200 )
714 and not $int_time =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
715 Lab::Exception::CorruptParameter->throw( error =>
716 "unexpected value for NPLC in sub get_value. Expected values are from 0.02 ... 200."
717 );
718 }
719 }
720  
721 $self->set_function( $function, $tail );
722  
723 $self->set_range($range);
724  
725 if ( $int_mode eq 'res' ) {
726 $self->set_resolution($int_time);
727  
728 #$self->write( ":SENS:$function:ZERO:AUTO OFF; RES $int_time");
729 }
730 elsif ( $int_mode eq 'nplc' ) {
731 $self->set_nplc($int_time);
732  
733 #$self->write( ":SENS:$function:ZERO:AUTO OFF; NPLC $int_time");
734 }
735  
736 return $self->request( ":read?", $tail );
737  
738 }
739  
740 sub config_measurement { # basic
741 my $self = shift;
742 my ( $function, $nop, $nplc, $range, $trigger, $tail )
743 = $self->_check_args(
744 \@_,
745 [ 'function', 'nop', 'nplc', 'range', 'trigger' ]
746 );
747  
748 # check input data
749 if ( not defined $trigger ) {
750 $trigger = 'BUS';
751 }
752 if ( not defined $range ) {
753 $range = 'DEF';
754 }
755 if ( not defined $nplc ) {
756 $nplc = 2;
757 }
758 if ( not defined $nop ) {
759 Lab::Exception::CorruptParameter->throw( error =>
760 "too view arguments given in sub config_measurement. Expected arguments are FUNCTION, #POINTS, NPLC, <RANGE>, <TRIGGERSOURCE>"
761 );
762 }
763  
764 print "--------------------------------------\n";
765 print "Agilent34410A: sub config_measurement:\n";
766  
767 # clear buffer
768 my $points = $self->query("DATA:POINTS?");
769 if ( $points > 0 ) {
770 $points = $self->{vi}->LongQuery("DATA:FETCH?");
771 }
772  
773 # set function
774  
775 print "set_function: " . $self->set_function( $function, $tail ) . "\n";
776  
777 # set range
778 print "set_range: " . $self->set_range( $range, $function, $tail ) . "\n";
779  
780 # set nplc/tc
781 print "set_nplc: " . $self->set_nplc( $nplc, $function, $tail ) . "\n";
782 my $time = $nop * $nplc / 50;
783 print "TIME for measurement trace => $time\n";
784  
785 # set some status registes
786 $self->write("*CLS");
787 $self->write("*ESE 128");
788  
789 # triggering
790 print "set Trigger Source: " . $self->_set_triggersource("BUS") . "\n";
791 print "set Trigger Count: " . $self->_set_triggercount(1) . "\n";
792 print "set Trigger Delay: " . $self->_set_triggerdelay("MIN") . "\n";
793  
794 print "set Sample Count: " . $self->_set_samplecount($nop) . "\n";
795 print "init()\n";
796 $self->write("INIT");
797 usleep(50e3)
798 ; # wait 50ms; it takes 20ms until the WAIT-FOR-TRIGGER-STATE has been setteled;
799  
800 print "Agilent34410A: sub config_measurement complete\n";
801 print "--------------------------------------\n";
802  
803 return $time;
804 }
805  
806 sub trg { # basic
807 my $self = shift;
808 $self->write("*TRG");
809 }
810  
811 sub abort
812 { # doesn't work, because Agilent 34420A doesn't accept any new SPCI COMMAND until last COMMAND has been completed.
813 my $self = shift;
814  
815 #$self->write("ABOR"); # ??
816 #$self->write("SYSTEM:LOCAL");
817 }
818  
819 sub get_data { # basic
820 my $self = shift;
821 my $data;
822 my @data;
823  
824 # wait until data are available
825 $self->write("*STB?");
826 while (1) {
827 eval '$self->read(100)';
828 if ( index( $@, "Error while reading:" ) >= 0 ) {
829 print "waiting for data ...\n";
830 }
831 else { last; }
832 }
833  
834 # read data
835 $data = $self->{vi}->LongQuery("FETC?");
836 chomp $data;
837 @data = split( ",", $data );
838 return @data;
839 }
840  
841 # ------------------ TRIGGER and SAMPLE settings ---------------------- #
842  
843 sub _set_triggersource { # internal only
844 my $self = shift;
845 my ( $source, $tail ) = $self->_check_args( \@_, ['value'] );
846  
847 if ( not defined $source ) {
848 $source = $self->query( sprintf("TRIGGER:SOURCE?") );
849 chomp($source);
850 return $source;
851 }
852  
853 if ( $source =~ /^(IMM|imm|EXT|ext|BUS|bus)$/ ) {
854 Lab::Exception::CorruptParameter->throw( error =>
855 "\nAgilent 34420A:\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"
856 );
857 }
858 $source
859 = $self->query( sprintf( "TRIGGER:SOURCE %s; SOURCE?", $source ) );
860  
861 return $source;
862 }
863  
864 sub _set_triggercount { # internal only
865 my $self = shift;
866 my ( $count, $tail ) = $self->_check_args( \@_, ['value'] );
867  
868 if ( not defined $count ) {
869 $count = $self->query( sprintf("TRIGGER:COUNT?") );
870 chomp($count);
871 return $count;
872 }
873  
874 if ( $count < 0 or $count >= 50000 ) {
875 Lab::Exception::CorruptParameter->throw( error =>
876 "\nAgilent 34420A:\nunexpected value for COUNT in sub _set_triggercount. Expected values are between 1 ... 50.000\n"
877 );
878 }
879  
880 return $self->query( sprintf( "TRIGGER:COUNT %d; COUNT?", $count ) );
881 }
882  
883 sub _set_triggerdelay { # internal only
884 my $self = shift;
885 my ( $delay, $tail ) = $self->_check_args( \@_, ['value'] );
886  
887 if ( not defined $delay ) {
888 $delay = $self->query( sprintf("TRIGGER:DELAY?") );
889 chomp($delay);
890 return $delay;
891 }
892  
893 if ( ( $delay < 0 or $delay > 3600 )
894 and $delay =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
895 Lab::Exception::CorruptParameter->throw( error =>
896 "\nAgilent 34420A:\nunexpected value for DELAY in sub _set_triggerdelay. Expected values are between 1 ... 3600, or 'MIN = 0', 'MAX = 3600' or 'AUTO'\n"
897 );
898 }
899  
900 if ( $delay =~ /^(AUTO|auto)$/ ) {
901 $self->query( sprintf( "TRIGGER:DELAY:AUTO ON; AUTO?", $delay ) );
902 return "AUTO ON";
903 }
904 elsif ( $delay =~ /^(MIN|min|MAX|max|DEF|def)$/ ) {
905 return $self->query( sprintf( "TRIGGER:DELAY %s; DELAY?", $delay ) );
906 }
907 else {
908 return $self->query(
909 sprintf( "TRIGGER:DELAY %.5f; DELAY?", $delay ) );
910 }
911 }
912  
913 sub _set_samplecount { # internal only
914 my $self = shift;
915 my ( $count, $tail ) = $self->_check_args( \@_, ['value'] );
916  
917 if ( not defined $count ) {
918 $count = $self->query( sprintf("SAMPLE:COUNT?") );
919 chomp($count);
920 return $count;
921 }
922  
923 elsif ( $count < 0 or $count > 1024 ) {
924 Lab::Exception::CorruptParameter->throw( error =>
925 "\nAgilent 34420A:\nunexpected value for COUNT in sub _set_samplecount. Expected values are between 1 ... 1024\n"
926 );
927 }
928 else {
929 return $self->query( sprintf( "SAMPLE:COUNT %d; COUNT?", $count ) );
930 }
931 }
932  
933 # ------------------------------- DISPLAY and BEEPER --------------------------------------------
934  
935 sub display_text { # basic
936 my $self = shift;
937 my ( $text, $tail ) = $self->_check_args( \@_, ['text'] );
938  
939 if ($text) {
940 $self->write(qq(DISPlay:TEXT "$text"));
941 }
942 else {
943 chomp( $text = $self->query(qq(DISPlay:TEXT?)) );
944 $text =~ s/\"//g;
945 }
946 return $text;
947 }
948  
949 sub display_on { # basic
950 my $self = shift;
951 $self->write("DISPLAY ON");
952 }
953  
954 sub display_off { # basic
955 my $self = shift;
956 $self->write("DISPLAY OFF");
957 }
958  
959 sub display_clear { # basic
960 my $self = shift;
961 $self->write("DISPLAY:TEXT:CLEAR");
962 }
963  
964 sub beep { # basic
965 my $self = shift;
966 $self->write("SYSTEM:BEEPER");
967 }
968  
969 1;
970  
971 __END__