Instrument/WR640.pm untested

filter: Statementstmt 0.0% 0 / 347 Branchbran 0.0% 0 / 156 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 592 CC 79 SCAR 63.7
CC
79
Cov
0%
_parseNRf
1560.0
Lab::Instrument::_check_args
132.0
_parseReply
56.0
CRAP 585.1
1 package Lab::Instrument::WR640;
2 #ABSTRACT: LeCroy WaveRunner 640 digital oscilloscope
3 $Lab::Instrument::WR640::VERSION = '3.899';
4 use v5.20;
5  
6 use strict;
7 use warnings;
8 use Lab::Instrument;
9 use Lab::SCPI;
10 use Carp;
11 use English;
12 use Time::HiRes qw(sleep);
13 use Clone 'clone';
14 use Data::Dumper;
15  
16 our $DEBUG = 0;
17 our @ISA = ("Lab::Instrument");
18 our %fields = (
19 supported_connections => ['VICP'],
20  
21 #default settings for connections
22  
23 connection_settings => {
24 connection_type => 'VICP',
25 remote_address => 'nulrs640',
26 },
27  
28 device_settings => {},
29  
30 # too many characteristics can easily be "messed with" on the front
31 # panel, so only allow changes when scope is "locked".
32  
33 device_cache => {},
34  
35 chan_cache => {},
36 default_chan_cache => {
37 channel => undef,
38 chan_bwlimit => undef,
39 chan_coupling => undef,
40 chan_current_probe => undef,
41 chan_invert => undef,
42 chan_position => undef,
43 chan_probe => undef,
44 chan_scale => undef,
45 chan_yunit => undef,
46 select => undef,
47 },
48  
49 # non-front-panel cache items
50 NFP => [
51 qw(
52 ID
53 HEADER
54 VERBOSE
55 LOCKED
56 )
57 ],
58  
59 shared_cache => {
60 ID => undef,
61 HEADER => undef,
62 VERBOSE => undef,
63 LOCKED => undef,
64  
65 },
66  
67 channel => undef,
68  
69 # almost all of the WR640 command suite is non-SCPI
70 scpi_override => {
71  
72 },
73 );
74  
75  
76 sub new {
77 my $proto = shift;
78 my $class = ref($proto) || $proto;
79  
80 foreach my $k ( keys( %{ $fields{default_chan_cache} } ) ) {
81 $fields{device_cache}->{$k} = $fields{default_chan_cache}->{$k};
82 }
83  
84 foreach my $k ( keys( %{ $fields{shared_cache} } ) ) {
85 $fields{device_cache}->{$k} = $fields{shared_cache}->{$k};
86 }
87  
88 my $self = $class->SUPER::new(@_);
89 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
90  
91 $self->{config}->{no_cache} = 1;
92 $self->{config}->{default_read_mode} = '';
93 $DEBUG = $self->{config}->{debug} if exists $self->{config}->{debug};
94  
95 # initialize channel caches
96 foreach my $ch (qw(C1 C2 C3 C4)) {
97 $self->{chan_cache}->{$ch} = {};
98 foreach my $k ( keys( %{ $self->{default_chan_cache} } ) ) {
99 $self->{chan_cache}->{$ch}->{$k}
100 = $self->{default_chan_cache}->{$k};
101 }
102 $self->{chan_cache}->{$ch}->{channel} = $ch;
103 foreach my $k ( keys( %{ $self->{shared_cache} } ) ) {
104 $self->{chan_cache}->{$ch}->{$k} = $self->{shared_cache}->{$k};
105 }
106 }
107  
108 $self->{device_cache} = $self->{chan_cache}->{C1};
109 $self->{channel} = "C1";
110 return $self;
111 }
112  
113 #initialize scope.. this means setting up status bit masking
114 #for non-destructive testing for device errors
115  
116 sub _device_init {
117 my $self = shift;
118 $self->write("*ESE 60")
119 ; # 0x3C -> CME+EXE+DDE+QYE to bit 5 of SBR (read with *STB?)
120 $self->write("*CLS"); # clear status registers
121 }
122  
123 { # keep perl from bitching about this stuff
124 no warnings qw(redefine);
125  
126 # calling argument parsing; this is an extension of the
127 # _check_args and _check_args_strict routines in Instrument.pm,
128 # allowing more flexibility in how routines are called.
129 # In particular routine(a=>1,b=>2,..) and
130 # routine({a=>1,b=>2,..}) can both be used.
131  
132 # note: if this code does not properly recognize the syntax,
133 # then you have to use the {key=>value...} form.
134  
135 # calling:
136 # ($par1,$par2,$par3,$tail) = $self->_Xcheck_args(\@_,qw(par1 par2 par3));
137 # or, for compatibility:
138 # ($par1,$par2,$par3,$tail) = $self->_Xcheck_args(\@_,[qw(par1 par2 par3)]);
139  
140 sub Lab::Instrument::_check_args {
141 my $self = shift;
142 my $args = shift;
143 my $params = [@_];
144 $params = $params->[0] if ref( $params->[0] ) eq 'ARRAY';
145 my $arguments = {};
146  
147 if ( $#{$args} == 0 && ref( $args->[0] ) eq 'HASH' ) { # case 3
148 %{$arguments} = ( %{ $args->[0] } );
149 }
150 else {
151 my $simple = 1;
152 if ( $#{$args} & 1 == 1 ) { # must have even # arguments
153 my $found = {};
154 for ( my $j = 0; $j <= $#{$args}; $j += 2 ) {
155 if ( ref( $args->[$j] ) ne '' ) { # a ref for a key? no
156 $simple = 1;
157 last;
158 }
159 foreach my $p ( @{$params} ) { # named param
160 $simple = 0 if $p eq $args->[$j];
161 }
162 if ( exists( $found->{ $args->[$j] } ) )
163 { # key used 2x? no
164 $simple = 1;
165 last;
166 }
167 $found->{ $args->[$j] } = 1;
168 }
169 }
170  
171 if ($simple) { # case 1
172 my $i = 0;
173 foreach my $arg ( @{$args} ) {
174 if ( defined @{$params}[$i] ) {
175 $arguments->{ @{$params}[$i] } = $arg;
176 }
177 $i++;
178 }
179 }
180 else { # case 2
181 %{$arguments} = ( @{$args} );
182 }
183 }
184  
185 my @return_args = ();
186  
187 foreach my $param ( @{$params} ) {
188 if ( exists $arguments->{$param} ) {
189 push( @return_args, $arguments->{$param} );
190 delete $arguments->{$param};
191 }
192 else {
193 push( @return_args, undef );
194 }
195 }
196  
197 push( @return_args, $arguments );
198  
199 if (wantarray) {
200 return @return_args;
201 }
202 else {
203 return $return_args[0];
204 }
205 }
206  
207 sub Lab::Instrument::_check_args_strict {
208 my $self = shift;
209 my $args = shift;
210 my $params = [@_];
211 $params = $params->[0] if ref( $params->[0] ) eq 'ARRAY';
212  
213 my @result = $self->_check_args( $args, $params );
214  
215 my $num_params = @result - 1;
216  
217 for ( my $i = 0; $i < $num_params; ++$i ) {
218 if ( not defined $result[$i] ) {
219 croak("missing mandatory argument '$params->[$i]'");
220 }
221 }
222  
223 if (wantarray) {
224 return @result;
225 }
226 else {
227 return $result[0];
228 }
229 }
230  
231 }
232 #
233 # utility function: check header/verbose and parse
234 # query reply appropriately; remove quotes in present
235 # ex: $self->_parseReply('ACQ:MODE average',qw{AVE PEAK SAM})
236 # gives AVE
237 sub _parseReply {
238 my $self = shift;
239 my $in = shift;
240  
241 my $h = $self->get_header();
242 if ($h) {
243 my $c;
244 ( $c, $in ) = split( /\s+/, $in );
245 return '' unless defined($in) && $in ne '';
246 }
247  
248 # remove quotes on strings
249 if ( $in =~ /^\"(.*)\"$/ ) {
250 $in = $1;
251 $in =~ s/\"\"/"/g;
252 }
253 elsif ( $in =~ /^\'(.*)\'$/ ) {
254 $in = $1;
255 $in =~ s/\'\'/'/g;
256 }
257  
258 return $in unless $#_ > -1;
259 my $v = $self->get_verbose();
260 return $in unless $v;
261 return _keyword( $in, @_ );
262 }
263  
264 #
265 # select keyword
266 # example: $got = _keyword('input', qw{ IN OUT EXT } )
267 # returns $got = 'IN'
268  
269 sub _keyword {
270 my $in = shift;
271 $in = shift if ref($in) eq 'HASH'; # dispose of $self->_keyword form...
272 my $r;
273  
274 $in =~ s/^\s+//;
275 foreach my $k (@_) {
276 if ( $in =~ /^$k/i ) {
277 return $k;
278 }
279 }
280 Lab::Exception::CorruptParameter->throw("Invalid keyword input '$in'\n");
281 }
282  
283 # convert 'short form' keywords to long form
284  
285 sub _bloat {
286 my $in = shift;
287 $in = shift if ref($in) eq 'HASH'; # dispose of $self->_bloat
288 my $tr = shift; # hash of short=>long:
289  
290 $in =~ s/^\s+//;
291 $in =~ s/\s+$//;
292 return $in if $in eq '';
293  
294 foreach my $k ( keys( %{$tr} ) ) {
295 if ( $in =~ /^${k}/i ) {
296 return $tr->{$k};
297 }
298 }
299  
300 return uc($in); # nothing matched
301 }
302  
303 # parse a GPIB number with suffix, units
304 # $result = _parseNRf($numberstring,$unit1[,$unit2,...])
305 # _parseNRf('maximum','foo) -> 'MAX'
306 # _parseNRf('-3.7e+3kJ','j') -> -3.7e6
307 # _parseNRf('2.3ksec','s','sec') -> 2300 ('s' and 'sec' alternate units)
308 # note special cases for suffixes: MHZ, MOHM, MA
309 # also handling 'dB' -> (number)dB(magnitudesuffix)(unit V|W|etc)
310 #
311 # if problem, string returned starts 'ERR: ..message...'
312 # see IEEE std 488-2 7.7.3
313  
314 sub _parseNRf {
315 my $in = shift;
316 $in = shift if ref($in) eq 'HASH'; # $self->_parseNRf handling...
317 my $un = shift;
318 $un = '' unless defined $un;
319 my $us;
320  
321 if ( ref($un) eq 'ARRAY' ) {
322 $us = $un;
323 }
324 elsif ( ref($un) eq 'SCALAR' ) {
325 $us = [ $$un, @_ ];
326 }
327 elsif ( ref($un) eq '' ) {
328 $us = [ $un, @_ ];
329 }
330 my $str = $in;
331  
332 $str =~ s/^\s+//;
333 $str =~ s/\s+$//;
334  
335 if ( $str =~ /^MIN/i ) {
336 return 'MIN';
337 }
338 if ( $str =~ /^MAX/i ) {
339 return 'MAX';
340 }
341  
342 my $mant = 0;
343 my $exp = 0;
344 if ( $str =~ /^([+\-]?(\d+\.\d*|\d+|\d*\.\d+))\s*/i ) {
345 $mant = $1;
346 $str = $POSTMATCH;
347 return $mant if $str eq '';
348 if ( $str =~ /^e\s*([+\-]?\d+)\s*/i ) {
349 $exp = $1;
350 $str = $POSTMATCH;
351 }
352 return $mant * ( 10**$exp ) if $str eq '';
353  
354 my $kexp = $exp;
355 my $kstr = $str;
356 foreach my $u ( @{$us} ) {
357 $u =~ s/^\s+//;
358 $u =~ s/\s+$//;
359  
360 $str = $kstr;
361 $exp = $kexp;
362 if ( $u =~ /^db/i ) { # db(magnitude_suffix)?(V|W|... unit)?
363 my $dbt = $POSTMATCH;
364 if ( $str =~ /^dBex(${dbt})?$/i ) {
365 $exp += 18;
366 }
367 elsif ( $str =~ /^dBpe(${dbt})?$/i ) {
368 $exp += 15;
369 }
370 elsif ( $str =~ /^dBt(${dbt})?$/i ) {
371 $exp += 12;
372 }
373 elsif ( $str =~ /^dBg(${dbt})?$/i ) {
374 $exp += 9;
375 }
376 elsif ( $str =~ /^dBma(${dbt})$/i ) {
377 $exp += 6;
378 }
379 elsif ( $str =~ /^dBk(${dbt})?$/i ) {
380 $exp += 3;
381 }
382 elsif ( $str =~ /^dBm(${dbt})?$/i ) {
383 $exp -= 3;
384 }
385 elsif ( $str =~ /^dBu(${dbt})?$/i ) {
386 $exp -= 6;
387 }
388 elsif ( $str =~ /^dBn(${dbt})?$/i ) {
389 $exp -= 9;
390 }
391 elsif ( $str =~ /^dBp(${dbt})?$/i ) {
392 $exp -= 12;
393 }
394 elsif ( $str =~ /^dBf(${dbt})?$/i ) {
395 $exp -= 15;
396 }
397 elsif ( $str =~ /^dB${dbt}$/i ) {
398 $exp += 0;
399 }
400 else {
401 next;
402 }
403 }
404 else { # regular units stuff: (magnitude_suffix)(unit)?
405 if ( $str =~ /^ex(${u})?$/i ) {
406 $exp += 18;
407 }
408 elsif ( $str =~ /^pe(${u})?$/i ) {
409 $exp += 15;
410 }
411 elsif ( $str =~ /^t(${u})?$/i ) {
412 $exp += 12;
413 }
414 elsif ( $str =~ /^g(${u})?$/i ) {
415 $exp += 9;
416 }
417 elsif ( $u =~ /(HZ|OHM)/i && $str =~ /^ma?(${u})$/i ) {
418 $exp += 6;
419 }
420 elsif ( $u =~ /A/i && $str =~ /^ma$/i ) {
421 $exp -= 3;
422 }
423 elsif ( $u !~ /(HZ|OHM)/i && $str =~ /^ma(${u})?$/i ) {
424 $exp += 6;
425 }
426 elsif ( $str =~ /^k(${u})?$/i ) {
427 $exp += 3;
428 }
429 elsif ( $str =~ /^m(${u})?$/i ) {
430 $exp -= 3;
431 }
432 elsif ( $str =~ /^u(${u})?$/i ) {
433 $exp -= 6;
434 }
435 elsif ( $str =~ /^n(${u})?$/i ) {
436 $exp -= 9;
437 }
438 elsif ( $str =~ /^p(${u})?$/i ) {
439 $exp -= 12;
440 }
441 elsif ( $str =~ /^f(${u})?$/i ) {
442 $exp -= 15;
443 }
444 elsif ( $str =~ /^${u}$/i ) {
445 $exp += 0;
446 }
447 else {
448 next;
449 }
450 }
451 return $mant * ( 10**$exp );
452 }
453 }
454 return "ERR: '$str' number parsing problem";
455  
456 }
457  
458  
459 sub reset {
460 my $self = shift;
461 $self->write("*RST");
462 $self->_debug();
463 $self->_reset_cache();
464 }
465  
466 our $_rst_state = {
467 LOCKED => 'NON',
468 HEADER => '1',
469 VERBOSE => '1',
470 };
471  
472 sub _reset_cache {
473 my $self = shift;
474  
475 for my $k ( keys( %{$_rst_state} ) ) {
476 $self->{device_cache}->{$k} = $_rst_state->{$k};
477 for ( my $ch = 1; $ch <= 4; $ch++ ) {
478 $self->{chan_cache}->{"CH$ch"}->{select} = ( $ch == 1 ? 1 : 0 );
479 next if "CH$ch" eq $self->{channel};
480 $self->{chan_cache}->{"CH$ch"}->{$k} = $_rst_state->{$k};
481 }
482 }
483 $self->{device_cache}->{select} = ( $self->{channel} eq 'CH1' ? 1 : 0 );
484 foreach my $wfm (qw(MATH REFA REFB REFC REFD)) {
485 $self->{chan_cache}->{$wfm}->{select} = 0;
486 }
487 }
488  
489 # print error queue; meant to be called at end of routine
490 # so uses 'caller' info to label the subroutine
491 sub _debug {
492 return unless $DEBUG;
493 my $self = shift;
494 my ( $p, $f, $l, $subr ) = caller(1);
495 while (1) {
496 my ( $code, $msg ) = $self->get_error();
497 last if $code == 0;
498 print "$subr\t$code: $msg\n";
499 }
500 }
501  
502  
503 sub get_error {
504 my $self = shift;
505  
506 my $err = $self->query("CHL? CLR");
507 $err =~ s/^(CHL\s*)?\"(.*)\"/$2/is;
508 my (@lines) = split( /\n/, $err );
509 my (@elines) = ();
510  
511 foreach my $x (@lines) {
512 $x =~ s/^\s*(.*)\s*$/$1/;
513 next if $x =~ /^connection\s/i;
514 next if $x =~ /^disconnect/i;
515 push( @elines, $x );
516 }
517 return (@elines);
518  
519 }
520  
521  
522 our $sbits = [qw(OPC RQC QYE DDE EXE CME URQ PON)];
523  
524 sub get_status {
525 my $self = shift;
526 my $bit = shift;
527 my $s = {};
528  
529 my $r = $self->query('*ESR?');
530 $self->_debug();
531  
532 for ( my $j = 0; $j < 7; $j++ ) {
533 $s->{ $sbits->[$j] } = ( $r >> $j ) & 0x01;
534 }
535 $s->{ERROR} = $s->{CME} | $s->{EXE} | $s->{DDE} | $s->{QYE};
536  
537 return $s->{ uc($bit) } if defined $bit;
538 return $s;
539 }
540  
541  
542 sub test_busy {
543 my $self = shift;
544 return 1 if $self->query('BUSY?') =~ /^(:BUSY )?\s*1/i;
545 return 0;
546 }
547  
548  
549 sub get_id {
550 my $self = shift;
551 my ($tail) = $self->_check_args( \@_ );
552  
553 $tail->{read_mode} = $self->{config}->{default_read_mode}
554 unless exists( $tail->{read_mode} ) && defined( $tail->{read_mode} );
555  
556 if ( $tail->{read_mode} ne 'cache'
557 || !defined( $self->{device_cache}->{ID} ) ) {
558 $self->{device_cache}->{ID} = $self->query('*IDN?');
559 $self->_debug();
560 }
561 return $self->{device_cache}->{ID};
562 }
563  
564  
565 sub recall {
566 my $self = shift;
567 my ( $mem, $tail ) = $self->_check_args( \@_, 'n' );
568  
569 my $n;
570 if ( $mem =~ /^\s*([0-6])\s/ ) {
571 $n = $1;
572 }
573 else {
574 carp("recall memory n=$mem invalid, should be 0..6");
575 return;
576 }
577 $self->write("*RCL $n");
578 }
579  
580 sub get_setup {
581 my $self = shift;
582 my (@a) = ();
583  
584 foreach my $ch (qw(C1 C2 C3 C4 EX EX10 ETM10 LINE)) {
585 if ( $ch =~ /C\d/ ) {
586 foreach my $q (qw(ATTN CPL OFST OFCT TRA TRCP VDIV)) {
587 push( @a, $self->query( $ch . ':' . $q . '?' ) );
588 }
589 }
590 if ( $ch ne 'LINE' ) {
591 push( @a, $self->query( $ch . ":TRLV?" ) );
592 }
593 push( @a, $self->query( $ch . ":TRSL?" ) );
594 }
595  
596 for ( my $j = 1; $j <= 8; $j++ ) {
597 my $ch = "F$j";
598 foreach my $q (qw(TRA VMAG VPOS)) {
599 push( @a, $self->query( $ch . ":" . $q . "?" ) );
600 }
601 }
602  
603 foreach my $ch (qw(M1 M2 M3 M4)) {
604 push( @a, $self->query( $ch . ":VPOS?" ) );
605 }
606  
607 foreach my $q (
608 qw(ALST BWL COUT CMR COMB CFMT CHDR CORD CRMS
609 ILVD RCLK SCLK SEQ TDIV TRDL TRMD TRPA
610 TRSE WFSU)
611 ) {
612 push( @a, $self->query( $q . '?' ) );
613 }
614  
615 return (@a);
616 }
617  
618 sub get_visible {
619 my $self = shift;
620 my $ch = shift;
621  
622 my $r = $self->query("$ch:TRA?");
623 $r =~ s/^.*:TRA(ce)?\s+//i;
624 $r = uc($r);
625 return 1 if $r eq 'ON';
626 return 0;
627 }
628  
629 sub get_waveform {
630 my $self = shift;
631 my $ch = shift;
632 return $self->query("$ch:WF?");
633 }
634  
635 1;
636  
637 __END__