Modbus Server
Purpose
Purpose of this plugin is to instantiate a Modbus Server on the Ardexa edge device.
Description
This plugin will read a configuration file, read Modbus registers from a local network, and then publish Modbus data locally. Data can be sourced either via RTU (local serial device) or from local IP addresses. The source Modbus registers can then be remapped to other registers. No data will be sent to the Ardexa cloud. This plugin is just to assist some plant owners that require Modbus data to be shared locally, via a Modbus Server.
Configuration File
The configuration file is required to manage various aspects of the data collection and conversion. The file must be defined as shown below. There are a few rules to note:
The IP/Serial device address MUST be at the end of ALL the mbpoll commands
Mbpoll commands MUST start with
_cmd,
, followed by the mbpoll command which sources the dataAll the other lines in the configuration file define registers and how they are to be treated.
All lines that start with an
#
character are treated as comments and will be ignored.Each of the columns listed below MUST not be empty.
For
Type
, only the valid strings can be used:INT16, INT32, FLOAT
.If a
INT32
orFLOAT
type is used, be sure to leave 2 sequential output registers free.Scale
can be any floating point number. It can only be used on FLOAT, but NOT INT16 or INT32 - which must be set to1.0
._cmd
is the commands to run. Onlymodpoll
or thembpoll
commands can be used to read Modbus registers.mbpoll
is preferred. The registers being collected MUST MATCH the registers listed in the configuration items. If not, you will have empty fields when running adiscover
. The IP address or serial device MUST appear as the last entry in the command.Each
Output Register
must be unique. If there are any duplicates, then the script will report an error and not run.
Usage
This plugin uses a configuration file. The file must be named as modbus_server.txt
in the directory: /opt/ardexa/config/modbus-server
, and configured as discussed above. Once a single scenario has been defined, a service will be activated that waits for Modbus requests. The task
element of the scenario will not do anything. The scenario must be activated as a service
. Due to a technical limitation, in order to save some config data when initially setting up the server, such as a custom port, once the service is created you may have to turn on the Test Mode
then SAVE, then turn it off and SAVE again. DO NOT create multiple scenarios for this service, it is not designed for this and will lead to undefined behaviour.
Also, you need to open a firewall port manually once the scenario has been activated. This is a design feature, to warrant that the user is aware of the risks of activating a service (server) on the local device. To open a firewall port on the Ardexa device run the following command: sudo ufw allow 502
, where 502
should be the port number required. To close all ports, run the command: sudo ufw default deny incoming
. The Modbus Server port is defined when setting up the server.
If a Modbus request is received, the data defined in the configuration file will be queried and served via the service. The plugin will make a maximum of 3 attempts to read the data, and will cache the results for a maximum of 10 seconds. This cache is to prevent the source endpoints from being overloaded with modbus queries. Queries may take a little longer, so a timeout of a few seconds may be required. If using the mbpoll
client, this is achieved using the -o
argument, such as the query: mbpoll -m tcp -a 1 -r 11 -c 8 -t 4:float -1 -0 -o 8 -p 502 127.0.0.1
- which specifies a timeout of 8 seconds. DO NOT connect to this server with multiple clients, it can only handle one connection at a time. If one client has been connected for more than 30 seconds it will be automatically disconnected, this is to prevent dead connections blocking the server.
Clients querying the Modbus server must use address 1
. It will respond to either Input or Holding registers requests. When the service is started, or when running a discover
command (see below), a PDF file will be written to /opt/ardexa/config/modbus-server/modbus_server_mapping.pdf
, which will show the parameters of the configuration file, IP address, port and cache settings. It can be downloaded from the edit scenario page by selecting in the drop down.
Please note that for this service to be reliable a static IP address must be defined on the Ardexa device. In order to test the plugin is working ok, the service can be checked as follows: systemctl status modbus-server-plugin.service
Test Mode
There is also a test mode, used to serve known values for debugging. this is activated by the switch in the scenario page. These registers and their value can then be checked with a script that is run with the discover
button. The script and its configuration files, found in /opt/ardexa/config/modbus-server
, can be downloaded and configured locally to test a remote server. There should be a test_client.py
script, a test_server.txt
configuration and some .dict
lookups. There is also a multiple
option in the config interface, this will add duplicate test registers in multiples of 100, for example if multiple
is 2 then registers 1 and 101 will store the current hour.
Some of the test mode registers values are dynamic and others are fixed, as per the list below:
Last updated