Device::Modbus::Client - Modbus clients with Device::Modbus
my $r = $client->read_coils( address => 23, quantity => 6 ); my $r = $client->read_discrete_inputs( address => 55, quantity => 7 ); my $r = $client->read_holding_registers( address => 1, quantity => 16 ); my $r = $client->read_input_registers( address => 6, quantity => 3 ); my $r = $client->write_single_coil( address => 33, value => 1 ); my $r = $client->write_single_register( address => 33, value => 3457 ); my $r = $client->write_multiple_coils( address => 64, values => [1, 1, 0, 1] ); my $r = $client->write_multiple_registers( address => 64, values => [345, 65, 67, 243] ); my $r = $client->read_write_registers( read_address => 14, read_quantity => 3, write_address => 20, values => [ 45, 87, 1, 298, 0, 0] ); $client->send_request($r); my $adu = $client->receive_response; if ($adu->success) { $values = $adu->values; }
This class implements Modbus clients. Clients must connect to servers, build requests, send them to the server, and receive their responses.
The following methods create request objects. They all receive the same arguments: an address and the quantity of bits or register to read. See the synopsis for examples.
The arguments for these methods are an address and a value or values. Again, see the synopsis:
This request receives two addresses (read and write), the quantity of records to read, and an array reference with the values to write:
Once you have one or more request objects, it is time to send them to the server:
This method receives a request object as its only argument.
Finally, this method will return the response received from the server encapsulated into an ADU (Application Data Unit).
Responses from the server are returned to the client wrapped into an ADU object. The ADU has several methods to inspect the response and retrieve any requested information:
my $resp = $adu->message;
Returns the actual response object.
my $resp = $adu->function;
Returns the function name of the request and response objects.
my $code = $adu->code;
Returns the function code of the request and response objects.
my $values = $adu->values;
Returns an array reference with the values returned by the server. Valid only for reading requests.
$val = $adu->values if $adu->success;
Returns a true value if the ADU contains a message and its function code is less than 0x80 (Modbus exception objects have a function code larger than 0x80).
Client methods to actually connect and close the connection to the server are available in Device::Modbus::RTU::Client and Device::Modbus::TCP::Client.
Julio Fraire, <julio.fraire@gmail.com>
Copyright (C) 2015 by Julio Fraire This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
To install Device::Modbus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Device::Modbus
CPAN shell
perl -MCPAN -e shell install Device::Modbus
For more information on module installation, please visit the detailed CPAN module installation guide.