This article describes how to communicate with a Brainboxes Ethernet IO Module (or Remote IO module) using the Perl programming language.

Requirements

  • A computer with the Perl Runtime
  • The Brainboxes Perl Module (download from this page)

Sample Code

Create a subdirectory in your perl package library directory (may be something like C:\perl\site\lib) called ‘Brainboxes’, and then save the file ‘AsciiIo.pm’ there.  You can then run the script ‘Brainboxes_AsciiIo_demo.pl’ which demonstrates the usage of that class.

If you are unable to add Brainboxes/AsciiIo.pm to your existing perl installation, for example because you don’t have the necessary permission, you can set up your own library directory anywhere you like.  Create a directory for the library, add a ‘Brainboxes’ subdirectory to put ‘AsciiIo.pm’ in, and add a ‘use lib’ (http://perldoc.perl.org/lib.html) command at the start of the script to add your library to the search path.

The script ‘Brainboxes_AsciiIo_demo.pl’ demonstrates the usage of the Brainboxes::AsciiIo class.  Before you run it, edit the IP address on line 6 to be the address of a Brainboxes Ethernet IO Module on your network.  Also edit the TCP port number if you have changed it from the default of 9500.  The output of the program should look something like this:

>perl Brainboxes_AsciiIo_demo.pl
Sending command '$01M'
  Response was '!01ED-588'
Sending command '~**'
  No response expected
Sending command '@01'
  Response was '>00FF'
Sending command '@02'
  No response received!
Sending command '$016'
  Response was '!00FF00'

 

The code below is available to download as a zip from the top of the page.

use Brainboxes::AsciiIo;

use strict;
use warnings;

my $remote = Brainboxes::AsciiIo->new({ipaddr => '192.168.0.95', port => 9500, timeout => 5});

for my $command ('$01M', '~**', '@01', '@02', '$016') {
    # send the above commands in sequence
    # the '@02' command will probably get no response, unless you have a slave device connected to the gateway port

    print "Sending command '$command'\n"; binmode STDOUT;
    
    if (substr($command, 1, 2) eq '**') {
        # this command is not one which we expect to get a response to
        $remote->command_noresponse($command);
        print "  No response expected\n";
        binmode STDOUT;
    }
    else {
        # this command is one which we SHOULD get a response to
        my $rxdata = $remote->command_response($command);
        
        if (defined $rxdata) {
            print "  Response was '$rxdata'\n";
        }
        else {
            print "  No response received!\n";
        }
        binmode STDOUT;
    }
}