Bus/IsoBus.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 25 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 181 SCAR 52.0
CC
13
Cov
0%
new
42.0
connection_write
20.0
connection_new
6.0
CRAP 182.0
1 package Lab::Bus::IsoBus;
2 #ABSTRACT: Oxford Instruments ISOBUS bus
3 $Lab::Bus::IsoBus::VERSION = '3.881';
4 use v5.20;
5  
6 use strict;
7 use Lab::Connection;
8 use Scalar::Util qw(weaken);
9 use Time::HiRes qw (usleep sleep);
10 use Lab::Bus;
11 use Data::Dumper;
12 use Carp;
13  
14 our @ISA = ("Lab::Bus");
15  
16 our %fields = (
17 type => 'IsoBus',
18 base_connection => undef,
19 IsoEnableTermChar => 1,
20 IsoTermChar => "\r",
21 brutal => 0, # brutal as default?
22 wait_status => 10e-6, # sec;
23 wait_query => 10e-6, # sec;
24 query_length => 300, # bytes
25 query_long_length => 10240, #bytes
26 read_length => 1000, # bytes
27 );
28  
29 sub new {
30 my $proto = shift;
31 my $class = ref($proto) || $proto;
32 my $twin = undef;
33 my $self = $class->SUPER::new(@_)
34 ; # getting fields and _permitted from parent class
35 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
36  
37 # search for twin in %Lab::Bus::BusList. If there's none, place $self there and weaken it.
38 if ( $class eq __PACKAGE__ )
39 { # careful - do only if this is not a parent class constructor
40 if ( $twin = $self->_search_twin() ) {
41 undef $self;
42 return $twin; # ...and that's it.
43 }
44 else {
45 # TODO implement twin detection
46 $Lab::Bus::BusList{ $self->type() }->{'default'} = $self;
47 weaken( $Lab::Bus::BusList{ $self->type() }->{'default'} );
48 }
49 }
50  
51 # set the connection $self->base_connection to the parameters required by IsoBus
52 $self->base_connection( $self->config('base_connection') )
53 if defined $self->config('base_connection');
54 $self->IsoEnableTermChar( $self->config('IsoEnableTermChar') )
55 if defined $self->config('IsoEnableTermChar');
56 $self->IsoTermChar( $self->config('IsoTermChar') )
57 if defined $self->config('IsoTermChar');
58  
59 # clear the connection if possible
60  
61 # we need to set the following RS232 options: 9600baud, 8 data bits, 1 stop bit, no parity, no flow control
62 # what is the read terminator? we assume CR=13 here, but this is not set in stone
63 # write terminator should I think always be CR=13=0x0d
64  
65 return $self;
66 }
67  
68 sub connection_new { # @_ = ({ isobus_address => $isobus_address })
69 my $self = shift;
70 my $args = undef;
71 my $status = undef;
72 my $connection_handle = undef;
73 if ( ref $_[0] eq 'HASH' ) {
74 $args = shift;
75 } # try to be flexible about options as hash/hashref
76 else { $args = {@_} }
77  
78 my $isobus_address = $args->{'isobus_address'};
79  
80 # check that this is a number and in the valid range
81  
82 # we dont actually have to open anything here
83  
84 # we abuse the isobus_address as connection handle
85 return $isobus_address;
86 }
87  
88 sub connection_read
89 { # @_ = ( $connection_handle, $args = { read_length, brutal }
90 my $self = shift;
91 my $connection_handle = shift;
92 my $args = undef;
93 if ( ref $_[0] eq 'HASH' ) {
94 $args = shift;
95 } # try to be flexible about options as hash/hashref
96 else { $args = {@_} }
97  
98 my $brutal = $args->{'brutal'} || $self->brutal();
99 my $read_length = $args->{'read_length'} || $self->read_length();
100 my $result = undef;
101  
102 $result = $self->config('base_connection')->Read(
103 {
104 brutal => $brutal,
105 read_length => $read_length,
106 }
107 );
108  
109 return $result;
110 }
111  
112 sub connection_write
113 { # @_ = ( $connection_handle, $args = { command, wait_status }
114 my $self = shift;
115 my $connection_handle = shift;
116 my $args = undef;
117 if ( ref $_[0] eq 'HASH' ) {
118 $args = shift;
119 } # try to be flexible about options as hash/hashref
120 else { $args = {@_} }
121  
122 my $command = $args->{'command'} || undef;
123  
124 my $write_cnt = 0;
125  
126 if ( !defined $command ) {
127 Lab::Exception::CorruptParameter->throw(
128 error => "No command given to "
129 . __PACKAGE__
130 . "::connection_write().\n", );
131 }
132 else {
133 if ( $self->config("IsoEnableTermChar") ) {
134 $write_cnt = $self->base_connection->Write(
135 {
136 # build the format for an IsoBus command
137 command => sprintf(
138 "@%d%s%s",
139 $connection_handle, $command, $self->IsoTermChar()
140 ),
141 }
142 );
143  
144 }
145 else {
146 $write_cnt = $self->base_connection->Write(
147 {
148 # build the format for an IsoBus command
149 command =>
150 sprintf( "@%d%s", $connection_handle, $command )
151 }
152 );
153 }
154 return $write_cnt;
155 }
156 }
157  
158 sub connection_query
159 { # @_ = ( $connection_handle, $args = { command, read_length, wait_status, wait_query, brutal }
160 my $self = shift;
161 my $connection_handle = shift;
162 my $args = undef;
163 if ( ref $_[0] eq 'HASH' ) {
164 $args = shift;
165 } # try to be flexible about options as hash/hashref
166 else { $args = {@_} }
167  
168 my $command = $args->{'command'} || undef;
169 my $brutal = $args->{'brutal'} || $self->brutal();
170 my $read_length = $args->{'read_length'} || $self->read_length();
171 my $wait_status = $args->{'wait_status'} || $self->wait_status();
172 my $wait_query = $args->{'wait_query'} || $self->wait_query();
173  
174 my $result = undef;
175 my $status = undef;
176 my $write_cnt = 0;
177 my $read_cnt = undef;
178  
179 $write_cnt = $self->connection_write($args);
180  
181 usleep($wait_query)
182 ; #<---ensures that asked data presented from the device
183  
184 $result = $self->connection_read($args);
185 return $result;
186 }
187  
188 sub connection_clear {
189 my $self = shift;
190  
191 $self->config('base_connection')->Clear();
192  
193 }
194  
195 #
196 # search and return an instance of the same type in %Lab::Bus::BusList
197 #
198 sub _search_twin {
199 my $self = shift;
200  
201 # Only one VISA bus for the moment, stored as "default"
202 if ( !$self->ignore_twins() ) {
203  
204 # if(defined $Lab::Bus::BusList{$self->type()}->{'default'}) {
205 # return $Lab::Bus::BusList{$self->type()}->{'default'};
206 # }
207 }
208  
209 return undef;
210 }
211  
212 1;
213  
214  
215 1;
216  
217 __END__
218  
219 =pod
220  
221 =encoding utf-8
222  
223 =head1 NAME
224  
225 Lab::Bus::IsoBus - Oxford Instruments ISOBUS bus
226  
227 =head1 VERSION
228  
229 version 3.881
230  
231 =head1 SYNOPSIS
232  
233 This is the IsoBus bus class. Typically you create it implicit through instrument creation:
234  
235 my $instrument = new Lab::Instrument::IPS({
236 BusType => 'IsoBus',
237 base_connection => new Lab::Bus::VISA_GPIB({ gpib_board => 0, gpib_address => 24}),
238 isobus_addres => 2,
239 }
240  
241 =head1 METHODS
242  
243 =head2 connection_new
244  
245 $isobus->connection_new({ resource_name => $isobus_address });
246  
247 Creates a new instrument handle for this bus.
248  
249 The handle is usually stored in an instrument object and given to connection_read, connection_write etc.
250 to identify and handle the calling instrument:
251  
252 $InstrumentHandle = $isobus->connection_new({ resource_name => $isobus_address });
253 $result = $isobus->connection_read($self->InstrumentHandle(), { options });
254  
255 See C<Lab::Instrument::Read()>.
256  
257 =head2 connection_write
258  
259 $isobus->connection_write( $InstrumentHandle, { command => $command, wait_status => $wait_status } );
260  
261 Puts in front of the $command-string the isobus_adress, e.g. "@1$command". Passes the modified argument hash to the base_connection.
262 For further information refer to the specific connection class of $base_connection.
263  
264 =head2 connection_read
265  
266 $isobus->connection_read( $InstrumentHandle, { command => $command, read_length => $read_length, timeout => $seconds, brutal => 0/1 } );
267  
268 Puts in front of the $command-string the isobus_adress, e.g. "@1$command". Passes the modified argument hash to the base_connection.
269 For further information refer to the specific connection class of $base_connection.
270  
271 =head2 connection_clear
272  
273 $isobus->connection_clear( $InstrumentHandle );
274  
275 Clears the specified connection $InstrumentHandle.
276  
277 =head2 connection_query
278  
279 $isobus->connection_query( $InstrumentHandle, { command => $command, read_length => $read_length, wait_status => $wait_status, wait_query => $wait_query, brutal => 0/1 } );
280  
281 Puts in front of the $command-string the isobus_adress, e.g. "@1$command". Passes the modified argument hash to the base_connection.
282 For further information refer to the specific connection class of $base_connection.
283  
284 =head1 SEE ALSO
285  
286 =over 4
287  
288 =item * L<Lab::Bus>
289  
290 =item * L<Lab::Connection>
291  
292 =item * and many more...
293  
294 =back
295  
296 =head1 COPYRIGHT AND LICENSE
297  
298 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
299  
300 Copyright 2011 Andreas K. Huettel, Florian Olbrich
301 2012 Alois Dirnaichner, Andreas K. Huettel, David Kalok, Florian Olbrich, Stefan Geissler
302 2013 Stefan Geissler
303 2016 Simon Reinhardt
304 2017 Andreas K. Huettel
305 2020 Andreas K. Huettel
306 2021 Fabian Weinelt
307  
308  
309 This is free software; you can redistribute it and/or modify it under
310 the same terms as the Perl 5 programming language system itself.
311  
312 =cut