Connection/VISA_RS232.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 112 SCAR 50.5
CC
12
Cov
0%
_configurebus
132.0
_setbus
6.0
new
2.0
CRAP 156.0
1 package Lab::Connection::VISA_RS232;
2 #ABSTRACT: RS232-type connection which uses NI VISA (L<Lab::VISA>) as backend
3 $Lab::Connection::VISA_RS232::VERSION = '3.881';
4 use v5.20;
5  
6 use strict;
7 use Lab::Bus::VISA;
8 use Lab::Connection;
9 use Lab::Exception;
10  
11 our @ISA = ("Lab::Connection");
12  
13 our %fields = (
14 bus_class => 'Lab::Bus::VISA',
15 resource_name => undef,
16 brutal => 0,
17 timeout => 2,
18 wait_status => 0, # sec;
19 wait_query => 10e-6, # sec;
20 read_length => 1000, # bytes
21 rs232_address => undef,
22 );
23  
24 sub new {
25 my $proto = shift;
26 my $class = ref($proto) || $proto;
27 my $twin = undef;
28  
29 my $self = $class->SUPER::new(@_)
30 ; # getting fields and _permitted from parent class, parameter checks
31 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
32  
33 return $self;
34 }
35  
36 #
37 # adapting bus setup to VISA
38 #
39 sub _setbus {
40 my $self = shift;
41 my $bus_class = $self->bus_class();
42  
43 no strict 'refs';
44 $self->bus( $bus_class->new( $self->config() ) )
45 || Lab::Exception::Error->throw(
46 error => "Failed to create bus $bus_class in "
47 . __PACKAGE__
48 . "::_setbus.\n" );
49 use strict;
50  
51 #
52 # build VISA resource name
53 #
54 if ( not defined $self->rs232_address() ) {
55 Lab::Exception::UndefinedField->throw(
56 error => 'No rs232 address defined !' );
57 }
58  
59 my $resource_name = "ASRL";
60 $resource_name .= $self->rs232_address();
61 $resource_name .= '::INSTR';
62 $self->resource_name($resource_name);
63 $self->config()->{'resource_name'} = $resource_name;
64  
65 # again, pass it all.
66 $self->connection_handle(
67 $self->bus()->connection_new( $self->config() ) );
68  
69 return $self->bus();
70  
71 }
72  
73 sub _configurebus {
74 my $self = shift;
75  
76 #
77 # set VISA Attributes:
78 #
79  
80 # boudrate
81 $self->bus()->set_visa_attribute(
82 $self->connection_handle(),
83 $Lab::VISA::VI_ATTR_ASRL_BAUD, $self->config()->{baudrate}
84 );
85  
86 # databits
87 $self->bus()->set_visa_attribute(
88 $self->connection_handle(),
89 $Lab::VISA::VI_ATTR_ASRL_DATA_BITS,
90 $self->config()->{databits}
91 );
92  
93 # parity
94 my $parity;
95 if ( $self->config()->{parity} eq 'none' ) { $parity = 0; }
96 elsif ( $self->config()->{parity} eq 'odd' ) { $parity = 1; }
97 elsif ( $self->config()->{parity} eq 'even' ) { $parity = 2; }
98 elsif ( $self->config()->{parity} eq 'mark' ) { $parity = 3; }
99 elsif ( $self->config()->{parity} eq 'space' ) { $parity = 4; }
100 else { $parity = $self->config()->{parity}; }
101 $self->bus()->set_visa_attribute(
102 $self->connection_handle(),
103 $Lab::VISA::VI_ATTR_ASRL_PARITY, $parity
104 );
105  
106 # stop bits
107 my $stopbits;
108 if ( $self->config()->{stopbits} == 1 ) {
109 $stopbits = $Lab::VISA::VI_ASRL_STOP_ONE;
110 }
111 elsif ( $self->config()->{stopbits} == 1.5 ) {
112 $stopbits = $Lab::VISA::VI_ASRL_STOP_ONE5;
113 }
114 elsif ( $self->config()->{stopbits} == 2 ) {
115 $stopbits = $Lab::VISA::VI_ASRL_STOP_TWO;
116 }
117 else {
118 $stopbits = $self->config()->{stopbits};
119 }
120 $self->bus()->set_visa_attribute(
121 $self->connection_handle(),
122 $Lab::VISA::VI_ATTR_ASRL_STOP_BITS, $stopbits
123 );
124  
125 # termination character
126 if ( defined $self->config()->{termchar} ) {
127 if ( $self->config()->{termchar} =~ m/^[0-9]+$/ ) {
128 $self->config()->{termchar} = chr( $self->config()->{termchar} );
129 }
130 $self->bus()->set_visa_attribute(
131 $self->connection_handle(),
132 $Lab::VISA::VI_ATTR_TERMCHAR_EN,
133 $Lab::VISA::VI_TRUE
134 );
135 $self->bus()->set_visa_attribute(
136 $self->connection_handle(),
137 $Lab::VISA::VI_ATTR_TERMCHAR, ord( $self->config()->{termchar} )
138 );
139 }
140 else {
141 $self->bus()->set_visa_attribute(
142 $self->connection_handle(),
143 $Lab::VISA::VI_ATTR_TERMCHAR_EN,
144 $Lab::VISA::VI_FALSE
145 );
146 }
147  
148 # read timeout
149 $self->bus()->set_visa_attribute(
150 $self->connection_handle(),
151 $Lab::VISA::VI_ATTR_TMO_VALUE, $self->config()->{timeout} * 1e3
152 );
153  
154 }
155  
156 1;
157  
158 #
159 # Read,Write,Query are OK in the version from Lab::Connection
160 #
161  
162  
163 1;
164  
165 __END__
166  
167 =pod
168  
169 =encoding utf-8
170  
171 =head1 NAME
172  
173 Lab::Connection::VISA_RS232 - RS232-type connection which uses NI VISA (L<Lab::VISA>) as backend
174  
175 =head1 VERSION
176  
177 version 3.881
178  
179 =head1 SYNOPSIS
180  
181 This RS232 Connection class for Lab::Bus::VISA implements a RS232-Standard
182 connection on top of VISA (translates RS232 settings to VISA attributes, mostly).
183  
184 This class is not called directly. To make a RS232 suppporting instrument use
185 Lab::Connection::VISA_RS232, set the connection_type parameter accordingly:
186  
187 $instrument = new BlaDeviceType(
188 connection_type => 'VISA_RS232',
189 port => 'ASRL1',
190 )
191  
192 =head1 DESCRIPTION
193  
194 C<Lab::Connection::VISA_RS232> provides a RS232-type connection with L<Lab::Bus::VISA> using
195 NI VISA (L<Lab::VISA>) as backend.
196  
197 It inherits from L<Lab::Connection::RS232> and subsequently from L<Lab::Connection>.
198  
199 The main feature is to set upon initialization all the RS232 libe parameters
200 baud_rate
201 ...
202  
203 =head1 CONSTRUCTOR
204  
205 =head2 new
206  
207 my $connection = new Lab::Connection::VISA_RS232(
208 port => 'ASRL1',
209 baud_rate => 9600,
210 )
211  
212 =head1 METHODS
213  
214 This just falls back on the methods inherited from L<Lab::Connection>.
215  
216 =head2 config
217  
218 Provides unified access to the fields in initial @_ to all the child classes.
219 E.g.
220  
221 $GPIB_Address=$instrument->Config(gpib_address);
222  
223 Without arguments, returns a reference to the complete $self->Config aka @_ of the constructor.
224  
225 $Config = $connection->Config();
226 $GPIB_Address = $connection->Config()->{'gpib_address'};
227  
228 =head1 CAVEATS/BUGS
229  
230 Probably few. Mostly because there's not a lot to be done here. Please report.
231  
232 =head1 SEE ALSO
233  
234 =over 4
235  
236 =item * L<Lab::Connection>
237  
238 =item * L<Lab::Connection::RS232>
239  
240 =item * L<Lab::Connection::VISA>
241  
242 =back
243  
244 =head1 COPYRIGHT AND LICENSE
245  
246 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
247  
248 Copyright 2012 Andreas K. Huettel, Stefan Geissler
249 2016 Simon Reinhardt
250 2017 Andreas K. Huettel
251 2020 Andreas K. Huettel
252  
253  
254 This is free software; you can redistribute it and/or modify it under
255 the same terms as the Perl 5 programming language system itself.
256  
257 =cut