Connection/VISA_RS232.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 116 CC 12 SCAR 42.6
CC
12
Cov
0%
_configurebus
132.0
_setbus
6.0
new
2.0
CRAP 70.5
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.899';
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__