Instrument.pm

filter: Statementstmt 74.8% 368 / 492 Branchbran 54.1% 132 / 244 Conditioncond 42.1% 35 / 83 MC/DCmcdc 13.2% 7 / 53 Subroutinesub 83.6% 51 / 61 Podpod 28.0% 7 / 25 totaltotal 62.6% 600 / 958 SCAR 72.3
CC
170
Cov
65%
_cache_init
72.0
_getset_key
56.0
__ANON__
25.7
CRAP 1375.0
« XPRESS/Data/XPRESS_logger.pm ↑ Summary Bus/MODBUS_RS232.pm »
1 package Lab::Instrument;
2 9 $Lab::Instrument::VERSION = '3.881';
3 #ABSTRACT: Instrument base class
4  
5 9 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use POSIX; # added for int() function
11 9 ▶use Lab::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Lab::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Lab::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Class::ISA qw(self_and_super_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Hook::LexWrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use Time::HiRes qw (usleep sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 our @ISA = ('Lab::Generic');
23  
24 9 our $AUTOLOAD;
25  
26 9 our %fields = (
27  
28 device_name => undef,
29 device_comment => undef,
30  
31 ins_debug => 0, # do we need additional output?
32  
33 connection => undef,
34 supported_connections => ['ALL'],
35  
36 # for connection default settings/user supplied settings. see accessor method.
37 connection_settings => { timeout => 1 },
38  
39 # default device settings/user supplied settings. see accessor method.
40 device_settings => {
41 name => undef,
42 wait_status => 10e-6, # sec
43 wait_query => 10e-6, # sec
44 query_length => 300, # bytes
45 query_long_length => 10240, # bytes
46 },
47  
48 device_cache => {
49  
50 },
51  
52 device_cache_order => [],
53  
54 config => {},
55 );
56  
57 sub new {
58 10 ▶ my $proto = shift;
Subroutine new: called CC=9 SCAR=22.5
Pod documented
59 10 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
60 10 my $config = undef;
61 10 ▶ if ( ref $_[0] eq 'HASH' ) { $config = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 10 0 10
62 0 else { $config = {@_} }
63  
64 10 my $self = $class->SUPER::new(@_);
65 10 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
66  
67 # wrap additional code for automatic cache-handling aroung all paramter set- and get-functions defined in %fields->{device_cache}
68 10 my @isa = Class::ISA::self_and_super_path($class);
69 10 my $flag = 0;
70 10 ▶ while (@isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (@isa) 38 10 48
71 38 my $isa = pop @isa;
72 38 ▶ if ( $flag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 1) 18 20 38
73 18 $self->_init_cache_handling($isa);
74 }
75 38 ▶ if ( $isa eq 'Lab::Instrument' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa eq "Lab::Instrument") 10 28 38
76 10 $flag = 1;
77 }
78  
79 }
80  
81 10 $self->config($config);
82  
83 #
84 # In most inherited classes, configure() is run through _construct()
85 #
86 10 $self->${ \( __PACKAGE__ . '::configure' ) }( $self->config() )
87 ; # use local configure, not possibly overwritten one
88  
89 10 ▶ if ( $class eq __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 0 10 10
90  
91 # _setconnection after providing $config - needed for direct instantiation of Lab::Instrument
92 0 $self->_setconnection();
93 }
94  
95 # digest parameters
96 10 ▶ $self->device_name( $self->config('device_name') )
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config("device_name") 0 10 10
97 if defined $self->config('device_name');
98 10 ▶ $self->device_comment( $self->config('device_comment') )
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config("device_comment") 0 10 10
99 if defined $self->config('device_comment');
100  
101 10 $self->register_instrument();
102  
103 10 return $self;
104 }
105  
106 #
107 # Call this in inheriting class's constructors to conveniently initialize the %fields object data.
108 #
109 sub _construct { # _construct(__PACKAGE__);
110 28 ▶ ( my $self, my $package ) = ( shift, shift );
Subroutine _construct: called CC=11 SCAR=24.0
111  
112 28 my $class = ref($self);
113 28 my $fields = undef;
114 {
115 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
116 28 $fields = *${ \( $package . '::fields' ) }{HASH};
117 }
118  
119 28 foreach my $element ( keys %{$fields} ) {
120  
121 # handle special subarrays
122 215 ▶ if ( $element eq 'device_settings' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element eq 'device_settings') { } 26 189 215
elsif ($element eq 'connection_settings') { } 18 171 189
123  
124 # don't overwrite filled hash from ancestor
125 $self->{device_settings} = {}
126 26 ▶ if !exists( $self->{device_settings} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"device_settings"} 10 16 26
127 26 for my $s_key ( keys %{ $fields->{'device_settings'} } ) {
128 $self->{device_settings}->{$s_key}
129 162 = clone( $fields->{device_settings}->{$s_key} );
130 }
131 }
132 elsif ( $element eq 'connection_settings' ) {
133  
134 # don't overwrite filled hash from ancestor
135 $self->{connection_settings} = {}
136 18 ▶ if !exists( $self->{connection_settings} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"connection_settings"} 10 8 18
137 18 for my $s_key ( keys %{ $fields->{connection_settings} } ) {
138 $self->{connection_settings}->{$s_key}
139 28 = clone( $fields->{connection_settings}->{$s_key} );
140 }
141 }
142 else {
143 # handle the normal fields - can also be hash refs etc, so use clone to get a deep copy
144 171 $self->{$element} = clone( $fields->{$element} );
145  
146 #warn "here comes\n" if($element eq 'device_cache');
147 #warn Dumper($Lab::Instrument::DummySource::fields) if($element eq 'device_cache');
148 }
149 215 $self->{_permitted}->{$element} = 1;
150  
151 }
152  
153 # @{$self}{keys %{$fields}} = values %{$fields};
154  
155 #
156 # run configure() of the calling package on the supplied config hash.
157 # this parses the whole config hash on every heritance level (and with every version of configure())
158 # For Lab::Instrument itself it does not make sense, as $self->config() is not set yet. Instead it's run from the new() method, see there.
159 #
160 28 ▶ $self->${ \( $package . '::configure' ) }( $self->config() )
Branch: 1 branch
BranchTrueFalseTotal
if $class ne "Lab::Instrument" 28 0 28
161 if $class ne 'Lab::Instrument'
162 ; # use configure() of calling package, not possibly overwritten one
163  
164 #
165 # Check and parse the connection data OR the connection object in $self->config(), but only if
166 # _construct() has been called from the instantiated class (and not from somewhere up the heritance hierarchy)
167 # That's because child classes can add new entrys to $self->supported_connections(), so delay checking to the top class.
168 # Also, don't run _setconnection() for Lab::Instrument, as in this case the needed fields in $self->config() are not set yet.
169 # It's run in Lab::Instrument::new() instead if needed.
170 #
171 # Also, other stuff that should only happen in the top level class instantiation can go here.
172 #
173  
174 28 ▶ if ( $class eq $package && $class ne 'Lab::Instrument' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq $package and $class ne "Lab::Instrument") 10 18 28
Condition66%2/3
$class eq $package and $class ne "Lab::Instrument"
!ll&&!rl&&r
18010
MC/DC50%1/2
$class eq $package and $class ne "Lab::Instrument"
$class eq $package $class ne "Lab::Instrument"
Truth table66%2/3
A and B
A= $class eq $packageB= $class ne "Lab::Instrument"
ABresult
0X0
100
111
175  
176 10 $self->_setconnection();
177  
178 # Match the device hash with the device
179 # The cache carries the default values set above and was possibly modified with user
180 # defined values through configure() before the connection was set. These settings are now transferred
181 # to the device.
182 10 $self->_device_init(); # enable device communication if necessary
183 10 $self->_set_config_parameters(); # transfer configuration to device
184  
185 }
186  
187 }
188  
189 # this methode implements the cache-handling:
190 #
191 # It will wrap all get- and set-functions for parameters initialized in $fields->{device_cache} with additional pre- and post-processing code.
192 # If a get-function is called and read_mode == cache, the $self->{device_cache}->{parameter} will be returned immediately. The original get-function won't be executed in this case.
193 # This behaviour can be disabled by setting the parmeter $self->{config}->{no_cache} = 1.
194 # The return-value of the get-function will be cached in $self->{device_cache}in any case.
195 #
196 # Set-functions will automatically call the corresponding get-function in the post-processing section, in order to keep the cache up to date.
197 #
198 # If a requestID has been set, only the get-function, which placed the request will be executed, while all others return the cache-value. Set-functions won't be executed at all.
199 #
200  
201 sub _init_cache_handling {
202 18 ▶ my $self = shift;
Subroutine _init_cache_handling: called CC=7 SCAR=21.5
203 18 my $class = shift;
204  
205 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 # avoid to redefine the subs twice
208 18 ▶ if ( defined ${ $class . '::MODIFIED' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$class . '::MODIFIED';}) 3 15 18
209 3 return;
210 }
211  
212 15 my $fields = *${ \( $class . '::fields' ) }{HASH};
213 15 my @cache_params = keys %{ $fields->{device_cache} };
214  
215 # wrap parameter function defined in %fields->{device_cache}:
216 15 foreach my $cache_param (@cache_params) {
217 38 my $set_sub = "set_" . $cache_param;
218 38 my $get_sub = "get_" . $cache_param;
219  
220 38 my $get_methode = *{ $class . "::" . $get_sub };
221 38 my $set_methode = *{ $class . "::" . $set_sub };
222  
223 38 ▶ if ( $class->can( "set_" . $cache_param ) and exists &$set_methode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('set_' . $cache_param) and exists &$set_methode) 29 9 38
Condition100%3/3
$class->can('set_' . $cache_param) and exists &$set_methode
!ll&&!rl&&r
4529
MC/DC100%2/2
$class->can('set_' . $cache_param) and exists &$set_methode
$class->can('set_' . $cache_param) exists &$set_methode
Truth table100%3/3
A and B
A= $class->can('set_' . $cache_param)B= exists &$set_methode
ABresult
0X0
100
111
224  
225 # Change STDERR to undef, in order to avoid warnings from Hook::LexWrap and
226 # and save original STDERR stream in SAVEERR to be able to restore original
227 # behavior
228 29 local (*SAVEERR);
229  
230 #open SAVEERR, ">&STDERR";
231 #open(STDERR, '>', undef);
232  
233 # wrap set-function:
234 wrap(
235 $class . "::" . $set_sub,
236  
237 # before set-functions is executed:
238 pre => sub {
239 75 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=12.7
240  
241 75 ${__PACKAGE__::SELF} = $self;
242 75 ${__PACKAGE__::SELF}->{fast_cache_value} = $_[0];
243  
244 # read_mode handling: do not execute if request is set:
245 75 ▶ if ( defined $self->{requestID}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"requestID"} or $self->connection->is_blocked) 0 75 75
Condition33%1/3
defined $self->{"requestID"} or $self->connection->is_blocked
l!l&&r!l&&!r
0075
MC/DC0%0/2
defined $self->{"requestID"} or $self->connection->is_blocked
defined $self->{"requestID"} $self->connection->is_blocked
Truth table33%1/3
A or B
A= defined $self->{"requestID"}B= $self->connection->is_blocked
ABresult
1X1
011
000
246 or $self->connection()->is_blocked() ) {
247 0 $_[-1] = 'connection blocked';
248 }
249 },
250  
251 # after set-functions is executed:
252 post => sub {
253  
254 75 ▶ if ( not defined ${__PACKAGE__::SELF} ) {
Subroutine __ANON__: called CC=6 SCAR=24.2
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $__PACKAGE__::SELF) 0 75 75
255 0 return;
256 }
257  
258 # skip get_sub if $self->{config}->{fast_cache} is set.
259 75 ▶ if ( defined ${__PACKAGE__::SELF}->{config}->{fast_cache}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $__PACKAGE__::SELF->{"config"}{"fast_cache"} and $__PACKAGE__::SELF->{"config"}{"fast_cache"} > 0) 0 75 75
Condition33%1/3
defined $__PACKAGE__::SELF->{"config"}{"fast_cache"} and $__PACKAGE__::SELF->{"config"}{"fast_cache"} > 0
!ll&&!rl&&r
7500
MC/DC0%0/2
defined $__PACKAGE__::SELF->{"config"}{"fast_cache"} and $__PACKAGE__::SELF->{"config"}{"fast_cache"} > 0
defined $__PACKAGE__::SELF->{"config"}{"fast_cache"} $__PACKAGE__::SELF->{"config"}{"fast_cache"} > 0
Truth table33%1/3
A and B
A= defined $__PACKAGE__::SELF->{"config"}{"fast_cache"}B= $__PACKAGE__::SELF->{"config"}{"fast_cache"} > 0
ABresult
0X0
100
111
260 and ${__PACKAGE__::SELF}->{config}->{fast_cache} > 0 )
261 {
262 ${__PACKAGE__::SELF}->device_cache(
263 {
264 $cache_param =>
265 ${__PACKAGE__::SELF}->{fast_cache_value}
266 }
267 0 );
268 0 return;
269 }
270  
271 # call coresponding get-function in order to keep the cache up to date, if available
272  
273 75 ▶ if ( ${__PACKAGE__::SELF}->can($get_sub)
Branch: 1 branch
BranchTrueFalseTotal
if ($__PACKAGE__::SELF->can($get_sub) and not $__PACKAGE__::SELF->{"config"}{"no_cache"}) 75 0 75
Condition33%1/3
$__PACKAGE__::SELF->can($get_sub) and not $__PACKAGE__::SELF->{"config"}{"no_cache"}
!ll&&!rl&&r
0075
MC/DC0%0/2
$__PACKAGE__::SELF->can($get_sub) and not $__PACKAGE__::SELF->{"config"}{"no_cache"}
$__PACKAGE__::SELF->can($get_sub) not $__PACKAGE__::SELF->{"config"}{"no_cache"}
Truth table33%1/3
A and not(B)
A= $__PACKAGE__::SELF->can($get_sub)B= not $__PACKAGE__::SELF->{"config"}{"no_cache"}
ABresult
0X0
100
111
274 and not ${__PACKAGE__::SELF}->{config}->{no_cache} ) {
275 75 my $var = ${__PACKAGE__::SELF}->$get_sub();
276 }
277  
278 }
279 29 );
280  
281 # Restore Warnings:
282 #open STDERR, ">&SAVEERR";
283  
284 }
285  
286 38 ▶ if ( $class->can( "get_" . $cache_param ) and exists &$get_methode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('get_' . $cache_param) and exists &$get_methode) 34 4 38
Condition66%2/3
$class->can('get_' . $cache_param) and exists &$get_methode
!ll&&!rl&&r
4034
MC/DC50%1/2
$class->can('get_' . $cache_param) and exists &$get_methode
$class->can('get_' . $cache_param) exists &$get_methode
Truth table66%2/3
A and B
A= $class->can('get_' . $cache_param)B= exists &$get_methode
ABresult
0X0
100
111
287  
288 # Change STDERR to undef, in order to avoid warnings from Hook::LexWrap and
289 # and save original STDERR stream in SAVEERR to be able to restore original
290 # behavior
291 34 local (*SAVEERR);
292  
293 #open SAVEERR, ">&STDERR";
294 #open(STDERR, '>', undef);
295  
296 34 my $parameter = $cache_param;
297  
298 # wrap get-function:
299 wrap(
300 $class . "::" . $get_sub,
301  
302 # before get-functions is executed:
303 pre => sub {
304 294 ▶ my $self = shift;
Subroutine __ANON__: called CC=12 SCAR=32.5
305  
306 294 ${__PACKAGE__::SELF} = $self;
307  
308 # read_mode handling:
309 294 my @args = @_;
310 294 pop @args;
311 294 my ( $read_mode, $tail )
312 = $self->_check_args( \@args, ['read_mode'] );
313  
314 # do not read if request has been set. set read_mode to cache if cache is available
315 $read_mode = $self->{config}->{default_read_mode}
316 if !defined($read_mode)
317 294 ▶ and exists( $self->{config}->{default_read_mode} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $read_mode and exists $self->{"config"}{"default_read_mode"} 0 294 294
Condition66%2/3
not defined $read_mode and exists $self->{"config"}{"default_read_mode"}
!ll&&!rl&&r
1061880
MC/DC0%0/2
not defined $read_mode and exists $self->{"config"}{"default_read_mode"}
not defined $read_mode exists $self->{"config"}{"default_read_mode"}
Truth table66%2/3
not(A) and B
A= not defined $read_modeB= exists $self->{"config"}{"default_read_mode"}
ABresult
0X0
100
111
318  
319 294 ▶ if ( $self->connection()->is_blocked() == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection->is_blocked == 1) 0 294 294
320 0 ▶ if ( defined $self->device_cache($parameter) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->device_cache($parameter)) { } 0 0 0
321 0 $read_mode = 'cache';
322 }
323 else {
324 0 $_[-1] = 'connection_blocked';
325 }
326 }
327  
328 294 ▶ if ( defined $self->{requestID} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"requestID"}) 0 294 294
329 my ( $package, $filename, $subroutine, $line )
330 0 = split( / /, $self->{requestID} );
331  
332 0 ▶ if ( $subroutine ne $class . "::" . $get_sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subroutine ne $class . '::' . $get_sub) { } 0 0 0
333 0 ▶ if ( defined $self->device_cache($parameter) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->device_cache($parameter)) { } 0 0 0
334  
335 0 $read_mode = 'cache';
336 }
337 else {
338 0 $_[-1] = 'connection_blocked';
339 }
340 }
341 else {
342 0 $read_mode = undef;
343 0 pop @_;
344 }
345  
346 }
347  
348 # return cache value if read_mode is set to cache
349 294 ▶ if ( defined $read_mode
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_mode and $read_mode eq "cache" and defined $self->device_cache($parameter) and not $self->{"config"}{"no_cache"}) 98 196 294
Condition88%8/9
defined $read_mode and $read_mode eq "cache" and defined $self->device_cache($parameter) and not $self->{"config"}{"no_cache"}
!ll&&!rl&&r
196098
defined $read_mode and $read_mode eq "cache" and defined $self->device_cache($parameter)
!ll&&!rl&&r
194298
defined $read_mode and $read_mode eq "cache"
!ll&&!rl&&r
1886100
MC/DC75%3/4
defined $read_mode and $read_mode eq "cache" and defined $self->device_cache($parameter) and not $self->{"config"}{"no_cache"}
defined $read_mode $read_mode eq "cache" defined $self->device_cache($parameter) not $self->{"config"}{"no_cache"}
Truth table80%4/5
A and B and C and not(D)
A= defined $read_modeB= $read_mode eq "cache"C= defined $self->device_cache($parameter)D= not $self->{"config"}{"no_cache"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
350 and $read_mode eq 'cache'
351 and defined $self->device_cache($parameter)
352 and not $self->{config}->{no_cache} ) {
353 98 $_[-1] = $self->device_cache($parameter);
354 }
355  
356 },
357  
358 # after get-functions is executed:
359 post => sub {
360  
361 196 ▶ if ( not defined ${__PACKAGE__::SELF} ) {
Subroutine __ANON__: called CC=5 SCAR=18.4
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $__PACKAGE__::SELF) 0 196 196
362 0 return;
363 }
364 196 my $retval = $_[-1];
365  
366 # refresh cache value
367 196 ▶ if ( not defined $retval
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $retval or ref $retval eq 'Hook::LexWrap::Cleanup') { } 0 196 196
Condition33%1/3
not defined $retval or ref $retval eq "Hook::LexWrap::Cleanup"
l!l&&r!l&&!r
00196
MC/DC0%0/2
not defined $retval or ref $retval eq "Hook::LexWrap::Cleanup"
not defined $retval ref $retval eq "Hook::LexWrap::Cleanup"
Truth table33%1/3
not(A) or B
A= not defined $retvalB= ref $retval eq "Hook::LexWrap::Cleanup"
ABresult
1X1
011
000
368 or ref($retval) eq 'Hook::LexWrap::Cleanup' ) {
369 0 return;
370 }
371 else {
372 196 ▶ my $cache_value = wantarray ? $retval->[0] : $retval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 193 196
373 196 ${__PACKAGE__::SELF}
374 ->device_cache( { $parameter => $cache_value } );
375 }
376 }
377 34 );
378  
379 # Restore Warnings:
380 #open STDERR, ">&SAVEERR";
381 }
382  
383 }
384  
385 # remeber that we have allready redefined the functions
386 15 ${ $class . '::MODIFIED' } = 1;
387  
388 9 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
389  
390 }
391  
392 sub register_instrument {
393 10 ▶ my $self = shift;
Subroutine register_instrument: called CC=1 SCAR=0.0
Pod undocumented
394  
395 10 push( @{Lab::Instrument::REGISTERED_INSTRUMENTS}, $self );
396  
397 }
398  
399 sub unregister_instrument {
400 0 ▶ my $self = shift;
Subroutine unregister_instrument: not called CC=1 SCAR=6.9
Pod undocumented
401  
402 @{Lab::Instrument::REGISTERED_INSTRUMENTS}
403 0 = grep { $_ ne $self } @{Lab::Instrument::REGISTERED_INSTRUMENTS};
404  
405 }
406  
407 sub sprint_config {
408 2 ▶ my $self = shift;
Subroutine sprint_config: called CC=2 SCAR=6.9
Pod undocumented
409  
410 2 $Data::Dumper::Varname = "device_cache_";
411 2 my $config = Dumper $self->device_cache();
412  
413 2 $config .= "\n";
414  
415 2 $Data::Dumper::Maxdepth = 1;
416 2 $Data::Dumper::Varname = "connection_settings_";
417 2 ▶ if ( defined $self->connection() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->connection) 2 0 2
418 2 $config .= Dumper $self->connection()->config();
419 }
420 2 return $config;
421  
422 }
423  
424 sub _set_config_parameters {
425 10 ▶ my $self = shift;
Subroutine _set_config_parameters: called CC=6 SCAR=20.5
426  
427 10 my @order = @{ $self->device_cache_order() };
428 10 my @keys = keys %{ $self->config() };
429  
430 10 foreach my $ckey (@order) {
431 16 my $subname = 'set_' . $ckey;
432 16 ▶ if ( defined $self->config($ckey) and $self->can($subname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config($ckey) and $self->can($subname)) 0 16 16
Condition33%1/3
defined $self->config($ckey) and $self->can($subname)
!ll&&!rl&&r
1600
MC/DC0%0/2
defined $self->config($ckey) and $self->can($subname)
defined $self->config($ckey) $self->can($subname)
Truth table33%1/3
A and B
A= defined $self->config($ckey)B= $self->can($subname)
ABresult
0X0
100
111
433 0 my $result = $self->$subname( $self->config($ckey) );
434 0 @keys = grep { $_ ne $ckey } @keys;
435 }
436 }
437  
438 10 foreach my $ckey (@keys) {
439 49 my $subname = 'set_' . $ckey;
440 49 ▶ if ( $self->can($subname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($subname)) 0 49 49
441 0 my $result = $self->$subname( $self->config($ckey) );
442 }
443 }
444  
445 }
446  
447 # old; replaced by _refresh_cache and _set_config_parameters
448 sub _getset_key {
449 0 ▶ my $self = shift;
Subroutine _getset_key: not called CC=7 SCAR=40.3
450 0 my $ckey = shift;
451  
452 #print Dumper $self->device_cache();
453  
454 Lab::Exception::CorruptParameter->throw(
455 "No field with name $ckey in device_cache!\n")
456 0 ▶ if !exists $self->device_cache()->{$ckey};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->device_cache->{$ckey} 0 0 0
457 0 ▶ if ( !defined $self->device_cache()->{$ckey}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->device_cache->{$ckey} || defined $self->config->{$ckey}) { } 0 0 0
Condition0%0/3
defined $self->device_cache->{$ckey} || defined $self->config->{$ckey}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $self->device_cache->{$ckey} || defined $self->config->{$ckey}
defined $self->device_cache->{$ckey} defined $self->config->{$ckey}
Truth table0%0/3
A || B
A= defined $self->device_cache->{$ckey}B= defined $self->config->{$ckey}
ABresult
1X1
011
000
458 and !defined $self->config()->{$ckey} ) {
459 0 my $subname = 'get_' . $ckey;
460 0 ▶ Lab::Exception::CorruptParameter->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($subname) 0 0 0
461 "No get method defined for device_cache field $ckey! \n")
462 if !$self->can($subname);
463 0 my $result = $self->$subname();
464 }
465 else {
466 0 my $subname = 'set_' . $ckey;
467 0 ▶ print Dumper $self->device_cache() if !$self->can($subname);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($subname) 0 0 0
468 0 ▶ Lab::Exception::CorruptParameter->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($subname) 0 0 0
469 "No set method defined for device_cache field $ckey!\n")
470 if !$self->can($subname);
471 0 my $result = $self->$subname( $self->device_cache()->{$ckey} );
472 }
473  
474 }
475  
476 #
477 # Sync the field set in $self->device_cache with the device.
478 # Undefined fields are filled in from the device, existing values in device_cache are written to the device.
479 # Without parameter, parses the whole $self->device_cache. Else, the parameter list is parsed as a list of
480 # field names. Contained fields for which have no corresponding getter/setter/device_cache entry exists will result in an exception thrown.
481 #
482 # old; replaced by _refresh_cache and _set_config_parameters
483 # still used in Yokogawa7651 and SignalRecovery726x
484 sub _cache_init {
485 0 ▶ my $self = shift;
Subroutine _cache_init: not called CC=8 SCAR=42.8
486 0 my $subname = shift;
487 0 ▶ my @ckeys = scalar(@_) > 0 ? @_ : keys %{ $self->device_cache() };
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 ? : 0 0 0
488  
489 #print Dumper $self->config();
490  
491 0 print "ckeys: @ckeys\n";
492  
493 # a key hash, to search for given keys quickly
494 0 my %ckeyhash;
495 my %orderhash;
496 0 @ckeyhash{@ckeys} = ();
497  
498 0 my @order = @{ $self->device_cache_order() };
499  
500 0 ▶ if ( $self->device_cache() && $self->connection() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->device_cache and $self->connection) 0 0 0
Condition0%0/3
$self->device_cache and $self->connection
!ll&&!rl&&r
000
MC/DC0%0/2
$self->device_cache and $self->connection
$self->device_cache $self->connection
Truth table0%0/3
A and B
A= $self->device_cacheB= $self->connection
ABresult
0X0
100
111
501  
502 # do we have a preferred order for device cache settings?
503 0 ▶ if (@order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) { } 0 0 0
504 0 @orderhash{@order} = ();
505 0 foreach my $ckey (@order) {
506 0 ▶ $self->_getset_key($ckey) if exists $ckeyhash{$ckey};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ckeyhash{$ckey} 0 0 0
507 }
508  
509 # initialize all values not in device_cache_order
510 #for my $ckey (@ckeys){
511 # $self->_getset_key($ckey) if not exists $orderhash{$ckey};
512 #}
513 }
514  
515 # no ordering requestd
516 else {
517 0 foreach my $ckey (@ckeys) {
518 0 $self->_getset_key($ckey);
519 }
520 }
521 }
522 }
523  
524 #
525 # Fill $self->device_settings() from config parameters
526 #
527 sub configure {
528 40 ▶ my $self = shift;
Subroutine configure: called CC=4 SCAR=13.9
Pod undocumented
529 40 my $config = shift;
530  
531 40 ▶ if ( ref($config) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config ne 'HASH') { } 0 40 40
532 0 Lab::Exception::CorruptParameter->throw(
533 error => 'Given Configuration is not a hash.' );
534 }
535 else {
536 #
537 # fill matching fields defined in %fields from the configuration hash ($self->config )
538 # this will also catch an explicitly given device_settings, default_device_settings (see Source.pm) or connection_settings hash ( overwritten default config )
539 #
540 40 for my $fields_key ( keys %{ $self->{_permitted} } ) {
541 { # restrict scope of "no strict"
542 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
543 $self->$fields_key( $config->{$fields_key} )
544 505 ▶ if exists $config->{$fields_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{$fields_key} 14 491 505
545 }
546 }
547  
548 #
549 # fill fields $self->device_settings and $self->device_cache from entries given in configuration hash (this is usually the same as $self->config )
550 #
551 40 $self->device_settings($config);
552  
553 #$self->device_cache($config);
554 }
555 }
556  
557 sub _checkconnection
558 { # Connection object or connection_type string (as in Lab::Connections::<connection_type>)
559 10 ▶ my $self = shift;
Subroutine _checkconnection: called CC=9 SCAR=26.7
560 10 ▶ my $connection = shift || undef;
Condition50%1/2
shift() || undef
l!l
100
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
561 10 my $found = 0;
562  
563 10 ▶ $connection = ref($connection) || $connection;
Condition66%2/3
ref $connection || $connection
l!l&&r!l&&!r
460
MC/DC0%0/2
ref $connection || $connection
ref $connection $connection
Truth table66%2/3
A || B
A= ref $connectionB= $connection
ABresult
1X1
011
000
564  
565 10 ▶ return 0 if !defined $connection;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $connection 0 10 10
566  
567 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
568 10 ▶ if ( grep( /^ALL$/, @{ $self->supported_connections() } ) == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(/^ALL$/u, @{$self->supported_connections;}) == 1) { } 0 10 10
elsif ($connection->isa('Lab::Connection::DEBUG') or $connection->isa('Lab::Connection::Mock')) { } 10 0 10
569 0 return $connection;
570 }
571 ▶ elsif ($connection->isa('Lab::Connection::DEBUG')
Condition66%2/3
$connection->isa("Lab::Connection::DEBUG") or $connection->isa("Lab::Connection::Mock")
l!l&&r!l&&!r
280
MC/DC0%0/2
$connection->isa("Lab::Connection::DEBUG") or $connection->isa("Lab::Connection::Mock")
$connection->isa("Lab::Connection::DEBUG") $connection->isa("Lab::Connection::Mock")
Truth table66%2/3
A or B
A= $connection->isa("Lab::Connection::DEBUG")B= $connection->isa("Lab::Connection::Mock")
ABresult
1X1
011
000
572 or $connection->isa('Lab::Connection::Mock') ) {
573 10 return $connection;
574 }
575 else {
576 0 for my $conn_supp ( @{ $self->supported_connections() } ) {
577 0 ▶ return $conn_supp
Branch: 1 branch
BranchTrueFalseTotal
if $connection->isa('Lab::Connection::' . $conn_supp) 0 0 0
578 if ( $connection->isa( 'Lab::Connection::' . $conn_supp ) );
579 }
580 }
581  
582 0 return undef;
583 }
584  
585 sub _setconnection
586 { # $self->setconnection() create new or use existing connection
587 10 ▶ my $self = shift;
Subroutine _setconnection: called CC=14 SCAR=29.3
588  
589 #
590 # fill in unset connection parameters with the defaults from $self->connections_settings to $self->config
591 #
592 10 my $config = $self->config();
593 10 my $connection_type = undef;
594 10 my $full_connection = undef;
595  
596 10 for my $setting_key ( keys %{ $self->connection_settings() } ) {
597 $config->{$setting_key} = $self->connection_settings($setting_key)
598 24 ▶ if !defined $config->{$setting_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{$setting_key} 20 4 24
599 }
600  
601 # check the configuration hash for a valid connection object or connection type, and set the connection
602 10 ▶ if ( defined( $self->config('connection') ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->config('connection')) { } 4 6 10
elsif (defined $self->config('connection_type')) { } 6 0 6
603  
604 4 ▶ if ( $self->_checkconnection( $self->config('connection') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_checkconnection($self->config('connection'))) { } 4 0 4
605 4 $self->connection( $self->config('connection') );
606  
607 }
608 else {
609 0 Lab::Exception::CorruptParameter->throw(
610 error => "Received invalid connection object!\n" );
611 }
612 }
613  
614 # else {
615 # Lab::Exception::CorruptParameter->throw( error => 'Received no connection object!\n' );
616 # }
617 elsif ( defined $self->config('connection_type') ) {
618  
619 6 $connection_type = $self->config('connection_type');
620  
621 6 ▶ if ( $connection_type !~ /^[A-Za-z0-9_\-\:]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection_type =~ /^[A-Za-z0-9_\-\:]*$/u) 0 6 6
622 0 Lab::Exception::CorruptParameter->throw( error =>
623 "Given connection type is does not look like a valid module name.\n"
624 );
625 }
626  
627 6 ▶ if ( $connection_type eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection_type eq "none") 0 6 6
628 0 ▶ if ( grep( /^none$/, @{ $self->supported_connections() } ) == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (grep(/^none$/u, @{$self->supported_connections;}) == 1) { } 0 0 0
629 {
630 0 return;
631 }
632 else {
633 0 Lab::Exception::Error->throw( error =>
634 "Sorry, this instrument cannot work without a connection.\n"
635 );
636 }
637 }
638  
639 6 $full_connection = "Lab::Connection::" . $connection_type;
640 6 eval("require ${full_connection};");
641 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
642 0 Lab::Exception::Error->throw( error =>
643 "Sorry, I was not able to load the connection ${full_connection}.\n"
644 . "The error received from the connections was\n===\n$@\n===\n"
645 );
646 }
647  
648 6 ▶ if ( $self->_checkconnection( "Lab::Connection::" . $connection_type )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_checkconnection('Lab::Connection::' . $connection_type)) { } 6 0 6
649 ) {
650  
651 # let's get creative
652 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
653  
654 # yep - pass all the parameters on to the connection, it will take the ones it needs.
655 # This way connection setup can be handled generically. Conflicting parameter names? Let's try it.
656 6 ▶ $self->connection( $full_connection->new($config) )
Branch: 1 branch
BranchTrueFalseTotal
$self->connection($full_connection->new($config)) or "Lab::Exception::Error"->throw("error", "Failed to create connection $full_connection!\n") 0 6 6
657 || Lab::Exception::Error->throw(
658 error => "Failed to create connection $full_connection!\n" );
659  
660 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
661 }
662 else {
663 0 Lab::Exception::CorruptParameter->throw(
664 error => "Given Connection not supported!\n" );
665 }
666 }
667 else {
668 0 Lab::Exception::CorruptParameter->throw( error =>
669 "Neither a connection nor a connection type was supplied.\n"
670 );
671 }
672  
673 # add predefined connection settings to connection config:
674 # no overwriting of user defined connection settings
675 10 my $new_config = $self->connection()->config();
676 10 for my $key ( keys %{ $self->connection_settings() } ) {
677 24 ▶ if ( not defined $self->connection()->config($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->connection->config($key)) 2 22 24
678 2 $new_config->{$key} = $self->connection_settings($key);
679 }
680 }
681 10 $self->connection()->config($new_config);
682 10 $self->connection()->_configurebus();
683 }
684  
685 sub _checkconfig {
686 0 ▶ my $self = shift;
Subroutine _checkconfig: not called CC=1 SCAR=6.9
687 0 my $config = $self->config();
688  
689 0 return 1;
690 }
691  
692 #
693 # To be overwritten...
694 # Returned $errcode has to be 0 for "no error"
695 #
696 sub get_error {
697 0 ▶ my $self = shift;
Subroutine get_error: not called CC=1 SCAR=6.9
Pod documented
698  
699 # overwrite with device specific error retrieval...
700 0 warn( "There was an error on the device "
701 . ref($self)
702 . ", but the driver is not able to supply more details.\n" );
703  
704 0 return ( -1, undef ); # ( $errcode, $message )
705 }
706  
707 #
708 # Optionally implement this to return a hash with device specific named status bits for this device, e.g. from the status byte/serial poll for GPIB
709 # return { ERROR => 1, READY => 1, DATA => 0, ... }
710 #
711 sub get_status {
712 0 ▶ my $self = shift;
Subroutine get_status: not called CC=1 SCAR=6.9
Pod documented
713 0 Lab::Exception::Unimplemented->throw(
714 "get_status() not implemented for " . ref($self) . ".\n" );
715 0 return undef;
716 }
717  
718 sub check_errors {
719 6 ▶ my $self = shift;
Subroutine check_errors: called CC=6 SCAR=30.5
Pod documented
720 6 my $command = shift;
721 6 my @errors = ();
722  
723 6 ▶ if ( $self->get_status()->{'ERROR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_status->{"ERROR"}) 0 6 6
724 0 my ( $code, $message ) = $self->get_error();
725 0 ▶ while ( $code != 0 && $code != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 0 and $code != -1) 0 0 0
Condition0%0/3
$code != 0 and $code != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$code != 0 and $code != -1
$code != 0 $code != -1
Truth table0%0/3
A and B
A= $code != 0B= $code != -1
ABresult
0X0
100
111
726 0 push @errors, [ $code, $message ];
727 0 warn
728 "\nReceived device error with code $code\nMessage: $message\n";
729 0 ( $code, $message ) = $self->get_error();
730 }
731  
732 0 ▶ if ( @errors || $code == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors or $code == -1) 0 0 0
Condition0%0/3
@errors or $code == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
@errors or $code == -1
@errors $code == -1
Truth table0%0/3
A or B
A= @errorsB= $code == -1
ABresult
1X1
011
000
733 0 Lab::Exception::DeviceError->throw(
734 error =>
735 "An Error occured in the device while executing the command: $command \n",
736 device_class => ref $self,
737 command => $command,
738 error_list => \@errors,
739 );
740 }
741 }
742 6 return 0;
743 }
744  
745 #
746 # Generic utility methods for string based connections (most common, SCPI etc.).
747 # For connections not based on command strings these should probably be overwritten/disabled!
748 #
749  
750 #
751 # passing through generic write, read and query from the connection.
752 #
753  
754 sub set_name {
755 0 ▶ my $self = shift;
Subroutine set_name: not called CC=1 SCAR=6.9
Pod undocumented
756 0 my ($name) = $self->_check_args( \@_, ['name'] );
757 0 $self->device_settings( { 'name' => $name } );
758  
759 }
760  
761 sub get_name {
762 2 ▶ my $self = shift;
Subroutine get_name: called CC=1 SCAR=0.0
Pod undocumented
763 2 return $self->device_settings('name');
764 }
765  
766 sub get_id {
767 2 ▶ my $self = shift;
Subroutine get_id: called CC=1 SCAR=0.0
Pod undocumented
768 2 my @name = split( /::/, ref($self) );
769 2 return pop(@name);
770 }
771  
772 ▶sub set_id {
Subroutine set_id: not called CC=1 SCAR=0.0
Pod undocumented
773  
774 }
775  
776 sub write {
777 83 ▶ my $self = shift;
Subroutine write: called CC=8 SCAR=21.6
Pod documented
778 83 ▶ my $command
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 == 0 && ref $_[1] ne 'HASH' ? : 0 83 83
Condition66%2/3
scalar(@_) % 2 == 0 and ref $_[1] ne "HASH"
!ll&&!rl&&r
61220
MC/DC0%0/2
scalar(@_) % 2 == 0 and ref $_[1] ne "HASH"
scalar(@_) % 2 == 0 ref $_[1] ne "HASH"
Truth table66%2/3
A and B
A= scalar(@_) % 2 == 0B= ref $_[1] ne "HASH"
ABresult
0X0
100
111
779 = scalar(@_) % 2 == 0 && ref $_[1] ne 'HASH'
780 ? undef
781 : shift
782 ; # even sized parameter list and second parm no hashref? => Assume parameter hash
783 83 ▶ my $args
Branch: 2 branches
BranchTrueFalseTotal
scalar(@_) % 2 == 0 ? : 61 22 83
ref $_[0] eq 'HASH' ? : 22 0 22
784 = scalar(@_) % 2 == 0
785 ? {@_}
786 : ( ref( $_[0] ) eq 'HASH' ? $_[0] : undef );
787 83 ▶ Lab::Exception::CorruptParameter->throw("Illegal parameter hash given!\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 83 83
788 if !defined($args);
789  
790 83 ▶ $args->{'command'} = $command if defined $command;
Branch: 1 branch
BranchTrueFalseTotal
if defined $command 83 0 83
791  
792 83 my $result = $self->connection()->Write($args);
793  
794 83 ▶ $self->check_errors( $args->{'command'} ) if $args->{error_check};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"error_check"} 6 77 83
795  
796 83 return $result;
797 }
798  
799 sub read {
800 4 ▶ my $self = shift;
Subroutine read: called CC=5 SCAR=17.8
Pod documented
801 4 ▶ my $args
Branch: 2 branches
BranchTrueFalseTotal
scalar(@_) % 2 == 0 ? : 4 0 4
ref $_[0] eq 'HASH' ? : 0 0 0
802 = scalar(@_) % 2 == 0
803 ? {@_}
804 : ( ref( $_[0] ) eq 'HASH' ? $_[0] : undef );
805 4 ▶ Lab::Exception::CorruptParameter->throw("Illegal parameter hash given!\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 4 4
806 if !defined($args);
807  
808 4 my $result = $self->connection()->Read($args);
809 $self->check_errors('Just a plain and simple read.')
810 4 ▶ if $args->{error_check};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"error_check"} 0 4 4
811  
812 4 $result =~ s/^[\r\t\n]+|[\r\t\n]+$//g;
813 4 return $result;
814 }
815  
816 sub clear {
817 1 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
818 1 $self->connection()->Clear();
819 }
820  
821 sub request {
822 18 ▶ my $self = shift;
Subroutine request: called CC=6 SCAR=22.6
Pod undocumented
823 18 my ( $command, $args ) = $self->parse_optional(@_);
824 my $read_mode
825 18 ▶ = ( defined $args->{'read_mode'} ) ? $args->{'read_mode'} : 'device';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'read_mode'} ? : 1 17 18
826  
827 # generate requestID from caller:
828 18 my ( $package, $filename, $line, $subroutine );
829 18 ( $package, $filename, $line, $subroutine ) = caller(1);
830 18 ( $package, $filename, $line ) = caller(0);
831 18 my $requestID
832 = $package . " " . $filename . " " . $subroutine . " " . $line;
833  
834 # # avoid to return an undef value:
835 18 ▶ if ( $read_mode eq 'request' and not defined $self->{requestID} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($read_mode eq 'request' and not defined $self->{'requestID'}) { } 0 18 18
elsif (defined $self->{'requestID'} and $self->{'requestID'} eq $requestID) { } 0 18 18
Condition33%1/3
$read_mode eq "request" and not defined $self->{"requestID"}
!ll&&!rl&&r
1800
MC/DC0%0/2
$read_mode eq "request" and not defined $self->{"requestID"}
$read_mode eq "request" not defined $self->{"requestID"}
Truth table33%1/3
A and not(B)
A= $read_mode eq "request"B= not defined $self->{"requestID"}
ABresult
0X0
100
111
836 0 $self->write(@_);
837 0 $self->connection()->block_connection();
838 0 $self->{requestID} = $requestID;
839 0 return undef;
840 }
841 ▶ elsif ( defined $self->{requestID} and $self->{requestID} eq $requestID )
Condition33%1/3
defined $self->{"requestID"} and $self->{"requestID"} eq $requestID
!ll&&!rl&&r
1800
MC/DC0%0/2
defined $self->{"requestID"} and $self->{"requestID"} eq $requestID
defined $self->{"requestID"} $self->{"requestID"} eq $requestID
Truth table33%1/3
A and B
A= defined $self->{"requestID"}B= $self->{"requestID"} eq $requestID
ABresult
0X0
100
111
842 {
843 0 $self->connection()->unblock_connection();
844 0 $self->{requestID} = undef;
845 0 return $self->read(@_);
846 }
847 else {
848 18 return $self->query(@_);
849 }
850  
851 }
852  
853 sub query {
854 166 ▶ my $self = shift;
Subroutine query: called CC=5 SCAR=16.6
Pod documented
855 166 my ( $command, $args ) = $self->parse_optional(@_);
856 my $read_mode
857 166 ▶ = ( defined $args->{'read_mode'} ) ? $args->{'read_mode'} : 'device';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'read_mode'} ? : 3 163 166
858 166 ▶ $args->{'command'} = $command if defined $command;
Branch: 1 branch
BranchTrueFalseTotal
if defined $command 166 0 166
859  
860 166 ▶ if ( not defined $args->{'command'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"command"}) 0 166 166
861 0 Lab::Exception::CorruptParameter->throw("No 'command' given!\n");
862 }
863  
864 166 my $result = $self->connection()->Query($args);
865 166 ▶ $self->check_errors( $args->{'command'} ) if $args->{error_check};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"error_check"} 0 166 166
866  
867 166 $result =~ s/^[\r\t\n]+|[\r\t\n]+$//g;
868 166 return $result;
869  
870 }
871  
872 #
873 # infrastructure stuff below
874 #
875  
876 #
877 # tool function to safely handle an optional scalar parameter in presence with a parameter hash/list
878 # only one optional scalar parameter can be handled, and its value must not be a hashref!
879 #
880 sub parse_optional {
881 184 ▶ my $self = shift;
Subroutine parse_optional: called CC=6 SCAR=18.8
Pod undocumented
882  
883 184 ▶ my $optional
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 == 0 && ref $_[1] ne 'HASH' ? : 0 184 184
Condition66%2/3
scalar(@_) % 2 == 0 and ref $_[1] ne "HASH"
!ll&&!rl&&r
321520
MC/DC0%0/2
scalar(@_) % 2 == 0 and ref $_[1] ne "HASH"
scalar(@_) % 2 == 0 ref $_[1] ne "HASH"
Truth table66%2/3
A and B
A= scalar(@_) % 2 == 0B= ref $_[1] ne "HASH"
ABresult
0X0
100
111
884 = scalar(@_) % 2 == 0 && ref $_[1] ne 'HASH'
885 ? undef
886 : shift
887 ; # even sized parameter list and second parm no hashref? => Assume parameter hash
888 184 ▶ my $args
Branch: 2 branches
BranchTrueFalseTotal
scalar(@_) % 2 == 0 ? : 32 152 184
ref $_[0] eq 'HASH' ? : 152 0 152
889 = scalar(@_) % 2 == 0
890 ? {@_}
891 : ( ref( $_[0] ) eq 'HASH' ? $_[0] : undef );
892 184 ▶ Lab::Exception::CorruptParameter->throw("Illegal parameter hash given!\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 184 184
893 if !defined($args);
894  
895 184 return $optional, $args;
896 }
897  
898 #
899 # accessor for device_settings
900 #
901 sub device_settings {
902 479 ▶ my $self = shift;
Subroutine device_settings: called CC=8 SCAR=22.9
Pod undocumented
903 479 my $value = undef;
904  
905 #warn "device_settings got this:\n" . Dumper(@_) . "\n";
906  
907 479 ▶ if ( scalar(@_) == 0 )
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 302 177 479
elsif (scalar @_ == 1) { } 177 0 177
elsif (scalar @_ > 1 and scalar(@_) % 2 == 0) { } 0 0 0
908 { # empty parameters - return whole device_settings hash
909 302 return $self->{'device_settings'};
910 }
911 elsif ( scalar(@_) == 1 )
912 { # one parm - either a scalar (key) or a hashref (try to merge)
913 177 $value = shift;
914 }
915 ▶ elsif ( scalar(@_) > 1 && scalar(@_) % 2 == 0 )
Condition0%0/3
scalar @_ > 1 and scalar(@_) % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ > 1 and scalar(@_) % 2 == 0
scalar @_ > 1 scalar(@_) % 2 == 0
Truth table0%0/3
A and B
A= scalar @_ > 1B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
916 { # even sized list - assume it's keys and values and try to merge it
917 0 $value = {@_};
918 }
919 else { # uneven sized list - don't know what to do with that one
920 0 Lab::Exception::CorruptParameter->throw(
921 error => "Corrupt parameters given to "
922 . __PACKAGE__
923 . "::device_settings().\n" );
924 }
925  
926 #warn "Keys present: \n" . Dumper($self->{device_settings}) . "\n";
927  
928 177 ▶ if ( ref($value) =~ /HASH/ ) { # it's a hash - merge into current settings
Branch: 1 branch
BranchTrueFalseTotal
if (ref($value) =~ /HASH/u) { } 40 137 177
929 40 for my $ext_key ( keys %{$value} ) {
930 $self->{'device_settings'}->{$ext_key} = $value->{$ext_key}
931 95 ▶ if ( exists( $self->device_settings()->{$ext_key} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->device_settings->{$ext_key} 16 79 95
932 }
933 40 return $self->{'device_settings'};
934 }
935 else { # it's a key - return the corresponding value
936 137 return $self->{'device_settings'}->{$value};
937 }
938 }
939  
940 #
941 # Accessor for device_cache settings
942 #
943  
944 sub device_cache {
945 604 ▶ my $self = shift;
Subroutine device_cache: called CC=8 SCAR=21.3
Pod undocumented
946 604 my $value = undef;
947  
948 #warn "device_cache got this:\n" . Dumper(@_) . "\n";
949  
950 604 ▶ if ( scalar(@_) == 0 )
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 204 400 604
elsif (scalar @_ == 1) { } 394 6 400
elsif (scalar @_ > 1 and scalar(@_) % 2 == 0) { } 6 0 6
951 { # empty parameters - return whole device_settings hash
952 204 return $self->{'device_cache'};
953 }
954 elsif ( scalar(@_) == 1 )
955 { # one parm - either a scalar (key) or a hashref (try to merge)
956 394 $value = shift;
957 }
958 ▶ elsif ( scalar(@_) > 1 && scalar(@_) % 2 == 0 )
Condition33%1/3
scalar @_ > 1 and scalar(@_) % 2 == 0
!ll&&!rl&&r
006
MC/DC0%0/2
scalar @_ > 1 and scalar(@_) % 2 == 0
scalar @_ > 1 scalar(@_) % 2 == 0
Truth table33%1/3
A and B
A= scalar @_ > 1B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
959 { # even sized list - assume it's keys and values and try to merge it
960 6 $value = {@_};
961 }
962 else { # uneven sized list - don't know what to do with that one
963 0 Lab::Exception::CorruptParameter->throw(
964 error => "Corrupt parameters given to "
965 . __PACKAGE__
966 . "::device_cache().\n" );
967 }
968  
969 #warn "Keys present: \n" . Dumper($self->{device_settings}) . "\n";
970  
971 400 ▶ if ( ref($value) =~ /HASH/ ) { # it's a hash - merge into current settings
Branch: 1 branch
BranchTrueFalseTotal
if (ref($value) =~ /HASH/u) { } 202 198 400
972 202 for my $ext_key ( keys %{$value} ) {
973 $self->{'device_cache'}->{$ext_key} = $value->{$ext_key}
974 202 ▶ if ( exists( $self->device_cache()->{$ext_key} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->device_cache->{$ext_key} 202 0 202
975 }
976 202 return $self->{'device_cache'};
977 }
978 else { # it's a key - return the corresponding value
979 198 return $self->{'device_cache'}->{$value};
980 }
981 }
982  
983 sub reset_device_cache {
984 1 ▶ my $self = shift;
Subroutine reset_device_cache: called CC=2 SCAR=6.9
Pod undocumented
985 1 my @cache_params = keys %{ $self->{'device_cache'} };
986 1 for my $param (@cache_params) {
987 6 $self->device_cache( $param => undef );
988 }
989 }
990  
991 #
992 # accessor for connection_settings
993 #
994 sub connection_settings {
995 42 ▶ my $self = shift;
Subroutine connection_settings: called CC=8 SCAR=30.1
Pod undocumented
996 42 my $value = undef;
997  
998 42 ▶ if ( scalar(@_) == 0 )
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 20 22 42
elsif (scalar @_ == 1) { } 22 0 22
elsif (scalar @_ > 1 and scalar(@_) % 2 == 0) { } 0 0 0
999 { # empty parameters - return whole device_settings hash
1000 20 return $self->{'connection_settings'};
1001 }
1002 elsif ( scalar(@_) == 1 )
1003 { # one parm - either a scalar (key) or a hashref (try to merge)
1004 22 $value = shift;
1005 }
1006 ▶ elsif ( scalar(@_) > 1 && scalar(@_) % 2 == 0 )
Condition0%0/3
scalar @_ > 1 and scalar(@_) % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ > 1 and scalar(@_) % 2 == 0
scalar @_ > 1 scalar(@_) % 2 == 0
Truth table0%0/3
A and B
A= scalar @_ > 1B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
1007 { # even sized list - assume it's keys and values and try to merge it
1008 0 $value = {@_};
1009 }
1010 else { # uneven sized list - don't know what to do with that one
1011 0 Lab::Exception::CorruptParameter->throw(
1012 error => "Corrupt parameters given to "
1013 . __PACKAGE__
1014 . "::connection_settings().\n" );
1015 }
1016  
1017 22 ▶ if ( ref($value) =~ /HASH/ ) { # it's a hash - merge into current settings
Branch: 1 branch
BranchTrueFalseTotal
if (ref($value) =~ /HASH/u) { } 0 22 22
1018 0 for my $ext_key ( keys %{$value} ) {
1019 $self->{'connection_settings'}->{$ext_key} = $value->{$ext_key}
1020 0 ▶ if ( exists( $self->{'connection_settings'}->{$ext_key} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"connection_settings"}{$ext_key} 0 0 0
1021  
1022 # warn "merge: set $ext_key to " . $value->{$ext_key} . "\n";
1023 }
1024 0 return $self->{'connection_settings'};
1025 }
1026 else { # it's a key - return the corresponding value
1027 22 return $self->{'connection_settings'}->{$value};
1028 }
1029 }
1030  
1031 sub _check_args {
1032 609 ▶ my $self = shift;
Subroutine _check_args: called CC=9 SCAR=22.0
1033 609 my $args = shift;
1034 609 my $params = shift;
1035  
1036 609 my $arguments = {};
1037  
1038 609 my $i = 0;
1039 609 foreach my $arg ( @{$args} ) {
1040 284 ▶ if ( ref($arg) ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg ne 'HASH') { } 131 153 284
1041 131 ▶ if ( defined @{$params}[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined @{$params;}[$i]) 113 18 131
1042 113 $arguments->{ @{$params}[$i] } = $arg;
1043 }
1044 131 $i++;
1045 }
1046 else {
1047 153 %{$arguments} = ( %{$arguments}, %{$arg} );
1048 153 $i++;
1049 }
1050 }
1051  
1052 609 my @return_args = ();
1053  
1054 609 foreach my $param ( @{$params} ) {
1055 476 ▶ if ( exists $arguments->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arguments->{$param}) { } 214 262 476
1056 214 push( @return_args, $arguments->{$param} );
1057 214 delete $arguments->{$param};
1058 }
1059 else {
1060 262 push( @return_args, undef );
1061 }
1062 }
1063  
1064 609 foreach my $param ( 'from_device', 'from_cache'
1065 ) # Delete Standard option parameters from $arguments hash if not defined in device driver function
1066 {
1067 1218 ▶ if ( exists $arguments->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arguments->{$param}) 0 1218 1218
1068 0 delete $arguments->{$param};
1069 }
1070 }
1071  
1072 609 push( @return_args, $arguments );
1073  
1074 609 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 609 0 609
1075 609 return @return_args;
1076 }
1077 else {
1078 0 return $return_args[0];
1079 }
1080  
1081 }
1082  
1083 sub _check_args_strict {
1084 19 ▶ my $self = shift;
Subroutine _check_args_strict: called CC=4 SCAR=14.4
1085 19 my $args = shift;
1086 19 my $params = shift;
1087  
1088 19 my @result = $self->_check_args( $args, $params );
1089  
1090 19 my $num_params = @result - 1;
1091  
1092 19 ▶ for ( my $i = 0; $i < $num_params; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_params) 19 19 38
1093 19 ▶ if ( not defined $result[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result[$i]) 0 19 19
1094 0 croak("missing mandatory argument '$params->[$i]'");
1095 }
1096 }
1097  
1098 19 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 19 0 19
1099 19 return @result;
1100 }
1101 else {
1102 0 return $result[0];
1103 }
1104 }
1105  
1106 #
1107 # config gets it's own accessor - convenient access like $self->config('GPIB_Paddress') instead of $self->config()->{'GPIB_Paddress'}
1108 # with a hashref as argument, set $self->{'config'} to the given hashref.
1109 # without an argument it returns a reference to $self->config (just like AUTOLOAD would)
1110 #
1111 sub config { # $value = self->config($key);
1112 143 ▶ ( my $self, my $key ) = ( shift, shift );
Subroutine config: called CC=3 SCAR=11.0
Pod undocumented
1113  
1114 143 ▶ if ( !defined $key ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $key) { } 60 83 143
elsif (ref($key) =~ /HASH/u) { } 10 73 83
1115 60 return $self->{'config'};
1116 }
1117 elsif ( ref($key) =~ /HASH/ ) {
1118 10 return $self->{'config'} = $key;
1119 }
1120 else {
1121 73 return $self->{'config'}->{$key};
1122 }
1123 }
1124  
1125 # sub device_cache { # $value = $self->{'device_cache'}($key);
1126 # (my $self, my $key) = (shift, shift);
1127  
1128 # if(!defined $key) {
1129 # return $self->{'device_cache'};
1130 # }
1131 # elsif(ref($key) =~ /HASH/) {
1132 # return $self->{'device_cache'} = ($self->{'device_cache'}, $key);
1133 # }
1134 # else {
1135 # return $self->{'device_cache'}->{$key};
1136 # }
1137 # }
1138  
1139 #
1140 # provides generic accessor methods to the fields defined in %fields and to the elements of $self->device_settings
1141 #
1142 sub AUTOLOAD {
1143  
1144 1232 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=9 SCAR=28.1
1145 1232 ▶ my $type = ref($self) or croak "\$self is not an object";
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("\$self is not an object") 0 1232 1232
1146 1232 my $value = undef;
1147  
1148 1232 my $name = $AUTOLOAD;
1149 1232 $name =~ s/.*://; # strip fully qualified portion
1150  
1151 1232 ▶ if ( exists $self->{_permitted}->{$name} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'_permitted'}{$name}) { } 1226 6 1232
elsif ($name =~ qr/^(get_|set_)(.*)$/u) { } 6 0 6
elsif (exists $self->{'device_settings'}{$name}) { } 0 0 0
1152 1226 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 31 1195 1226
1153 31 return $self->{$name} = shift;
1154 }
1155 else {
1156 1195 return $self->{$name};
1157 }
1158 }
1159 elsif ( $name =~ qr/^(get_|set_)(.*)$/ ) {
1160 6 ▶ if ( exists $self->device_settings()->{$2} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->device_settings->{$2}) { } 6 0 6
elsif (exists $self->device_cache->{$2}) { } 0 0 0
1161 6 return $self->getset( $1, $2, "device_settings", @_ );
1162 }
1163 elsif ( exists $self->device_cache()->{$2} ) {
1164 0 return $self->getset( $1, $2, "device_cache", @_ );
1165 }
1166 else {
1167 0 Lab::Exception::Warning->throw( error =>
1168 "AUTOLOAD could not find var for getter/setter: $name \n"
1169 );
1170 }
1171 }
1172 elsif ( exists $self->{'device_settings'}->{$name} ) {
1173 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
1174 0 return $self->{'device_settings'}->{$name} = shift;
1175 }
1176 else {
1177 0 return $self->{'device_settings'}->{$name};
1178 }
1179 }
1180 else {
1181 0 Lab::Exception::Warning->throw( error => "AUTOLOAD in "
1182 . __PACKAGE__
1183 . " couldn't access field '${name}'.\n" );
1184 }
1185 }
1186  
1187 # needed so AUTOLOAD doesn't try to call DESTROY on cleanup and prevent the inherited DESTROY
1188 sub DESTROY {
1189 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
1190  
1191 #$self->connection()->DESTROY();
1192 0 ▶ $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::DESTROY") 0 0 0
1193 }
1194  
1195 sub getset {
1196 6 ▶ my $self = shift;
Subroutine getset: called CC=6 SCAR=27.4
Pod undocumented
1197 6 my $gs = shift;
1198 6 my $varname = shift;
1199 6 my $subfield = shift;
1200 6 ▶ if ( $gs eq 'set_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gs eq 'set_') { } 0 6 6
1201 0 my $value = shift;
1202 0 ▶ if ( !defined $value || ref($value) ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value ne "") 0 0 0
Condition0%0/3
not defined $value or ref $value ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or ref $value ne ""
not defined $value ref $value ne ""
Truth table0%0/3
not(A) or B
A= not defined $valueB= ref $value ne ""
ABresult
1X1
011
000
1203 0 Lab::Exception::CorruptParameter->throw( error =>
1204 "No or no scalar value given to generic set function $AUTOLOAD in "
1205 . __PACKAGE__
1206 . "::AUTOLOAD().\n" );
1207 }
1208 0 ▶ if ( @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 0 0
1209 0 Lab::Exception::CorruptParameter->throw( error =>
1210 "Too many values given to generic set function $AUTOLOAD "
1211 . __PACKAGE__
1212 . "::AUTOLOAD().\n" );
1213 }
1214 0 return $self->{$subfield}->{$varname} = $value;
1215 }
1216 else {
1217 6 ▶ if ( @_ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 6 6
1218 0 Lab::Exception::CorruptParameter->throw( error =>
1219 "Too many values given to generic get function $AUTOLOAD "
1220 . __PACKAGE__
1221 . "::AUTOLOAD().\n" );
1222 }
1223 6 return $self->{$subfield}->{$varname};
1224 }
1225 }
1226  
1227 #
1228 # This is a hook which is called after connection initialization and before the device cache is synced (see _construct).
1229 # Necessary for some devices to put them into e.g. remote control mode or otherwise enable communication.
1230 # Overwrite this if needed.
1231 #
1232 ▶sub _device_init {
Subroutine _device_init: called CC=1 SCAR=0.0
1233 }
1234  
1235 #
1236 # This tool just returns the index of the element in the provided list
1237 #
1238  
1239 sub function_list_index {
1240 0 ▶ 1 while $_[0] ne pop;
Subroutine function_list_index: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ne pop() and do { '???' } 0 0 0
1241 0 $#_;
1242 }
1243  
1244 # sub WriteConfig {
1245 # my $self = shift;
1246 #
1247 # my %config = @_;
1248 # %config = %{$_[0]} if (ref($_[0]));
1249 #
1250 # my $command = "";
1251 # # function characters init
1252 # my $inCommand = "";
1253 # my $betweenCmdAndData = "";
1254 # my $postData = "";
1255 # # config data
1256 # if (exists $self->{'CommandRules'}) {
1257 # # write stating value by default to command
1258 # $command = $self->{'CommandRules'}->{'preCommand'}
1259 # if (exists $self->{'CommandRules'}->{'preCommand'});
1260 # $inCommand = $self->{'CommandRules'}->{'inCommand'}
1261 # if (exists $self->{'CommandRules'}->{'inCommand'});
1262 # $betweenCmdAndData = $self->{'CommandRules'}->{'betweenCmdAndData'}
1263 # if (exists $self->{'CommandRules'}->{'betweenCmdAndData'});
1264 # $postData = $self->{'CommandRules'}->{'postData'}
1265 # if (exists $self->{'CommandRules'}->{'postData'});
1266 # }
1267 # # get command if sub call from itself
1268 # $command = $_[1] if (ref($_[0]));
1269 #
1270 # # build up commands buffer
1271 # foreach my $key (keys %config) {
1272 # my $value = $config{$key};
1273 #
1274 # # reference again?
1275 # if (ref($value)) {
1276 # $self->WriteConfig($value,$command.$key.$inCommand);
1277 # } else {
1278 # # end of search
1279 # $self->Write($command.$key.$betweenCmdAndData.$value.$postData);
1280 # }
1281 # }
1282 #
1283 # }
1284  
1285 # =head2 WriteConfig
1286 #
1287 # this is NOT YET IMPLEMENTED in this base class so far
1288 #
1289 # $instrument->WriteConfig( 'TRIGGER' => { 'SOURCE' => 'CHANNEL1',
1290 # 'EDGE' => 'RISE' },
1291 # 'AQUIRE' => 'HRES',
1292 # 'MEASURE' => { 'VRISE' => 'ON' });
1293 #
1294 # Builds up the commands and sends them to the instrument. To get the correct
1295 # format a
1296 # command rules hash has to be set up by the driver package
1297 #
1298 # e.g. for SCPI commands
1299 # $instrument->{'CommandRules'} = {
1300 # 'preCommand' => ':',
1301 # 'inCommand' => ':',
1302 # 'betweenCmdAndData' => ' ',
1303 # 'postData' => '' # empty entries can be skipped
1304 # };
1305 #
1306 #
1307 #
1308  
1309  
1310 9 1;
1311  
1312 __END__
1313  
1314 =pod
1315  
1316 =encoding utf-8
1317  
1318 =head1 NAME
1319  
1320 Lab::Instrument - Instrument base class
1321  
1322 =head1 VERSION
1323  
1324 version 3.881
1325  
1326 =head1 SYNOPSIS
1327  
1328 Lab::Instrument is meant to be used as a base class for inheriting instruments.
1329 For very simple applications it can also be used directly, like
1330  
1331 $generic_instrument = new Lab::Instrument ( connection_type => VISA_GPIB, gpib_address => 14 );
1332 my $idn = $generic_instrument->query('*IDN?');
1333  
1334 Every inheriting class constructor should start as follows:
1335  
1336 sub new {
1337 my $proto = shift;
1338 my $class = ref($proto) || $proto;
1339 my $self = $class->SUPER::new(@_);
1340 $self->${\(__PACKAGE__.'::_construct')}(__PACKAGE__); # check for supported connections, initialize fields etc.
1341 ...
1342 }
1343  
1344 Beware that only the first set of parameters specific to an individual GPIB
1345 board or any other bus hardware gets used. Settings for EOI assertion for
1346 example.
1347  
1348 If you know what you're doing or you have an exotic scenario you can use the
1349 connection parameter "ignore_twins => 1" to force the creation of a new bus
1350 object, but this is discouraged - it will kill bus management and you might run
1351 into hardware/resource sharing issues.
1352  
1353 =head1 DESCRIPTION
1354  
1355 C<Lab::Instrument> is the base class for Instruments. It doesn't do much by
1356 itself, but is meant to be inherited in specific instrument drivers. It provides
1357 general C<read>, C<write> and C<query> methods and basic connection handling
1358 (internally, C<_set_connection>, C<_check_connection>).
1359  
1360 =head1 CONSTRUCTOR
1361  
1362 =head2 new
1363  
1364 This blesses $self (don't do it yourself in an inheriting class!), initializes
1365 the basic "fields" to be accessed via AUTOLOAD and puts the configuration hash
1366 in $self->config to be accessed in methods and inherited classes.
1367  
1368 Arguments: just the configuration hash (or even-sized list) passed along from a
1369 child class constructor.
1370  
1371 =head1 METHODS
1372  
1373 =head2 write
1374  
1375 $instrument->write($command <, {optional hashref/hash}> );
1376  
1377 Sends the command C<$command> to the instrument. An option hash can be supplied
1378 as second or also as only argument. Generally, all options are passed to the
1379 connection/bus, so additional named options may be supported based on the
1380 connection and bus and can be passed as a hashref or hash. See
1381 L<Lab::Connection>.
1382  
1383 Optional named parameters for hash:
1384  
1385 error_check => 1/0
1386  
1387 Invoke $instrument->check_errors after write. Defaults to off.
1388  
1389 =head2 read
1390  
1391 $result=$instrument->read({ read_length => <max length>, brutal => <1/0>);
1392  
1393 Reads a result of C<ReadLength> from the instrument and returns it. Returns an
1394 exception on error.
1395  
1396 If the parameter C<brutal> is set, a timeout in the connection will not result
1397 in an Exception thrown, but will return the data obtained until the timeout
1398 without further comment. Be aware that this data is also contained in the the
1399 timeout exception object (see C<Lab::Exception>).
1400  
1401 Generally, all options are passed to the connection/bus, so additional named
1402 options may be supported based on the connection and bus and can be passed as a
1403 hashref or hash. See L<Lab::Connection>.
1404  
1405 =head2 query
1406  
1407 $result=$instrument->query({ command => $command,
1408 wait_query => $wait_query,
1409 read_length => $read_length);
1410  
1411 Sends the command C<$command> to the instrument and reads a result from the
1412 instrument and returns it. The length of the read buffer is set to
1413 C<read_length> or to the default set in the connection.
1414  
1415 Waits for C<wait_query> microseconds before trying to read the answer.
1416  
1417 Generally, all options are passed to the connection/bus, so additional named
1418 options may be supported based on the connection and bus and can be passed as a
1419 hashref or hash. See L<Lab::Connection>.
1420  
1421 =head2 get_error
1422  
1423 ($errcode, $errmsg) = $instrument->get_error();
1424  
1425 Method stub to be overwritten. Implementations read one error (and message, if
1426 available) from the device.
1427  
1428 =head2 get_status
1429  
1430 $status = $instrument->get_status();
1431 if( $instrument->get_status('ERROR') ) {...}
1432  
1433 Method stub to be overwritten. This returns the status reported by the device
1434 (e.g. the status byte retrieved via serial poll from GPIB devices). When
1435 implementing, use only information which can be retrieved very fast from the
1436 device, as this may be used often.
1437  
1438 Without parameters, has to return a hashref with named status bits, e.g.
1439  
1440 $status => {
1441 ERROR => 1,
1442 DATA => 0,
1443 READY => 1
1444 }
1445  
1446 If present, the first argument is interpreted as a key and the corresponding
1447 value of the hash above is returned directly.
1448  
1449 The 'ERROR'-key has to be implemented in every device driver!
1450  
1451 =head2 check_errors
1452  
1453 $instrument->check_errors($last_command);
1454
1455 # try
1456 eval { $instrument->check_errors($last_command) };
1457 # catch
1458 if ( my $e = Exception::Class->caught('Lab::Exception::DeviceError')) {
1459 warn "Errors from device!";
1460 @errors = $e->error_list();
1461 @devtype = $e->device_class();
1462 $command = $e->command();
1463 }
1464  
1465 Uses get_error() to check the device for occured errors. Reads all present
1466 errors and throws a Lab::Exception::DeviceError. The list of errors, the device
1467 class and the last issued command(s) (if the script provided them) are enclosed.
1468  
1469 =head2 _check_args
1470  
1471 Parse the arguments given to a method. The typical use is like this:
1472  
1473 sub my_method () {
1474 my $self = shift;
1475 my ($arg_1, $arg_2, $tail) = $self->_check_args(\@_, ['arg1', 'arg2']);
1476 ...
1477 }
1478  
1479 There are now two ways, how a user can give arguments to C<my_method>. Both of
1480 the following calls will assign C<$value1> to C<$arg1> and C<$value2> to C<$arg2>.
1481  
1482 =over
1483  
1484 =item old style:
1485  
1486 $instrument->my_method($value1, $value2, $tail);
1487  
1488 =item new style:
1489  
1490 $instrument->my_method({arg1 => $value1, arg2 => $value2});
1491  
1492 Remaining key-value pairs will be consumed by C<$tail>. For example, after
1493  
1494 $instrument->my_method({arg1 => $value1, arg2 => $value2, x => $value_x});
1495  
1496 C<$tail> will hold the hashref C<< {x => $value_x} >>.
1497  
1498 Multiple hashrefs given to C<my_method> are concatenated.
1499  
1500 For a method without named arguments, you can either use
1501  
1502 my ($tail) = $self->_check_args(\@_, []);
1503  
1504 or
1505  
1506 my ($tail) = $self->_check_args(\@);
1507  
1508 =back
1509  
1510 =head2 _check_args_strict
1511  
1512 Like C<_check_args>, but makes all declared arguments mandatory.
1513  
1514 If an argument does not
1515 receive a non-undef value, this will throw an exception. Thus, the returned
1516 array will never contain undefined values.
1517  
1518 =head1 CAVEATS/BUGS
1519  
1520 Probably many, with all the porting. This will get better.
1521  
1522 =head1 SEE ALSO
1523  
1524 =over 4
1525  
1526 =item * L<Lab::Bus>
1527  
1528 =item * L<Lab::Connection>
1529  
1530 =item * L<Lab::Instrument::HP34401A>
1531  
1532 =item * L<Lab::Instrument::HP34970A>
1533  
1534 =item * L<Lab::Instrument::Source>
1535  
1536 =item * L<Lab::Instrument::Yokogawa7651>
1537  
1538 =item * and many more...
1539  
1540 =back
1541  
1542 =head1 COPYRIGHT AND LICENSE
1543  
1544 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
1545  
1546 Copyright 2005-2006 Daniel Schroeer
1547 2009 Andreas K. Huettel
1548 2010 Andreas K. Huettel, Daniel Schroeer, Florian Olbrich, Matthias Voelker
1549 2011 Andreas K. Huettel, Florian Olbrich
1550 2012 Alois Dirnaichner, Andreas K. Huettel, Christian Butschkow, Florian Olbrich, Stefan Geissler
1551 2013 Alois Dirnaichner, Andreas K. Huettel, Christian Butschkow, Stefan Geissler
1552 2014 Alexei Iankilevitch, Christian Butschkow
1553 2016 Charles Lane, Simon Reinhardt
1554 2017 Andreas K. Huettel
1555 2019 Simon Reinhardt
1556 2020 Andreas K. Huettel
1557  
1558  
1559 This is free software; you can redistribute it and/or modify it under
1560 the same terms as the Perl 5 programming language system itself.
1561  
1562 =cut
« XPRESS/Data/XPRESS_logger.pm ↑ Summary Bus/MODBUS_RS232.pm »