Skip to main content

Modbus TCP Overview

The MODBUS_TCP route enables communication with PLCs, RTUs, and industrial devices using the Modbus TCP/IP protocol. It supports reading and writing registers and coils with configurable polling intervals and automatic batch optimization.
Modbus TCP is one of the most widely supported industrial protocols. If your device supports Modbus, this route provides a reliable way to integrate it with MQTT.

Basic Syntax

DEFINE ROUTE ModbusDevice WITH TYPE MODBUS_TCP
    ADD MODBUS_CONFIG
        WITH HOST "192.168.1.100"
        WITH PORT '502'
        WITH SLAVE_ID '1'
    ADD MAPPING SensorReadings
        WITH EVERY 500 MILLISECONDS
        ADD TAG Temperature
            WITH ADDRESS "100"
            WITH ADDRESS_TYPE "HOLDING_REGISTER"
            WITH DATA_TYPE "FLOAT"
            WITH SOURCE_TOPIC "modbus/temperature"

Connection Configuration

MODBUS_CONFIG Parameters

HOST
string
required
Modbus TCP server hostname or IP address.
PORT
integer
Modbus TCP port. Default: 502.
SLAVE_ID
integer
Modbus slave/unit ID (1-247). Default: 1.
CONNECTION_TIMEOUT
integer
Connection timeout in milliseconds. Default: 5000.
READ_TIMEOUT
integer
Read operation timeout in milliseconds. Default: 1000.
WRITE_TIMEOUT
integer
Write operation timeout in milliseconds. Default: 1000.
RETRY_COUNT
integer
Number of retry attempts on failure. Default: 3.
RETRY_DELAY
integer
Delay between retries in milliseconds. Default: 100.

Connection Example

ADD MODBUS_CONFIG
    WITH HOST "192.168.1.100"
    WITH PORT '502'
    WITH SLAVE_ID '1'
    WITH CONNECTION_TIMEOUT '5000'
    WITH READ_TIMEOUT '1000'
    WITH WRITE_TIMEOUT '1000'
    WITH RETRY_COUNT '3'
    WITH RETRY_DELAY '100'

Address Types

Modbus defines four address spaces:
Address TypeDescriptionFunction CodesTypical Use
HOLDING_REGISTERRead/Write 16-bit registersFC03, FC06, FC16Setpoints, configuration
INPUT_REGISTERRead-only 16-bit registersFC04Sensor values, measurements
COILRead/Write single bitsFC01, FC05, FC15Digital outputs, controls
DISCRETE_INPUTRead-only single bitsFC02Digital inputs, status
Most Modbus devices use Holding Registers for configuration and Input Registers for measured values. Check your device documentation for the correct address type.

Data Types

DATA_TYPE
string
required
The data type determines how raw register values are interpreted:
Data TypeSizeDescription
BOOL1 bitBoolean value (for coils/discrete inputs)
INT88 bitsSigned 8-bit integer
UINT88 bitsUnsigned 8-bit integer
INT1616 bitsSigned 16-bit integer (1 register)
UINT1616 bitsUnsigned 16-bit integer (1 register)
INT3232 bitsSigned 32-bit integer (2 registers)
UINT3232 bitsUnsigned 32-bit integer (2 registers)
INT6464 bitsSigned 64-bit integer (4 registers)
UINT6464 bitsUnsigned 64-bit integer (4 registers)
FLOAT32 bits32-bit floating point (2 registers)
DOUBLE64 bits64-bit floating point (4 registers)
STRINGVariableASCII string across multiple registers

TAG Configuration

Complete TAG Parameters

ADD TAG TemperatureSensor
    WITH ADDRESS "100"
    WITH ADDRESS_TYPE "HOLDING_REGISTER"
    WITH DATA_TYPE "FLOAT"
    WITH SOURCE_TOPIC "modbus/sensors/temperature"
    WITH SCALING "0.1"
    WITH OFFSET "0"
    WITH UNIT "°C"
    WITH DECIMAL_PLACES "2"
    WITH MIN_VALUE "-40"
    WITH MAX_VALUE "150"
    WITH DEADBAND "0.5"
    WITH PUBLISH_MODE "JSON"
    WITH WRITABLE "true"
    WITH DESTINATION_TOPIC "modbus/sensors/temperature/set"
    WITH BYTE_ORDER "BIGENDIAN"
    WITH WORD_ORDER "BIGENDIAN"
    WITH DESCRIPTION "Main process temperature sensor"

TAG Parameters Reference

ADDRESS
string
required
Register or coil address (numeric value). This is the zero-based address in the Modbus address space.
ADDRESS_TYPE
string
required
Type of Modbus address: HOLDING_REGISTER, INPUT_REGISTER, COIL, or DISCRETE_INPUT.
SCALING
double
Multiplier applied to raw value. Default: 1.0. Example: Raw value 245 with SCALING “0.1” becomes 24.5.
OFFSET
double
Value added after scaling. Default: 0.0. Formula: result = (raw * scaling) + offset.
DECIMAL_PLACES
integer
Number of decimal places in published value. Default: 2.
MIN_VALUE
double
Minimum allowed value. Values below this threshold are not published.
MAX_VALUE
double
Maximum allowed value. Values above this threshold are not published.
DEADBAND
double
Minimum change required to publish a new value. Default: 0.0.
SOURCE_TOPIC
string
MQTT topic to publish read values.
PUBLISH_MODE
string
Output format: VALUE_ONLY or JSON. Default: VALUE_ONLY.
UNIT
string
Engineering unit for documentation (e.g., °C, bar, RPM).
DESCRIPTION
string
Human-readable description of the TAG.
WRITABLE
boolean
Allow writing to this register/coil. Default: false.
DESTINATION_TOPIC
string
MQTT topic to subscribe for write commands.
BYTE_ORDER
string
Byte order for multi-byte values: BIGENDIAN or LITTLEENDIAN. Default: BIGENDIAN.
WORD_ORDER
string
Word order for 32/64-bit values: BIGENDIAN or LITTLEENDIAN. Default: BIGENDIAN.
Different manufacturers use different byte/word ordering. If values appear incorrect, try changing BYTE_ORDER or WORD_ORDER.

Event-Based Operations

For on-demand Modbus operations (not polling), use the EVENT syntax:
ADD EVENT ReadOnDemand
    WITH SOURCE_TOPIC "modbus/commands/read"
    WITH DESTINATION_TOPIC "modbus/responses/read"
    WITH QUERY '{"operation": "READ_HOLDING_REGISTERS", "start_address": 0, "count": 10}'

Supported Operations

OperationDescription
READ_HOLDING_REGISTERSRead holding registers (FC03)
READ_INPUT_REGISTERSRead input registers (FC04)
READ_COILSRead coils (FC01)
READ_DISCRETE_INPUTSRead discrete inputs (FC02)
WRITE_SINGLE_REGISTERWrite single holding register (FC06)
WRITE_MULTIPLE_REGISTERSWrite multiple holding registers (FC16)
WRITE_SINGLE_COILWrite single coil (FC05)
WRITE_MULTIPLE_COILSWrite multiple coils (FC15)

Complete Examples

Read temperature and pressure from a Modbus device:
DEFINE ROUTE ProcessSensors WITH TYPE MODBUS_TCP
    ADD MODBUS_CONFIG
        WITH HOST "192.168.1.100"
        WITH PORT '502'
        WITH SLAVE_ID '1'
    ADD MAPPING Sensors
        WITH EVERY 1 SECOND
        ADD TAG Temperature
            WITH ADDRESS "100"
            WITH ADDRESS_TYPE "HOLDING_REGISTER"
            WITH DATA_TYPE "FLOAT"
            WITH SOURCE_TOPIC "process/temperature"
            WITH SCALING "0.1"
            WITH UNIT "°C"
        ADD TAG Pressure
            WITH ADDRESS "102"
            WITH ADDRESS_TYPE "HOLDING_REGISTER"
            WITH DATA_TYPE "FLOAT"
            WITH SOURCE_TOPIC "process/pressure"
            WITH UNIT "bar"

Troubleshooting

  • Verify IP address and port are correct
  • Check firewall settings (port 502 must be open)
  • Ensure device is powered on and connected to network
  • Try increasing CONNECTION_TIMEOUT
  • Verify ADDRESS matches device documentation
  • Check BYTE_ORDER and WORD_ORDER settings
  • Confirm DATA_TYPE matches register size
  • Verify SCALING and OFFSET calculations
  • Ensure WRITABLE is set to “true”
  • Verify ADDRESS_TYPE is HOLDING_REGISTER or COIL
  • Check device permissions for write operations
  • Verify value is within MIN_VALUE and MAX_VALUE
  • Confirm correct SLAVE_ID (usually 1 for TCP devices)
  • Some gateways use different slave IDs for different devices
  • Check device documentation for correct unit ID

Next Steps