La séptima vida

...o el gato así lo espera/teme

Device::Modbus server in a Raspberry Pi

And finally, this week I was able to install Device::Modbus into a Raspberry Pi. I ran a Modbus TCP server which responded to requests sent from my laptop. Things are becoming interesting!

The simple server is the following:

#! /usr/bin/env perl

use Device::Modbus::TCP::Server;
use strict;
use warnings;
use v5.10;

    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) = @_;
        $server->log(4,"Executed server routine for address 2, 1 register");
        return 6;

my $server = Device::Modbus::TCP::Server->new(
    log_level => 4,

my $unit = My::Unit->new(id => 3);


And the client, which is even simpler, is here:

#! /usr/bin/perl

use Device::Modbus::TCP::Client;
use Data::Dumper;
use strict;
use warnings;
use v5.10;

my $client = Device::Modbus::TCP::Client->new(
    host => '',

my $req = $client->read_holding_registers(
    unit     => 3,
    address  => 2,
    quantity => 1

say Dumper $req;
$client->send_request($req) || die "Send error: $!";
my $response = $client->receive_response;
say Dumper $response;


While this little demonstration is far from being useful or complicated, it shows that Device::Modbus can work in a Raspberry Pi and that it is capable of handling client connections.

Hopefully a more interesting example will follow. In the mean while, I started to write the documentation for the three distributions: Device::Modbus, Device::Modbus::TCP and Device::Modbus::RTU.

The code above is in the examples of Device::Modbus::TCP, available in GitHub.