Moose/Connection/VISA.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 160 SCAR 42.8
CC
8
Cov
0%
Read
20.0
_handle_status
12.0
_set_timeout
6.0
CRAP 72.0
1 package Lab::Moose::Connection::VISA;
2 $Lab::Moose::Connection::VISA::VERSION = '3.881';
3 #ABSTRACT: Connection back end to National Instruments' VISA library.
4  
5 use v5.20;
6  
7  
8  
9 use Moose;
10 use MooseX::Params::Validate;
11 use Carp;
12  
13 use Lab::Moose::Instrument qw/timeout_param read_length_param/;
14  
15 use Lab::VISA;
16  
17 use namespace::autoclean;
18  
19 use constant {
20 VI_TRUE => $Lab::VISA::VI_TRUE,
21 VI_NULL => $Lab::VISA::VI_NULL,
22 VI_SUCCESS => $Lab::VISA::VI_SUCCESS,
23 VI_SUCCESS_TERM_CHAR => $Lab::VISA::VI_SUCCESS_TERM_CHAR,
24 VI_SUCCESS_MAX_CNT => $Lab::VISA::VI_SUCCESS_MAX_CNT,
25 VI_ATTR_TMO_VALUE => $Lab::VISA::VI_ATTR_TMO_VALUE,
26 VI_ATTR_TERMCHAR => $Lab::VISA::VI_ATTR_TERMCHAR,
27 VI_ATTR_TERMCHAR_EN => $Lab::VISA::VI_ATTR_TERMCHAR_EN,
28 };
29  
30 has resource_name => (
31 is => 'ro',
32 isa => 'Str',
33 required => 1,
34 );
35  
36 has handle => (
37 is => 'ro',
38 isa => 'Int',
39 writer => '_handle',
40 init_arg => undef,
41 );
42  
43 # Timeout set on controller
44 has current_timeout => (
45 is => 'ro',
46 isa => 'Num',
47 init_arg => undef,
48 writer => '_current_timeout',
49 );
50  
51 # FIXME: do timeout stuff like LinuxGPIB connection.
52  
53 sub _timeout_to_ms {
54 my $timeout = shift;
55 return sprintf( "%.0f", $timeout * 1000 );
56 }
57  
58 sub _set_timeout {
59 my ( $self, %args ) = validated_hash(
60 \@_,
61 timeout => { isa => 'Num' },
62 );
63  
64 my $timeout = $args{timeout};
65 my $ms_value = _timeout_to_ms($timeout);
66 my $current_timeout = $self->current_timeout();
67 my $current_ms_value = _timeout_to_ms($current_timeout);
68  
69 if ( $ms_value != $current_ms_value ) {
70 $self->_set_visa_attribute( attribute => VI_ATTR_TMO_VALUE, value => $ms_value );
71 $self->_current_timeout($timeout);
72 }
73 }
74  
75 sub _handle_status {
76 my $self = shift;
77 my $status = shift;
78 if ( $status != VI_SUCCESS ) {
79 if (@_) {
80 croak( "Lab::VISA error $status: ", @_ );
81 }
82 else {
83 croak("Lab::VISA error $status");
84 }
85 }
86 }
87  
88 sub _set_visa_attribute {
89 my $self = shift;
90 my ( $attribute, $value ) = validated_list(
91 \@_,
92 attribute => { isa => 'Int' },
93 value => { isa => 'Int' },
94 );
95 my $handle = $self->handle();
96 my $status = Lab::VISA::viSetAttribute( $handle, $attribute, $value );
97 $self->_handle_status( $status, "viSetAttribute" );
98 }
99  
100  
101 sub set_termchar {
102 my ( $self, %args ) = validated_hash(
103 \@_,
104 timeout_param,
105 termchar => { isa => 'Str' },
106 );
107  
108 my $timeout = $self->_timeout_arg(%args);
109 $self->_set_timeout( timeout => $timeout );
110 my $termchar = ord( $args{termchar} );
111 $self->_set_visa_attribute( attribute => VI_ATTR_TERMCHAR, value => $termchar );
112 }
113  
114  
115 sub enable_read_termchar {
116 my ( $self, %args ) = validated_hash(
117 \@_,
118 timeout_param
119 );
120 my $timeout = $self->_timeout_arg(%args);
121 $self->_set_timeout( timeout => $timeout );
122 $self->_set_visa_attribute( attribute => VI_ATTR_TERMCHAR_EN, value => VI_TRUE );
123 }
124  
125 sub gen_resource_name {
126 return shift->resource_name();
127 }
128  
129 sub BUILD {
130 my $self = shift;
131 my $resource_name = $self->gen_resource_name();
132 my ( $status, $rm ) = Lab::VISA::viOpenDefaultRM();
133 $self->_handle_status( $status, "viOpenDefaultRM" );
134  
135 ( $status, my $handle )
136 = Lab::VISA::viOpen( $rm, $resource_name, VI_NULL, VI_NULL );
137 $self->_handle_status( $status, "viOpen" );
138 $self->_handle($handle);
139  
140 my $timeout = $self->timeout();
141 $self->_current_timeout($timeout);
142 }
143  
144  
145 sub Read {
146 my ( $self, %args ) = validated_hash(
147 \@_,
148 timeout_param,
149 read_length_param,
150 );
151  
152 my $timeout = $self->_timeout_arg(%args);
153 $self->_set_timeout( timeout => $timeout );
154  
155 my $read_length = $self->_read_length_arg(%args);
156 my $handle = $self->handle();
157 my $result = '';
158 while ($read_length) {
159 my ( $status, $data, $length )
160 = Lab::VISA::viRead( $handle, $read_length );
161 if ( $status != VI_SUCCESS
162 and $status != VI_SUCCESS_TERM_CHAR
163 and $status != VI_SUCCESS_MAX_CNT ) {
164 croak("Lab::VISA error $status: viRead");
165 }
166  
167 if ( length($data) != $length ) {
168 croak "length(data) != length";
169 }
170  
171 $result .= $data;
172 $read_length -= length($data);
173  
174 if ( $status == VI_SUCCESS
175 or $status == VI_SUCCESS_TERM_CHAR
176 or $status == VI_SUCCESS_MAX_CNT ) {
177 last;
178 }
179 }
180 return $result;
181 }
182  
183 sub Write {
184 my ( $self, %args ) = validated_hash(
185 \@_,
186 timeout_param,
187 command => { isa => 'Str' },
188 );
189  
190 my $timeout = $self->_timeout_arg(%args);
191 $self->_set_timeout( timeout => $timeout );
192  
193 my $command = $args{command};
194 my $length = length($command);
195  
196 my $handle = $self->handle();
197 my ( $status, $bytes_written )
198 = Lab::VISA::viWrite( $handle, $command, $length );
199 $self->_handle_status( $status, "viWrite" );
200 if ( $bytes_written != $length ) {
201 croak "viWrite: written: $bytes_written, length: $length";
202 }
203 }
204  
205 sub Clear {
206 my ( $self, %args ) = validated_hash(
207 \@_,
208 timeout_param,
209 );
210  
211 my $timeout = $self->_timeout_arg(%args);
212 $self->_set_timeout( timeout => $timeout );
213  
214 my $handle = $self->handle();
215 my $status = Lab::VISA::viClear($handle);
216 $self->_handle_status( $status, "viClear" );
217 }
218  
219 with 'Lab::Moose::Connection';
220  
221 __PACKAGE__->meta->make_immutable();
222  
223 1;
224  
225 __END__
226  
227 =pod
228  
229 =encoding UTF-8
230  
231 =head1 NAME
232  
233 Lab::Moose::Connection::VISA - Connection back end to National Instruments' VISA library.
234  
235 =head1 VERSION
236  
237 version 3.881
238  
239 =head1 SYNOPSIS
240  
241 use Lab::Moose
242
243 my $instrument = instrument(
244 type => 'random_instrument',
245 connection_type => 'VISA',
246 connection_options => {resource_name => $resource_name}
247 );
248  
249 =head2 set_termchar
250  
251 $connection->set_termchar(termchar => "\r");
252  
253 Set the end-of-string byte
254  
255 =head2 enable_read_termchar
256  
257 $connection->enable_read_termchar();
258  
259 Enable termination of reads when eos character is received.
260  
261 =head1 METHODS
262  
263 =head1 COPYRIGHT AND LICENSE
264  
265 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
266  
267 Copyright 2017 Simon Reinhardt
268 2019 Simon Reinhardt
269 2020 Andreas K. Huettel
270  
271  
272 This is free software; you can redistribute it and/or modify it under
273 the same terms as the Perl 5 programming language system itself.
274  
275 =cut