Instrument/HP3458A.pm untested

filter: Statementstmt 0.0% 0 / 239 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 506 CC 49 SCAR 38.9
CC
49
Cov
0%
configure_voltage_dc_trigger_highspeed
182.0
configure_voltage_dc
90.0
configure_voltage_dc_trigger
42.0
CRAP 48.8
1 package Lab::Instrument::HP3458A;
2 #ABSTRACT: Agilent 3458A Multimeter
3 $Lab::Instrument::HP3458A::VERSION = '3.899';
4 use v5.20;
5  
6 use strict;
7 use Lab::Instrument;
8 use Lab::Instrument::Multimeter;
9 use Time::HiRes qw (usleep sleep);
10  
11 our @ISA = ("Lab::Instrument::Multimeter");
12  
13 our %fields = (
14 supported_connections => ['GPIB'],
15  
16 # default settings for the supported connections
17 connection_settings => {
18 gpib_board => 0,
19 gpib_address => undef,
20 },
21  
22 device_settings => {
23 pl_freq => 50,
24 },
25  
26 device_cache => {
27 autozero => undef,
28 },
29  
30 );
31  
32 sub new {
33 my $proto = shift;
34 my $class = ref($proto) || $proto;
35 my $self = $class->SUPER::new(@_);
36 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
37  
38 return $self;
39 }
40  
41 sub _device_init {
42 my $self = shift;
43  
44 #$self->connection()->SetTermChar("\r\n");
45 #$self->connection()->EnableTermChar(1);
46 #print "hallo\n";
47 $self->write( "END 2", error_check => 1 )
48 ; # or ERRSTR? and other queries will time out, unless using a line/message end character
49 $self->write( 'TARM AUTO', error_check => 1 ); # keep measuring
50 $self->write( 'TRIG AUTO', error_check => 1 ); # keep measuring
51 $self->write( 'NRDGS 1,AUTO', error_check => 1 ); # keep measuring
52 }
53  
54 #
55 # utility methods
56 #
57  
58 sub configure_voltage_dc {
59 my $self = shift;
60  
61 my ( $range, $tint ) = $self->_check_args( \@_, [ 'range', 'tint' ] );
62  
63 my $range_cmd = "FUNC DCV ";
64  
65 if ( $range eq 'AUTO' || !defined($range) ) {
66 $range_cmd = 'ARANGE ON';
67 }
68 elsif ( $range =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
69 $range_cmd = sprintf( "FUNC DCV %e", abs($range) );
70 }
71 elsif ( $range !~ /^(MIN|MAX)$/ ) {
72 Lab::Exception::CorruptParameter->throw( error =>
73 "Range has to be set to a decimal value or 'AUTO', 'MIN' or 'MAX' in "
74 . ( caller(0) )[3]
75 . "\n" );
76 }
77  
78 if ( !defined($tint) || $tint eq 'DEFAULT' ) {
79 $tint = 10;
80 }
81 elsif ( $tint =~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
82 && ( ( $tint >= 0 && $tint <= 1000 ) || $tint == -1 ) ) {
83  
84 # Convert seconds to PLC (power line cycles)
85 $tint *= $self->pl_freq();
86 }
87 elsif ( $tint =~ /^MIN$/ ) {
88 $tint = 0;
89 }
90 elsif ( $tint =~ /^MAX$/ ) {
91 $tint = 1000;
92 }
93 elsif ( $tint !~ /^(MIN|MAX)$/ ) {
94 Lab::Exception::CorruptParameter->throw( error =>
95 "Integration time has to be set to a positive value or 'AUTO', 'MIN' or 'MAX' in "
96 . ( caller(0) )[3]
97 . "\n" );
98 }
99  
100 # do it
101 $self->write( $range_cmd, error_check => 1 );
102 $self->write( "NPLC ${tint}", error_check => 1 );
103  
104 #$self->write( "NPLC ${tint}", { error_check=>1 });
105 }
106  
107 sub configure_voltage_dc_trigger {
108 my $self = shift;
109  
110 my ( $range, $tint, $count, $delay )
111 = $self->_check_args( \@_, [ 'range', 'tint', 'count', 'delay' ] );
112  
113 $count = 1 if !defined($count);
114 Lab::Exception::CorruptParameter->throw(
115 error => "Sample count has to be an integer between 1 and 512\n" )
116 if ( $count !~ /^[0-9]*$/ || $count < 1 || $count > 16777215 );
117  
118 $delay = 0 if !defined($delay);
119 Lab::Exception::CorruptParameter->throw(
120 error => "Trigger delay has to be a positive decimal value\n" )
121 if ( $count !~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ );
122  
123 $self->configure_voltage_dc( $range, $tint );
124  
125 #$self->write( "PRESET NORM" );
126 if ( $count > 1 ) {
127 $self->write( "INBUF ON", error_check => 1 );
128 }
129 else {
130 $self->write( "INBUF OFF", error_check => 1 );
131 }
132 $self->write( "TARM AUTO", error_check => 1 );
133 $self->write( "TRIG HOLD", error_check => 1 );
134 $self->write( "NRDGS $count, AUTO", error_check => 1 );
135 $self->write( "TIMER $delay", error_check => 1 );
136  
137 #$self->write( "TRIG:DELay:AUTO OFF");
138 }
139  
140 sub configure_voltage_dc_trigger_highspeed {
141 my $self = shift;
142 my $range = shift || 10; # in V, or "AUTO", "MIN", "MAX"
143 my $tint = shift
144 || 1.4e-6
145 ; # integration time in sec, "DEFAULT", "MIN", "MAX". Default of 1.4e-6 is the highest possible value for 100kHz sampling.
146 my $count = shift || 10000;
147 my $delay = shift; # in seconds, 'MIN'
148  
149 if ( $range eq 'AUTO' || !defined($range) ) {
150 $range = 'AUTO';
151 }
152 elsif ( $range =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
153  
154 #$range = sprintf("%e",abs($range));
155 }
156 elsif ( $range !~ /^(MIN|MAX)$/ ) {
157 Lab::Exception::CorruptParameter->throw( error =>
158 "Range has to be set to a decimal value or 'AUTO', 'MIN' or 'MAX' in "
159 . ( caller(0) )[3]
160 . "\n" );
161 }
162  
163 if ( $tint eq 'DEFAULT' || !defined($tint) ) {
164 $tint = 1.4e-6;
165 }
166 elsif ( $tint =~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
167 && ( ( $tint >= 0 && $tint <= 1000 ) || $tint == -1 ) ) {
168  
169 # Convert seconds to PLC (power line cycles)
170 #$tint*=$self->pl_freq();
171 }
172 elsif ( $tint =~ /^MIN$/ ) {
173 $tint = 0;
174 }
175 elsif ( $tint =~ /^MAX$/ ) {
176 $tint = 1000;
177 }
178 elsif ( $tint !~ /^(MIN|MAX)$/ ) {
179 Lab::Exception::CorruptParameter->throw( error =>
180 "Integration time has to be set to a positive value or 'AUTO', 'MIN' or 'MAX' in "
181 . ( caller(0) )[3]
182 . "\n" );
183 }
184  
185 $count = 1 if !defined($count);
186 Lab::Exception::CorruptParameter->throw(
187 error => "Sample count has to be an integer between 1 and 512\n" )
188 if ( $count !~ /^[0-9]*$/ || $count < 1 || $count > 16777215 );
189  
190 $delay = 0 if !defined($delay);
191 Lab::Exception::CorruptParameter->throw(
192 error => "Trigger delay has to be a positive decimal value\n" )
193 if ( $count !~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ );
194  
195 $self->write( "PRESET FAST", error_check => 1 );
196 $self->write( "TARM HOLD", error_check => 1 );
197 $self->write( "APER " . $tint, error_check => 1 );
198 $self->write( "MFORMAT SINT", error_check => 1 );
199 $self->write( "OFORMAT SINT", error_check => 1 );
200 $self->write( "MEM FIFO", error_check => 1 );
201 $self->write( "NRDGS $count, AUTO", error_check => 1 );
202  
203 #$self->write( "TIMER $delay") if defined($delay);
204  
205 }
206  
207 sub triggered_read {
208 my $self = shift;
209 my $args
210 = scalar(@_) % 2 == 0
211 ? {@_}
212 : ( ref( $_[0] ) eq 'HASH' ? $_[0] : undef );
213 Lab::Exception::CorruptParameter->throw("Illegal parameter hash given!\n")
214 if !defined($args);
215  
216 $args->{'timeout'} = $args->{'timeout'} || undef;
217  
218 my $value = $self->query( "TRIG SGL", $args );
219  
220 chomp $value;
221  
222 my @valarray = split( "\n", $value );
223  
224 return @valarray;
225 }
226  
227 sub triggered_read_raw {
228 my $self = shift;
229 my $args
230 = scalar(@_) % 2 == 0
231 ? {@_}
232 : ( ref( $_[0] ) eq 'HASH' ? $_[0] : undef );
233 Lab::Exception::CorruptParameter->throw("Illegal parameter hash given!\n")
234 if !defined($args);
235  
236 my $read_until_length = $args->{'read_until_length'};
237 my $value = '';
238 my $fragment = undef;
239  
240 {
241 use bytes;
242 $value = $self->query( "TARM SGL", $args );
243 my $tmp = length($value);
244 while ( defined $read_until_length
245 && length($value) < $read_until_length ) {
246 $value .= $self->read($args);
247 }
248 }
249  
250 return $value;
251 }
252  
253 sub decode_SINT {
254 use bytes;
255 my $self = shift;
256 my $bytestring = shift;
257 my $iscale = shift || $self->query('ISCALE?');
258  
259 my @values = split( //, $bytestring );
260 my $ival = 0;
261 my $val_revb = 0;
262 my $tbyte = 0;
263 my $value = 0;
264 my @result_list = ();
265 my $i = 0;
266 for ( my $v = 0; $v < $#values; $v += 2 ) {
267 $ival = unpack( 'S', join( '', $values[$v], $values[ $v + 1 ] ) );
268  
269 # flipping the bytes to MSB,...,LSB
270 $val_revb = 0;
271 for ( $i = 0; $i < 2; $i++ ) {
272 $val_revb = $val_revb
273 | ( ( $ival >> $i * 8 & 0x000000FF ) << ( ( 1 - $i ) * 8 ) );
274 }
275  
276 my $decval = 0;
277 my $msb = ( $val_revb >> 15 ) & 0x0001;
278 $decval = $msb == 0 ? 0 : -1 * ( 2**15 );
279 for ( $i = 14; $i >= 0; $i-- ) {
280 $decval += ( ( ( $val_revb >> $i ) & 0x0001 ) * 2 )**$i;
281 }
282 push( @result_list, $decval * $iscale );
283 }
284 return @result_list;
285 }
286  
287 sub set_oformat {
288 my $self = shift;
289 my $format = shift;
290  
291 if ( $format !~ /^\s*(ASCII|1|SINT|2|DINT|3|SREAD|4|DREAL|5)\s*$/ ) {
292 Lab::Exception::CorruptParameter->throw("Invalid OFORMAT specified.");
293 }
294 $format = $1;
295  
296 $self->write("OFORMAT $1");
297  
298 $self->check_errors();
299 }
300  
301 sub get_oformat {
302 my $self = shift;
303 my $args
304 = scalar(@_) % 2 == 0
305 ? {@_}
306 : ( ref( $_[0] ) eq 'HASH' ? $_[0] : undef );
307 Lab::Exception::CorruptParameter->throw("Illegal parameter hash given!\n")
308 if !defined($args);
309  
310 if ( $args->{direct_read} || !defined $self->device_cache()->{oformat} ) {
311 return $self->device_cache()->{oformat}
312 = $self->query( 'OFORMAT?', $args );
313 }
314 else {
315 return $self->device_cache()->{oformat};
316 }
317 }
318  
319 sub get_autozero {
320 my $self = shift;
321  
322 return $self->device_cache()->{autozero}
323 = $self->query( 'AZERO?', @_, error_check => 0 );
324 }
325  
326 sub set_autozero {
327 my $self = shift;
328 my $enable = shift;
329  
330 my $command = "";
331 my $cval = undef;
332  
333 if ( $enable =~ /^ONCE$/i ) {
334 $command = "AZERO ONCE";
335 $cval = 0;
336 }
337 elsif ( $enable =~ /^(ON|1)$/i ) {
338 $command = "AZERO ON";
339 $cval = 1;
340 }
341 elsif ( $enable =~ /^(OFF|0)$/i ) {
342 $command = "AZERO OFF";
343 $cval = 0;
344 }
345 else {
346 Lab::Exception::CorruptParameter->throw( error => ( caller(0) )[3]
347 . " can be set to 'ON'/1, 'OFF'/0 or 'ONCE'. Received '${enable}'\n"
348 );
349 }
350 $self->write( $command, error_check => 1, @_ );
351  
352 $self->device_cache()->{autozero} = $cval;
353 }
354  
355 sub get_voltage_dc {
356 my $self = shift;
357  
358 $self->write( "DCV AUTO", @_ );
359 $self->write( "TARM SGL", @_ );
360 return $self->read(@_);
361 }
362  
363 sub set_nplc {
364 my $self = shift;
365 my $n = shift;
366  
367 $self->write( "NPLC $n", @_ );
368 }
369  
370 sub selftest {
371 my $self = shift;
372  
373 $self->write( "TEST", @_ );
374 }
375  
376 sub autocalibration {
377 my $self = shift;
378 my $mode = shift;
379  
380 if ( $mode !~ /^(ALL|0|DCV|1|DIG|2|OHMS|4)$/i ) {
381 Lab::Exception::CorruptParameter->throw(
382 "preset(): Illegal preset mode given: $mode\n");
383 }
384  
385 $self->write( "ACAL \U$mode\E", @_ );
386 }
387  
388 sub reset {
389 my $self = shift;
390  
391 $self->write( "PRESET NORM", @_ );
392 }
393  
394 sub set_display_state {
395 my $self = shift;
396 my $value = shift;
397  
398 if ( $value == 1 || $value =~ /on/i ) {
399 $self->write( "DISP ON", @_ );
400 }
401 elsif ( $value == 0 || $value =~ /off/i ) {
402 $self->write( "DISP OFF", @_ );
403 }
404 else {
405 Lab::Exception::CorruptParameter->throw(
406 "set_display_state(): Illegal parameter.\n");
407 }
408 }
409  
410 sub display_clear {
411 my $self = shift;
412  
413 $self->write( "DISP CLR", @_ );
414 }
415  
416 sub set_display_text {
417 my $self = shift;
418 my $text = shift;
419 if ( $text
420 !~ /^[A-Za-z0-9\ \!\#\$\%\&\'\(\)\^\\\/\@\;\:\[\]\,\.\+\-\=\<\>\?\_]*$/
421 ) { # characters allowed by the 3458A
422 Lab::Exception::CorruptParameter->throw(
423 "set_display_text(): Illegal characters in given text.\n");
424 }
425 $self->write("DISP MSG,\"$text\"");
426  
427 $self->check_errors();
428 }
429  
430 sub beep {
431  
432 # It beeps!
433 my $self = shift;
434 $self->write( "BEEP", @_ );
435 }
436  
437 sub get_status {
438 my $self = shift;
439 my $request = shift;
440 my $status = {};
441 (
442 $status->{PRG_COMPLETE}, $status->{LIMIT_EXCEEDED},
443 $status->{SRQ_EXECUTED}, $status->{POWER_ON},
444 $status->{READY}, $status->{ERROR},
445 $status->{SRQ}, $status->{DATA}
446 ) = $self->connection()->serial_poll();
447 return $status->{$request} if defined $request;
448 return $status;
449 }
450  
451 sub get_error {
452 my $self = shift;
453 my $error = $self->query( "ERRSTR?", brutal => 1, @_ );
454 if ( $error !~ /0,\"NO ERROR\"/ ) {
455 if ( $error =~ /^\+?([0-9]*)\,\"?([^\"].*[^\"])\"?$/m ) {
456 return ( $1, $2 ); # ($code, $message)
457 }
458 else {
459 Lab::Exception::DeviceError->throw(
460 "Reading the error status of the device failed in "
461 . ( caller(0) )[3]
462 . ". Something's going wrong here.\n" );
463 }
464 }
465 else {
466 return (0);
467 }
468 }
469  
470 sub preset {
471  
472 # Sets HP3458A into predefined configurations
473 # 0 Fast
474 # 1 Norm
475 # 2 DIG
476 my $self = shift;
477 my $preset = shift;
478 if ( $preset !~ /^(FAST|0|NORM|1|DIG|2)$/i ) {
479 Lab::Exception::CorruptParameter->throw(
480 "preset(): Illegal preset mode given: $preset\n");
481 }
482  
483 $self->write( "PRESET \U$preset\E", @_ );
484 }
485  
486 sub get_id {
487 my $self = shift;
488 return $self->query( 'ID?', @_ );
489 }
490  
491 sub trg {
492 my $self = shift;
493 $self->write( 'TRIG SGL', @_ );
494 }
495  
496 sub get_value {
497  
498 my $self = shift;
499  
500 my $val = $self->read(@_);
501 chomp $val;
502 return $val;
503 }
504  
505 1;
506  
507 __END__