| 1 | package Lab::Bus::RS232; | ||
| 2 | #ABSTRACT: RS232 or Virtual Comm port bus | ||
| 3 | $Lab::Bus::RS232::VERSION = '3.899'; | ||
| 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__ |