| 1 | package Lab::Bus::RS232; | ||
| 2 | #ABSTRACT: RS232 or Virtual Comm port bus | ||
| 3 | $Lab::Bus::RS232::VERSION = '3.881'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Lab::Bus; | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | use Scalar::Util qw(weaken); | ||
| 13 | |||
| 14 | our @ISA = ("Lab::Bus"); | ||
| 15 | |||
| 16 | # load serial driver | ||
| 17 | use vars qw( $OS_win); | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | $OS_win = ( $^O eq "MSWin32" ) ? 1 : 0; | ||
| 21 | |||
| 22 | if ($OS_win) { | ||
| 23 | eval "use Win32::SerialPort"; | ||
| 24 | die "$@\n" if ($@); | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | eval "use Device::SerialPort"; | ||
| 28 | die "$@\n" if ($@); | ||
| 29 | } | ||
| 30 | } # End BEGIN | ||
| 31 | |||
| 32 | our $RS232_DEBUG = 0; | ||
| 33 | our $WIN32 = ( $^O eq "MSWin32" ) ? 1 : 0; | ||
| 34 | |||
| 35 | our %fields = ( | ||
| 36 | client => undef, | ||
| 37 | type => 'RS232', | ||
| 38 | port => '/dev/ttyS0', | ||
| 39 | baudrate => 38400, | ||
| 40 | parity => 'none', | ||
| 41 | databits => 8, | ||
| 42 | stopbits => 1, | ||
| 43 | handshake => 'none', | ||
| 44 | timeout => 500, | ||
| 45 | read_length => 'all', | ||
| 46 | brutal => 0, | ||
| 47 | wait_query => 10e-6, #sec | ||
| 48 | ); | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my $proto = shift; | ||
| 52 | my $class = ref($proto) || $proto; | ||
| 53 | my $twin = undef; | ||
| 54 | my $self = $class->SUPER::new(@_) | ||
| 55 | ; # getting fields and _permitted from parent class | ||
| 56 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 57 | |||
| 58 | # parameter parsing | ||
| 59 | $self->port( $self->config('port') ) if defined $self->config('port'); | ||
| 60 | warn( "No port supplied to RS232 bus. Assuming default port " | ||
| 61 | . $self->config('port') | ||
| 62 | . "\n" ) | ||
| 63 | if ( !defined $self->config('port') ); | ||
| 64 | $self->baudrate( $self->config('baudrate') ) | ||
| 65 | if defined $self->config('baudrate'); | ||
| 66 | $self->parity( $self->config('parity') ) | ||
| 67 | if defined $self->config('parity'); | ||
| 68 | $self->databits( $self->config('databits') ) | ||
| 69 | if defined $self->config('databits'); | ||
| 70 | $self->stopbits( $self->config('stopbits') ) | ||
| 71 | if defined $self->config('stopbits'); | ||
| 72 | $self->handshake( $self->config('handshake') ) | ||
| 73 | if defined $self->config('handshake'); | ||
| 74 | $self->timeout( $self->config('timeout') ) | ||
| 75 | if defined $self->config('timeout'); | ||
| 76 | |||
| 77 | # search for twin in %Lab::Bus::BusList. If there's none, place $self there and weaken it. | ||
| 78 | if ( $class eq __PACKAGE__ ) | ||
| 79 | { # careful - do only if this is not a parent class constructor | ||
| 80 | if ( $twin = $self->_search_twin() ) { | ||
| 81 | undef $self; | ||
| 82 | warn "Existing Bus object of type " | ||
| 83 | . $self->type() | ||
| 84 | . " for port " | ||
| 85 | . $self->port() | ||
| 86 | . " found. Reusing.\n"; | ||
| 87 | return $twin; # ...and that's it. | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | $Lab::Bus::BusList{ $self->type() }->{ $self->port() } = $self; | ||
| 91 | weaken( $Lab::Bus::BusList{ $self->type() }->{ $self->port() } ); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | # clear new port | ||
| 96 | if ($WIN32) { | ||
| 97 | $self->client( | ||
| 98 | new Win32::SerialPort( $self->config('port') ) | ||
| 99 | or warn "Could not open serial port\n" | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | $self->client( | ||
| 104 | new Device::SerialPort( $self->config('port') ) | ||
| 105 | or warn "Could not open serial port\n" | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | # config port if needed | ||
| 110 | |||
| 111 | if ( defined $self->client ) { | ||
| 112 | $self->client()->purge_all; | ||
| 113 | $self->client()->read_const_time( $self->timeout() ); | ||
| 114 | $self->client()->handshake( $self->config('handshake') ) | ||
| 115 | if ( defined $self->config('handshake') ); | ||
| 116 | $self->client()->baudrate( $self->config('baudrate') ) | ||
| 117 | if ( defined $self->config('baudrate') ); | ||
| 118 | $self->client()->parity( $self->config('parity') ) | ||
| 119 | if ( defined $self->config('parity') ); | ||
| 120 | $self->client()->databits( $self->config('databits') ) | ||
| 121 | if ( defined $self->config('databits') ); | ||
| 122 | $self->client()->stopbits( $self->config('stopbits') ) | ||
| 123 | if ( defined $self->config('stopbits') ); | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | Lab::Exception::Error->throw( | ||
| 127 | error => "Error initializing the serial interface\n" ); | ||
| 128 | } | ||
| 129 | |||
| 130 | return $self; | ||
| 131 | } | ||
| 132 | |||
| 133 | # | ||
| 134 | # This will be short. | ||
| 135 | # | ||
| 136 | sub connection_new { | ||
| 137 | my $self = shift; | ||
| 138 | my $args = undef; | ||
| 139 | if ( ref $_[0] eq 'HASH' ) { | ||
| 140 | $args = shift; | ||
| 141 | } # try to be flexible about options as hash/hashref | ||
| 142 | else { $args = {@_} } | ||
| 143 | |||
| 144 | return { handle_type => 'RS232', valid => 1 }; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub connection_read | ||
| 148 | { # @_ = ( $connection_handle, $args = { read_length, brutal } | ||
| 149 | my $self = shift; | ||
| 150 | my $connection_handle = shift; | ||
| 151 | my $args = undef; | ||
| 152 | if ( ref $_[0] eq 'HASH' ) { | ||
| 153 | $args = shift; | ||
| 154 | } # try to be flexible about options as hash/hashref | ||
| 155 | else { $args = {@_} } | ||
| 156 | |||
| 157 | return $self->_direct_read($args); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _direct_read | ||
| 161 | { # _direct_read() this is for inheriting buses like MODBUS_RS232 for direct access # @_ = ( $connection_handle, $args = { read_length, brutal } | ||
| 162 | use bytes; | ||
| 163 | my $self = shift; | ||
| 164 | my $args = undef; | ||
| 165 | if ( ref $_[0] eq 'HASH' ) { | ||
| 166 | $args = shift; | ||
| 167 | } # try to be flexible about options as hash/hashref | ||
| 168 | else { $args = {@_} } | ||
| 169 | |||
| 170 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 171 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 172 | |||
| 173 | my $result = ""; | ||
| 174 | my $buf = ""; | ||
| 175 | my $raw = ""; | ||
| 176 | |||
| 177 | if ( $read_length eq 'all' ) { | ||
| 178 | do { | ||
| 179 | $buf = $self->client()->read(4096); | ||
| 180 | $result .= $buf; | ||
| 181 | } while ( length($buf) == 4096 ); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $result = $self->client()->read($read_length) | ||
| 185 | ; # note: taken from older code - is 4096 some strong limit? If yes, this needs more work. | ||
| 186 | } | ||
| 187 | |||
| 188 | return $result; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub connection_write | ||
| 192 | { # @_ = ( $connection_handle, $args = { command, brutal } | ||
| 193 | my $self = shift; | ||
| 194 | my $connection_handle = shift; | ||
| 195 | my $args = undef; | ||
| 196 | if ( ref $_[0] eq 'HASH' ) { | ||
| 197 | $args = shift; | ||
| 198 | } # try to be flexible about options as hash/hashref | ||
| 199 | else { $args = {@_} } | ||
| 200 | |||
| 201 | return $self->_direct_write($args); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub _direct_write | ||
| 205 | { # _direct_write( command => $cmd ) this is for inheriting buses like MODBUS_RS232 for direct access | ||
| 206 | use bytes; | ||
| 207 | my $self = shift; | ||
| 208 | my $args = undef; | ||
| 209 | if ( ref $_[0] eq 'HASH' ) { $args = shift } | ||
| 210 | else { $args = {@_} } | ||
| 211 | |||
| 212 | my $command = $args->{'command'} || undef; | ||
| 213 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 214 | |||
| 215 | my $status = undef; | ||
| 216 | |||
| 217 | if ( !defined $command ) { | ||
| 218 | Lab::Exception::CorruptParameter->throw( | ||
| 219 | error => "No command given to " | ||
| 220 | . __PACKAGE__ | ||
| 221 | . "::connection_write().\n", ); | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | $status = $self->client()->write($command); | ||
| 225 | } | ||
| 226 | |||
| 227 | if ( !$status && !$brutal ) { | ||
| 228 | Lab::Exception::RS232Error->throw( | ||
| 229 | error => "Error in " | ||
| 230 | . __PACKAGE__ | ||
| 231 | . "::connection_write() while executing $command: write failed.\n", | ||
| 232 | status => $status, | ||
| 233 | ); | ||
| 234 | } | ||
| 235 | elsif ($brutal) { | ||
| 236 | warn "(brutal=>Ignored) error in " | ||
| 237 | . __PACKAGE__ | ||
| 238 | . "::connection_write() while executing $command: write failed.\n"; | ||
| 239 | } | ||
| 240 | |||
| 241 | return 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub connection_query | ||
| 245 | { # @_ = ( $connection_handle, $args = { command, read_length, wait_query, brutal } | ||
| 246 | my $self = shift; | ||
| 247 | my $connection_handle = shift; | ||
| 248 | my $args = undef; | ||
| 249 | if ( ref $_[0] eq 'HASH' ) { | ||
| 250 | $args = shift; | ||
| 251 | } # try to be flexible about options as hash/hashref | ||
| 252 | else { $args = {@_} } | ||
| 253 | |||
| 254 | my $command = $args->{'command'} || undef; | ||
| 255 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 256 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 257 | my $wait_status = $args->{'wait_status'} || $self->wait_status(); | ||
| 258 | my $wait_query = $args->{'wait_query'} || $self->wait_query(); | ||
| 259 | my $result = undef; | ||
| 260 | |||
| 261 | $self->connection_write( $connection_handle, $args ); | ||
| 262 | |||
| 263 | usleep($wait_query) | ||
| 264 | ; #<---ensures that asked data presented from the device | ||
| 265 | |||
| 266 | $result = $self->connection_read( $connection_handle, $args ); | ||
| 267 | return $result; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub connection_clear { | ||
| 271 | my $self = shift; | ||
| 272 | my $connection_handle = shift; | ||
| 273 | |||
| 274 | $self->connection_read( $connection_handle, read_length => 'all' ) | ||
| 275 | ; # clear buffer | ||
| 276 | |||
| 277 | return 1; | ||
| 278 | } | ||
| 279 | |||
| 280 | # | ||
| 281 | # search and return an instance of the same type in %Lab::Bus::BusList | ||
| 282 | # | ||
| 283 | sub _search_twin { | ||
| 284 | my $self = shift; | ||
| 285 | |||
| 286 | if ( !$self->ignore_twins() ) { | ||
| 287 | for my $conn ( values %{ $Lab::Bus::BusList{ $self->type() } } ) { | ||
| 288 | return $conn if $conn->port() == $self->port(); | ||
| 289 | } | ||
| 290 | } | ||
| 291 | return undef; | ||
| 292 | } | ||
| 293 | |||
| 294 | # BrutalRead and Clear not implemented | ||
| 295 | |||
| 296 | 1; | ||
| 297 | |||
| 298 | __END__ | ||
| 299 | |||
| 300 | =pod | ||
| 301 | |||
| 302 | =encoding utf-8 | ||
| 303 | |||
| 304 | =head1 NAME | ||
| 305 | |||
| 306 | Lab::Bus::RS232 - RS232 or Virtual Comm port bus | ||
| 307 | |||
| 308 | =head1 VERSION | ||
| 309 | |||
| 310 | version 3.881 | ||
| 311 | |||
| 312 | =head1 SYNOPSIS | ||
| 313 | |||
| 314 | my $bus = Lab::Bus::RS232({ | ||
| 315 | port => '/dev/ttyACM0' | ||
| 316 | }); | ||
| 317 | |||
| 318 | Return blessed $self, with @_ accessible through $self->config(). | ||
| 319 | |||
| 320 | C<port>: Device name to use (e.g. C<COM1> under Windows or C</dev/ttyUSB1> under Linux) | ||
| 321 | |||
| 322 | TODO: check this!!! | ||
| 323 | |||
| 324 | =head1 DESCRIPTION | ||
| 325 | |||
| 326 | This is a bus for Lab::Measurement to communicate via RS232 or Virtual Comm port e.g. for | ||
| 327 | FTDI devices. | ||
| 328 | |||
| 329 | =head1 CONSTRUCTOR | ||
| 330 | |||
| 331 | =head2 new | ||
| 332 | |||
| 333 | All parameters are used as by C<Device::SerialPort>. port is needed in every case. | ||
| 334 | An additional parameter C<reuse> is avaliable if two instruments use the same port. This | ||
| 335 | is mainly implemented for USBprologix gateway. C<reuse> can be a SerialPort object or | ||
| 336 | a C<Lab::Instrument...> package. Default value for timeout is 500ms and can be set by the | ||
| 337 | parameter "timeout". Other options: handshake, baudrate, databits, stopbits and parity | ||
| 338 | |||
| 339 | =head1 METHODS | ||
| 340 | |||
| 341 | Used by C<Lab::Instrument>. Not for direct use!!! | ||
| 342 | |||
| 343 | =head2 Read | ||
| 344 | |||
| 345 | Reads data. | ||
| 346 | |||
| 347 | =head2 Write | ||
| 348 | |||
| 349 | Sent data to instrument | ||
| 350 | |||
| 351 | =head2 Handle | ||
| 352 | |||
| 353 | Give instrument object handle | ||
| 354 | |||
| 355 | =head1 CAVEATS/BUGS | ||
| 356 | |||
| 357 | Probably many. So far BrutalRead and Clear are not implemented because not needed for this interface. | ||
| 358 | |||
| 359 | =head1 SEE ALSO | ||
| 360 | |||
| 361 | =over 4 | ||
| 362 | |||
| 363 | =item L<Lab::Instrument> | ||
| 364 | |||
| 365 | =item L<Win32::SerialPort> | ||
| 366 | |||
| 367 | =item L<Device::SerialPort> | ||
| 368 | |||
| 369 | =back | ||
| 370 | |||
| 371 | =head1 COPYRIGHT AND LICENSE | ||
| 372 | |||
| 373 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 374 | |||
| 375 | Copyright 2010 Andreas K. Huettel | ||
| 376 | 2011-2012 Andreas K. Huettel, Florian Olbrich | ||
| 377 | 2016 Simon Reinhardt | ||
| 378 | 2017 Andreas K. Huettel | ||
| 379 | 2020 Andreas K. Huettel | ||
| 380 | |||
| 381 | |||
| 382 | This is free software; you can redistribute it and/or modify it under | ||
| 383 | the same terms as the Perl 5 programming language system itself. | ||
| 384 | |||
| 385 | =cut |