| 1 | package Lab::Connection::MODBUS_RS232; | ||
| 2 | #ABSTRACT: RS232/RS485 MODBUS RTU connection | ||
| 3 | $Lab::Connection::MODBUS_RS232::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Scalar::Util qw(weaken); | ||
| 8 | use Time::HiRes qw (usleep sleep); | ||
| 9 | use Lab::Exception; | ||
| 10 | |||
| 11 | our @ISA = ("Lab::Connection"); | ||
| 12 | |||
| 13 | our %fields = ( | ||
| 14 | bus_class => 'Lab::Bus::MODBUS_RS232', | ||
| 15 | wait_status => 0, # sec; | ||
| 16 | wait_query => 10e-6, # sec; | ||
| 17 | read_length => 1000, # bytes | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $proto = shift; | ||
| 22 | my $class = ref($proto) || $proto; | ||
| 23 | my $self = $class->SUPER::new(@_) | ||
| 24 | ; # getting fields and _permitted from parent class | ||
| 25 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 26 | |||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | # this does not really make sense for MODBUS, as there are no "commands" and "responses" | ||
| 31 | # disable for the time being | ||
| 32 | # maybe makes sense: write value to one address, wait, read from another address. | ||
| 33 | sub Query { | ||
| 34 | my $self = shift; | ||
| 35 | my $options = undef; | ||
| 36 | if ( ref $_[0] eq 'HASH' ) { $options = shift } | ||
| 37 | else { $options = {@_} } | ||
| 38 | |||
| 39 | warn | ||
| 40 | "Query is not implemented (and makes no sense) for MODBUS connections. Use Read. Ignoring.\n"; | ||
| 41 | return undef; | ||
| 42 | } | ||
| 43 | |||
| 44 | # return undef unless $self->slave_address($self->config()->{'slave_address'}); | ||
| 45 | # # check the configuration hash for a valid bus object or bus type, and set the bus | ||
| 46 | # if( defined($self->config()->{'Bus'}) ) { | ||
| 47 | # if($self->_checkbus($self->config()->{'Bus'})) { | ||
| 48 | # $self->Bus($self->config()->{'Bus'}); | ||
| 49 | # } | ||
| 50 | # else { | ||
| 51 | # warn('Given Bus not supported'); | ||
| 52 | # return undef; | ||
| 53 | # } | ||
| 54 | # } | ||
| 55 | # else { | ||
| 56 | # if($self->_checkbus($self->config()->{'ConnType'})) { | ||
| 57 | # my $ConnType = $self->config()->{'ConnType'}; | ||
| 58 | # my $Port = $self->config()->{'Port'}; | ||
| 59 | # my $slave_address = $self->config()->{'slave_address'}; | ||
| 60 | # my $Interface = ""; | ||
| 61 | # if($ConnType eq 'MODBUS_RS232') { | ||
| 62 | # $self->config()->{'Interface'} = 'RS232'; | ||
| 63 | # $self->Bus(new Lab::Bus::MODBUS_RS232( $self->config() )) || croak('Failed to create bus'); | ||
| 64 | # #$self->Bus(eval("new Lab::Bus::$ConnType( $self->config() )")) || croak('Failed to create bus'); | ||
| 65 | # } | ||
| 66 | # else { | ||
| 67 | # warn('Only RS232 bus type supported for now!\n'); | ||
| 68 | # return undef; | ||
| 69 | # } | ||
| 70 | # } | ||
| 71 | # else { | ||
| 72 | # warn('Given Bus Type not supported'); | ||
| 73 | # return undef; | ||
| 74 | # } | ||
| 75 | # } | ||
| 76 | |||
| 77 | # | ||
| 78 | # Nothing to do, Read, Write, Query from Lab::Connection are sufficient. | ||
| 79 | # | ||
| 80 | |||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | __END__ |