The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME Device::Modbus::Server::TCP -- Modbus TCP server class

SYNOPSIS

   use My::Unit;
   use Device::Modbus::Server::TCP;
   use strict;
   use warnings;

   my $unit = My::Unit->new( id => 3 );
      
   my $server = Device::Modbus::Server::TCP->new(
       server_type       => ['PreFork'],
       log_level         =>  2,
       min_servers       => 10,
       max_servers       => 30,
       min_spare_servers => 5,
       max_spare_servers => 10,
       max_requests      => 1000,
   );

   $server->add_server_unit($unit);
   $server->start;

DESCRIPTION

One of the goals for Device::Modbus is to have the ability to write Modbus servers that execute arbitrary code. This class defines the Modbus TCP version of such servers. Please see the documentation in Device::Modbus::Server for a thorough description of the interface; refer to this document only for the details inherent to Modbus TCP.

USAGE

Device::Modbus::Server inherits from Net::Server::MultiType, so be sure to read carefully its documentation. You can pass Net::Server::MultiType parameters via the new constructor, via the start method, or using a configuration file as defined by the conf_file argument described in Net::Derver. Note that start is just a wrapper for Net::Server's run.

The example in the synopsis of this document is using the PreFork personality of Net::Server. If you do not specify a personality, it will default to Single.

CONFIGURATION

All the configuration possibilities found in Net::Server::MultiType are available. The default parameters for Device::Modbus::Server::TCP are:

log_level   => 2,
log_file    => undef,
port        => 502,
host        => '*',
ipv         => 4,
proto       => 'tcp',

Net::Server::MultiType METHODS USED

The methods defined by Net::Server::MultiType and used by Device::Modbus::Server::TCP are:

default_values

This is used only to pass the default parameters of the server. Note that this is the lowest priority form of configuration; these values can be overwritten by passing arguments to new, by passing command-line arguments, by passing arguments to start, or by using a configuration file. You can, of course, write your own default_values method in a sub-class.

process_request

This is where the generic Modbus server method is called. It listens for requests, processes them, and returns the responses.

run

The run method is simply wrapped by this module's start to keep a certain symmetry with Device::Modbus::RTU::Server.

post_accept_hook

This method is used to make the underlying socket available with the socket method of the server.

NOTES

While Modbus RTU processes are single-process, this server is not. It is important to notice that, because of its forking nature, each process has its own copy of the units you defined. While there are indeed mechanisms for them to communicate (see Net::Server), in general they are completely independent. Global variables are then global by process only and not accross the whole process group. This boils down to the fact that the example server in this distribution, which keeps register values in a per-process global variable, will not work in a real world scenario. It would be necessary to persist registers outside of the server, like in a database.

SEE ALSO

Most of the functionality is described in Device::Modbus::Server.

Other distributions

These are other implementations of Modbus in Perl which may be well suited for your application: Protocol::Modbus, MBclient, mbserverd.

GITHUB REPOSITORY

You can find the repository of this distribution in GitHub.

AUTHOR

Julio Fraire, <julio.fraire@gmail.com>

COPYRIGHT AND LICENSE

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.