Instrument/HP34401A.pm untested

filter: Statementstmt 0.0% 0 / 203 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 393 CC 44 SCAR 39.5
CC
44
Cov
0%
configure_voltage_dc_trigger
210.0
configure_voltage_dc
72.0
autozero
30.0
CRAP 51.7
1 package Lab::Instrument::HP34401A;
2 #ABSTRACT: HP/Agilent 34401A digital multimeter
3 $Lab::Instrument::HP34401A::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 return $error;
165 }
166 }
167 else {
168 return undef;
169 }
170 }
171  
172 sub get_status {
173 my $self = shift;
174  
175 # This is to be implemented with code that queries the status bit
176  
177 my $request = shift;
178 my $status = {};
179  
180 (
181 $status->{NOT_USED1}, $status->{NOT_USED2}, $status->{NOT_USED3},
182 $status->{CORR_DATA}, $status->{MSG_AVAIL}, $status->{EVNT},
183 $status->{SRQ}, $status->{NOT_USED4}
184 ) = $self->connection()->serial_poll();
185 return $status->{$request} if defined $request;
186 return $status;
187 }
188  
189 sub set_display_state {
190 my $self = shift;
191 my $value = shift;
192  
193 if ( $value == 1 || $value =~ /on/i ) {
194 $self->write( "DISP ON", @_ );
195 }
196 elsif ( $value == 0 || $value =~ /off/i ) {
197 $self->write( "DISP OFF", @_ );
198 }
199 else {
200 Lab::Exception::CorruptParameter->throw(
201 "set_display_state(): Illegal parameter.\n");
202 }
203 }
204  
205 sub set_display_text {
206 my $self = shift;
207 my $text = shift;
208 if ( $text
209 !~ /^[A-Za-z0-9\ \!\#\$\%\&\'\(\)\^\\\/\@\;\:\[\]\,\.\+\-\=\<\>\?\_]*$/
210 ) { # characters allowed by the 3458A
211 Lab::Exception::CorruptParameter->throw(
212 "set_display_text(): Illegal characters in given text.\n");
213 }
214 $self->write("DISP:TEXT $text");
215  
216 $self->check_errors();
217 }
218  
219 sub set_range {
220 my $self = shift;
221  
222 # This is the range set function, to be implemented.
223 }
224  
225 sub reset {
226 my $self = shift;
227 $self->connection()->Write( command => "*CLS" );
228 $self->connection()->Write( command => "*RST" );
229  
230 # $self->connection()->InstrumentClear($self->instrument_handle());
231 }
232  
233 sub wait_done {
234 my $self = shift;
235  
236 # wait until currently running program is finished.
237  
238 while ( !$self->get_status()->{"EVNT"} ) {
239 sleep 1;
240 }
241  
242 }
243  
244 sub autozero {
245 my $self = shift;
246 my $enable = shift;
247 my $az_status = undef;
248 my $command = "";
249  
250 if ( !defined $enable ) {
251  
252 # read autozero setting
253 $command = "ZERO:AUTO?";
254 $az_status = $self->query( $command, error_check => 1 );
255 }
256 else {
257 if ( $enable =~ /^ONCE$/i ) {
258 $command = "ZERO:AUTO ONCE";
259 }
260 elsif ( $enable =~ /^(ON|1)$/i ) {
261 $command = "ZERO:AUTO ONCE";
262 }
263 elsif ( $enable =~ /^(OFF|0)$/i ) {
264 $command = "ZERO:AUTO OFF";
265 }
266 else {
267 Lab::Exception::CorruptParameter->throw( error =>
268 "HP34401A::autozero() can be set to 'ON'/1, 'OFF'/0 or 'ONCE'. Received '${enable}'\n"
269 );
270 }
271 $self->write( $command, error_check => 1 );
272 }
273  
274 return $az_status;
275 }
276  
277 sub configure_voltage_dc {
278 my $self = shift;
279 my $range = shift; # in V, or "AUTO", "MIN", "MAX"
280 my $tint = shift; # integration time in sec, "DEFAULT", "MIN", "MAX"
281 my $res_cmd = shift;
282  
283 if ( $range eq 'AUTO' || !defined($range) ) {
284 $range = 'DEF';
285 }
286 elsif ( $range =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
287  
288 #$range = sprintf("%e",abs($range));
289 }
290 elsif ( $range !~ /^(MIN|MAX)$/ ) {
291 Lab::Exception::CorruptParameter->throw( error =>
292 "Range has to be set to a decimal value or 'AUTO', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
293 );
294 }
295  
296 if ( $tint =~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
297  
298 # Convert seconds to PLC (power line cycles)
299 $tint *= $self->pl_freq();
300 }
301 elsif ( $tint !~ /^(MIN|MAX|DEFAULT)$/ ) {
302 Lab::Exception::CorruptParameter->throw( error =>
303 "Integration time has to be set to a positive value or 'AUTO', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
304 );
305 }
306  
307 if ( !defined($res_cmd) ) {
308 $res_cmd = '';
309 }
310  
311 # do it
312 $self->write( "CONF:VOLT:DC ${range} ${res_cmd}", error_check => 1 );
313 $self->write( "VOLT:DC:NPLC ${tint}", error_check => 1 )
314 if $res_cmd eq
315 ''; # integration time implicitly set through resolution
316 }
317  
318 sub configure_voltage_dc_trigger {
319 my $self = shift;
320  
321 my ( $range, $tint, $count, $delay, $res_cmd ) = $self->_check_args(
322 \@_,
323 [ 'range', 'tint', 'count', 'delay', 'resolution' ]
324 );
325  
326 ### Check the parameters for errors
327  
328 $count = 1 if !defined($count);
329 Lab::Exception::CorruptParameter->throw(
330 error => "Sample count has to be an integer between 1 and 512\n" )
331 if ( $count !~ /^[0-9]*$/ || $count < 1 || $count > 512 );
332  
333 $delay = 0 if !defined($delay);
334 Lab::Exception::CorruptParameter->throw(
335 error => "Trigger delay has to be a positive decimal value\n" )
336 if ( $count !~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ );
337  
338 if ( !defined($tint) ) {
339 $tint = "DEF";
340 }
341 elsif ( $tint =~ /^([+]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
342  
343 # Convert seconds to PLC (power line cycles)
344 $tint *= $self->pl_freq();
345 if ( $tint > 100 || $tint < 0.02 ) {
346 Lab::Exception::CorruptParameter->throw( error =>
347 "Integration time out of bounds (int. time = $tint) in HP34401A::configure_voltage_dc()\n"
348 );
349 }
350 }
351 elsif ( $tint !~ /^(MIN|MAX|DEF)$/ ) {
352 Lab::Exception::CorruptParameter->throw( error =>
353 "Integration time has to be set to a positive value, 'DEFAULT', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
354 );
355 }
356  
357 if ( $range eq 'AUTO' || !defined($range) ) {
358 $range = 'DEF';
359 }
360 elsif ( $range =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
361 $range = sprintf( "%e", abs($range) );
362 }
363 elsif ( $range !~ /^(MIN|MAX)$/ ) {
364 Lab::Exception::CorruptParameter->throw( error =>
365 "Range has to be set to a decimal value or 'AUTO', 'MIN' or 'MAX' in HP34401A::configure_voltage_dc()\n"
366 );
367 }
368  
369 if ( !defined($res_cmd) ) {
370 $res_cmd = '';
371 }
372  
373 $self->write( "CONF:VOLT:DC ${range} ${res_cmd}", error_check => 1 );
374 $self->write( "VOLT:DC:NPLC ${tint}", error_check => 1 )
375 if $res_cmd eq
376 ''; # integration time implicitly set if resolution not given
377  
378 $self->write( "*ESE 1", error_check => 1 );
379 $self->write( "*CLS", error_check => 1 );
380  
381 $self->write( "TRIG:SOURce BUS", error_check => 1 );
382 $self->write( "SAMPle:COUNt $count", error_check => 1 );
383 $self->write( "TRIG:DELay $delay", error_check => 1 );
384  
385 }
386  
387 sub read_trig {
388 my $self = shift;
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 init {
408 my $self = shift;
409 $self->write("INIT");
410 }
411  
412 sub triggered_read {
413 my $self = shift;
414 my $args = undef;
415 if ( ref $_[0] eq 'HASH' ) { $args = shift }
416 else { $args = {@_} }
417  
418 #$args->{'timeout'} = $args->{'timeout'} || $self->timeout();
419  
420 $self->init();
421 $self->read_trig();
422 $self->wait_done();
423 my $value = $self->query( "FETCh?", $args );
424  
425 chomp $value;
426  
427 my @valarray = split( ",", $value );
428  
429 return @valarray;
430 }
431  
432 sub scroll_message {
433 use Time::HiRes (qw/usleep/);
434 my $self = shift;
435 my $message = shift
436 || " Lab::Measurement - designed to make measuring fun! ";
437 for my $i ( 0 .. ( length($message) - 12 ) ) {
438 $self->display_text( sprintf "%12.12s", substr( $message, $i ) );
439 usleep(100000);
440 }
441 $self->display_clear();
442 }
443  
444 1;
445  
446 __END__