Device::Modbus::RTU::Server - Perl server for Modbus RTU communications
#! /usr/bin/env perl use Device::Modbus::RTU::Server; use strict; use warnings; use v5.10; # This simple server can be tested with an Arduino with the # program 'arduino_client.ino' in the examples directory { package My::Unit; our @ISA = ('Device::Modbus::Unit'); sub init_unit { my $unit = shift; # Zone addr qty method # ------------------- ---- --- --------- $unit->get('holding_registers', 2, 1, 'get_addr_2'); } sub get_addr_2 { my ($unit, $server, $req, $addr, $qty) = @_; say "Executed server routine for address 2, 1 register"; return 6; } } my $server = Device::Modbus::RTU::Server->new( port => '/dev/ttyACM0', baudrate => 9600, parity => 'none', log_level => 4 ); my $unit = My::Unit->new(id => 3); $server->add_server_unit($unit); $server->start;
This module is part of Device::Modbus::RTU, a distribution which implements the Modbus RTU protocol on top of Device::Modbus.
Device::Modbus::RTU::Server inherits from Device::Modbus::Server, and adds the capability of communicating via the serial port. As such, Device::Modbus::RTU::Server implements the constructor and a logging method. Please see Device::Modbus::Server for most of the server-related documentation.
This method opens the serial port to communicate using the Modbus RTU protocol. It takes the following arguments:
The serial port to open.
A number between 0 and 4, where 0 will log only emergencies and 4 will produce sufficient output for debugging. Default is 2.
A valid baud rate. Defaults to 9600 bps.
An integer from 5 to 8. Defaults to 8.
Either 'even', 'odd' or 'none'. Defaults to 'none'.
1 or 2. Defaults to 1.
Defaults to 10 (seconds).
This method closes the serial port, but it is called automatically for you when the server is shut down with a SIG_INT.
This module will log messages to STDERR. The level of logging is set with the log_level argument to the constructor method. The logging level may be changed also with the method log_level.
log_level
log takes two arguments: The level of a message and the message itself:
log
$server->log(3, 'This is the text to log');
Most of the functionality is described in Device::Modbus::Server.
These are other implementations of Modbus in Perl which may be well suited for your application: Protocol::Modbus, MBclient, mbserverd.
You can find the repository of this distribution in GitHub.
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::RTU, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Device::Modbus::RTU
CPAN shell
perl -MCPAN -e shell install Device::Modbus::RTU
For more information on module installation, please visit the detailed CPAN module installation guide.