Stand with Ukraine flag
Try it now Pricing
IoT Gateway
Documentation > Configuration guides > Modbus Connector
Getting Started
Installation
On this page

Modbus Connector Configuration

This comprehensive guide is designed to familiarize you with the Modbus connector configuration for ThingsBoard IoT Gateway. In the following sections, we will provide a detailed description of the connector configuration, covering all aspects necessary for successful implementation and customization.

The Modbus connector is a powerful tool that allows seamless integration between Modbus devices and the ThingsBoard platform.

Doc info icon

Please note: If you are new to IoT Gateway, use the “Basic” configuration mode. If you are familiar with configuring IoT Gateway, you can use the “Advanced” configuration mode.

Connector configuration

Connector configuration is a UI form that contains information about how to connect to Modbus slaves. Let’s look at all the available settings and explain each one clearly. This will help you understand how everything works.

Doc info icon

Please note: Connect the gateway before creating the connector. Otherwise, the UI will display the old configuration fields for backward compatibility.

Section “General”

This configuration section contains general connector settings, such as:

  • Name - connector name for logs and saving to persistent devices;
  • Logs configuration:
    • Enable remote logging - enabling remote logging for connector;
    • Logging level - logging level for local and remote logs: INFO, DEBUG, WARNING, ERROR, CRITICAL, TRACE, NONE;

image

Doc info icon

The general tab is the same for both the basic and advanced configurations.

Section “Master Connections”

A Modbus Master is used to query data from Modbus servers (slaves). In order to configure servers for data retrieving, you need to specify list of slaves.

Subsection “Server settings”

Select basic or advanced Modbus configuration:

To add new configuration for connection to server, navigate to the “Master Connections” tab and click the “plus” icon:

image

Due to the nature of preferred way of communication between Modbus master there are 2 options how to configure this part: if using TCP, UDP or via Serial port.

The table below describes the parameters required to configure TCP connection for slave:

Parameter Default value Description
Host 127.0.0.1 Hostname or ip address of Modbus server
Port 5021 Port of Modbus server for connection
Method Socket Type of a framer Socket or RTU, if needed
Unit ID 1 ID of current slave on Modbus
Device name Temp Sensor Name of the current slave
Device profile default Device profile of the current slave

image

The table below describes the parameters required to configure UDP connection for slave:

Parameter Default value Description
Host 127.0.0.1 Hostname or ip address of Modbus server
Port 5021 Port of Modbus server for connection
Method Socket Type of a framer Socket or RTU, if needed
Unit ID 1 ID of current slave on Modbus
Device name Temp Sensor Name of the current slave
Device profile default Device profile of the current slave

image

The table below describes the parameters required to configure serial connection for slave:

Parameter Default value Description
Port   Port of Modbus server for connection
Method Socket Type of a framer Socket or RTU, if needed
Baudrate 4800 Baudrate for The baud rate to use for the serial device
Bytesize 5 The number of bits in a byte of serial data. This can be one of 5, 6, 7, or 8
Stopbits 1 The number of bits sent after each character in a message to indicate the end of the byte
Parity None The type of checksum to use to verify data integrity. This can be on of the following: (E)ven, (O)dd, (N)one
Strict true Use Inter char timeout for baudrates <= 19200
Unit ID 1 ID of current slave on Modbus
Device name Temp Sensor Name of the current slave
Device profile default Device profile of the current slave

image

To add new configuration for connection to server, navigate to the “Configuration” tab.

image

Due to the nature of preferred way of communication between Modbus master there are 2 options how to configure this part: if using TCP, UDP or via Serial port.

The table below describes the parameters required to configure TCP connection for slave:

Parameter Default value Description
host 127.0.0.1 Hostname or ip address of Modbus server
port 5021 Port of Modbus server for connection
method Socket Type of a framer Socket or RTU, if needed
unitId 1 ID of current slave on Modbus
deviceName Temp Sensor Name of the current slave
deviceProfile default Device profile of the current slave

image


Example:

1
2
3
4
5
6
7
8
9
10
11
12
"master": {
    "slaves": [
        {
            "host": "127.0.0.1",
            "port": 5021,
            "method": "socket",
            "unitId": 1,
            "deviceName": "Temp Sensor",
            "deviceType": "default",
        }
    ]
}

The table below describes the parameters required to configure TCP connection for slave:

Parameter Default value Description
host 127.0.0.1 Hostname or ip address of Modbus server
port 5021 Port of Modbus server for connection
method Socket Type of a framer Socket or RTU, if needed
unitId 1 ID of current slave on Modbus
deviceName Temp Sensor Name of the current slave
deviceProfile default Device profile of the current slave

image


Example:

1
2
3
4
5
6
7
8
9
10
11
12
"master": {
    "slaves": [
        {
            "host": "127.0.0.1",
            "port": 5021,
            "method": "socket",
            "unitId": 1,
            "deviceName": "Temp Sensor",
            "deviceType": "default",
        }
    ]
}

The table below describes the parameters required to configure serial connection for slave:

Parameter Default value Description
port   Port of Modbus server for connection
method Socket Type of a framer Socket or RTU, if needed
baudrate 4800 Baudrate for The baud rate to use for the serial device
bytesize 5 The number of bits in a byte of serial data. This can be one of 5, 6, 7, or 8
stopbits 1 The number of bits sent after each character in a message to indicate the end of the byte
parity None The type of checksum to use to verify data integrity. This can be on of the following: (E)ven, (O)dd, (N)one
strict true Use Inter char timeout for baudrates <= 19200
unitId 1 ID of current slave on Modbus
deviceName Temp Sensor Name of the current slave
deviceProfile default Device profile of the current slave

image


Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"master": {
    "slaves": [
        {
            "method": "ascii",
            "baudrate": 4800,
            "stopbits": 1,
            "bytesize": 5,
            "parity": "N",
            "strict": true,
            "unitId": 1,
            "deviceName": "Temp Sensor",
            "deviceType": "default",
        }
    ]
}

Subsection “Report strategy”

Select basic or advanced Modbus configuration:

image

This subsection enabling report strategy for data sending, and you can use one of 3 behavior scenarios:

  • On report period - data will be collected and send every period of time (in milliseconds).
  • On value change - data will be send after change.
  • On value change or report period - data will be sent either when it changes or after a report period has passed since the last time it was sent.

The table below describes the parameters required to configure report strategy:

Parameter Default value Description
Type On report period Report strategy type: On report period, On value change, On value change or report period
Report period 30000 Period in milliseconds to send data to platform

image

This subsection enabling report strategy for data sending, and you can use one of 3 behavior scenarios:

  • ON_REPORT_PERIOD - data will be collected and send every period of time (in milliseconds).
  • ON_CHANGE - data will be send after change.
  • ON_CHANGE_OR_REPORT_PERIOD - data will be sent either when it changes or after a report period has passed since the last time it was sent.

The table below describes the parameters required to configure report strategy:

Parameter Default value Description
type ON_REPORT_PERIOD Report strategy type: ON_REPORT_PERIOD, ON_CHANGE_OR_REPORT_PERIOD, ON_CHANGE
reportPeriod 30000 Period in milliseconds to send data to platform

Configuration example:

1
2
3
4
reportStrategy: {
    "type": "ON_REPORT_PERIOD",
    "reportPeriod": 30000
}

Subsection “Advanced connection settings”

You can configure additional settings like: TLS connection, polling time, byte order and word order, and other. The table below describes the parameters for this purpose:

Parameter Default value Description
Connection timeout (s) 35 Timeout in seconds for connecting to Modbus server
Byte order LITTLE Order of bytes to read
Word order LITTLE The order of words when reading several registers
Retries true Retrying sending data to the master. The values can be either: true or false
Retries on empty true Retrying sending data to the master if it is empty
Retries on invalid true Retrying sending data to the master if it is failed
Poll period (ms) 5000 Period in milliseconds to check the attributes and the telemetry on the slave
Connect attempt time (ms) 5000 A waiting period in milliseconds before connecting to the master
Connect attempt count 5 The number of connection attempts made through the ThingsBoard gateway
Wait after failed attempts (ms) 300000 A waiting period in milliseconds before trying to send data to the master
  • Click on the “Advanced connection settings” subsection to open it;

  • Make the necessary settings.

Also, you can configure TLS connection (can be used only for TCP/UDP connection type). The table below describes the parameters required to configure TLS connection:

Parameter Description
Path to client certificate file Path to certificate file in filesystem with gateway
Path to private key file Path to private key file
Password Server password
Server hostname Server hostname (Should match with hostname in certificate)

image

You can configure additional settings like: TLS connection, polling time, byte order and word order, and other. The table below describes the parameters for this purpose:

Parameter Default value Description
timeout 35 Timeout in seconds for connecting to Modbus server
byteOrder LITTLE Order of bytes to read
wordOrder LITTLE The order of words when reading several registers
retries true Retrying sending data to the master. The values can be either: true or false
retriesOnEmpty true Retrying sending data to the master if it is empty
retriesOnInvalid true Retrying sending data to the master if it is failed
pollPeriod 5000 Period in milliseconds to check the attributes and the telemetry on the slave
connectAttemptTimeMs 5000 A waiting period in milliseconds before connecting to the master
connectAttemptCount 5 The number of connection attempts made through the ThingsBoard gateway
waitAfterFailedAttemptsMs 300000 A waiting period in milliseconds before trying to send data to the master

image


Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"master": {
  "slaves": [
    {
      "timeout": 35,
      "byteOrder": "LITTLE",
      "wordOrder": "LITTLE",
      "retries": true,
      "retryOnEmpty": true,
      "retryOnInvalid": true,
      "pollPeriod": 5000,
      "sendDataOnlyOnChange": true,
      "connectAttemptTimeMs": 5000,
      "connectAttemptCount": 5,
      "waitAfterFailedAttemptsMs": 300000,
    }
  ]
}

Subsection “Attributes” & “Time series”

Select basic or advanced Modbus configuration:

The configuration in this unit provides settings for processing data on Modbus server. These settings will be interpreted in ThingsBoard platform instance as attributes/time series of the device. For adding new attribute or time series use the following steps:

  • Click on “pencil” icon in the “Attributes” or “Time series” section (depends on the selected section);

  • In the opened window click “Add attribute” or “Add time series” (depends on selected section);

  • Fill in key, type, function code, objects count and address fields. Click “Apply”.


Also, modifier for attribute/time series value can be applied using the following settings:

Parameter Description
Type multiplier - the result of reading will be multiplied by the value of this parameter.
divider - the result of reading will be divided by the value of this parameter
Value The value that will be used to modify the read value

image

The configuration in this unit provides settings for processing data on Modbus server. These settings will be interpreted in ThingsBoard platform instance as attributes/time series of the device.

The table below describes the attributes and time series parameters:

Parameter Description
tag Key name
type Type of value
functionCode The function to use in data processing, specifically Modbus functions
objectsCount Count of objects to write
address Object address

image


Example of attributes section:

1
2
3
4
5
6
7
8
9
"attributes": [
  {
    "tag": "string_read",
    "type": "string",
    "functionCode": 4,
    "objectsCount": 4,
    "address": 1
  }
]

Example of time series section:

1
2
3
4
5
6
7
8
9
"timeseries": [
  {
    "tag": "16uint_read",
    "type": "16uint",
    "functionCode": 4,
    "objectsCount": 2,
    "address": 18
  },
]

Subsection “Attribute updates”

This configuration section is optional.

ThingsBoard allows to provision device attributes and fetch some of them from the device application. You can treat this as a remote configuration for devices. Your devices are able to request shared attributes from ThingsBoard. See user guide for more details.

The “attributeUpdates” configuration allows configuring the format of the corresponding attribute request and response messages.

Select basic or advanced Modbus configuration:

  • To add new requests mapping, navigate to the “Attribute updates” section and click the “pencil” icon;

  • In the opened window click “Add attribute update”;

  • Set an attribute key name, type, function code, objects count, and address. Then, click “Apply”.

The table below describes the attribute updates parameters:

Parameter Description
Key Shared attribute name
Type Type of value
Function code The function to use in data processing, specifically Modbus functions
Objects count Count of objects to write
Address Object address

For example, create a new attribute “maxTemperature” and give it a value “25”.

  • Go to the “Devices” page. Click on your device to open the device details, then navigate to the “Attributes” tab. Select “Server attributes” as the entity attributes scope. Then, click the “plus” icon to add a new attribute;

  • Name the attribute “maxTemperature”, select “Integer” as the value type, and set the value to 25;

  • Shared attribute is added.

The table below describes the attribute updates parameters:

Parameter Description
tag Shared attribute name
type Type of value
functionCode The function to use in data processing, specifically Modbus functions
objectsCount Count of objects to write
address Object address

image


Example:

1
2
3
4
5
6
7
8
9
"attributeUpdates": [
  {
    "tag": "shared_attribute_write",
    "type": "32int",
    "functionCode": 6,
    "objectsCount": 2,
    "address": 29
  }
]

Subsection “RPC requests”

ThingsBoard allows sending RPC commands to the device that is connected to ThingsBoard directly or via Gateway. Configuration, provided in this section is used for sending RPC requests from ThingsBoard to device.

Select basic or advanced Modbus configuration:

  • To add new requests mapping, navigate to the “RPC requests” section and click the “pencil” icon;

  • In the opened window click the “Add request”;

  • Fill in key, type, function code, objects count and address fields. Click “Apply”.

The table below describes the RPC request parameters:

Parameter Description
Key RPC method name
Type Type of value
Function code The function to use in data processing, specifically Modbus functions
Objects count Count of objects to write or read
Address Object address

The table below describes the RPC request parameters:

Parameter Description
tag RPC method name
type Type of value
functionCode The function to use in data processing, specifically Modbus functions
objectsCount Count of objects to write or read
address Object address

image


Example:

1
2
3
4
5
6
7
8
9
"rpc": [
  {
    "tag": "setValue",
    "type": "bits",
    "functionCode": 5,
    "objectsCount": 1,
    "address": 31
  }
]

Section “Server configuration”: description and configuration parameters

Gateway can run as a Modbus slave. Configuration section for the Modbus server, storing data and sending updates to the platform when changes occur or at fixed intervals.

Subsection “Server (Slave) configuration”

Select basic or advanced Modbus configuration:

Due to the nature of preferred way of communication between Modbus master there are 2 options how to configure this part: if using TCP/UDP or via Serial port.

image

The table below describes the parameters required to configure TCP connection for slave:

Parameter Default value Description
Host 127.0.0.1 Hostname or ip address of Modbus server
Port 5021 Port of Modbus server for connection
Method Socket Type of a framer Socket or RTU, if needed
Unit ID 1 ID of current slave on Modbus
Device name Temp Sensor Name of the current slave
Device profile default Device profile of the current slave
Send data to ThingsBoard false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every poll period
Poll period (ms) 5000 Period in milliseconds for checking the attributes and the telemetry

image

The table below describes the parameters required to configure UDP connection for slave:

Parameter Default value Description
Host 127.0.0.1 Hostname or ip address of Modbus server
Port 5021 Port of Modbus server for connection
Method Socket Type of a framer Socket or RTU, if needed
Unit ID 0 ID of current slave on Modbus
Device name Modbus Slave Example Name of the current slave
Device profile default Device profile of the current slave
Send data to ThingsBoard false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every poll period
Poll period (ms) 5000 Period in milliseconds for checking the attributes and the telemetry

image

The table below describes the parameters required to configure serial connection for slave:

Parameter Default value Description
Port   Port of Modbus server for connection
Method Socket Type of application data unit - RTU or ASCII
Baudrate 4800 Baudrate for The baud rate to use for the serial device
Strict true Use Inter char timeout for baudrates <= 19200
Unit ID 1 ID of current slave on Modbus
Device name Temp Sensor Name of the current slave Don’t use “Gateway” as the value of “Device name” parameter!
Device profile default Device profile of the current slave
Send data to platform false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every poll period.

image

Due to the nature of preferred way of communication between Modbus master there are 2 options how to configure this part: if using TCP/UDP or via Serial port.

image

The table below describes the parameters required to configure TCP connection for slave:

Parameter Default value Description
host 127.0.0.1 Hostname or ip address of Modbus server
port 5021 Port of Modbus server for connection
method socket Type of a framer Socket or RTU, if needed
unitId 0 ID of current slave on Modbus
deviceName Modbus Slave Example Name of the current slave
deviceProfile default Device profile of the current slave
sendDataToThingsBoard false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every poll period
pollPeriod 5000 Period in milliseconds for checking the attributes and the telemetry

image

The table below describes the parameters required to configure UDP connection for slave:

Parameter Default value Description
host 127.0.0.1 Hostname or ip address of Modbus server
port 5021 Port of Modbus server for connection
method socket Type of a framer Socket or RTU, if needed
unitId 0 ID of current slave on Modbus
deviceName Modbus Slave Example Name of the current slave
deviceProfile default Device profile of the current slave
sendDataToThingsBoard false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every poll period
pollPeriod 5000 Period in milliseconds for checking the attributes and the telemetry

image

The table below describes the parameters required to configure serial connection for slave:

Parameter Default value Description
port   Port of Modbus server for connection
method socket Type of application data unit - RTU or ASCII
unitId 0 ID of current slave on Modbus
deviceName Modbus Slave Example Name of the current slave Don’t use “Gateway” as the value of “Device name” parameter!
deviceProfile default Device profile of the current slave
pollPeriod 5000 Period in milliseconds to check attributes and telemetry on the slave
baudrate 4800 Baudrate for The baud rate to use for the serial device

image

Subsection “Advanced connection settings”

Select basic or advanced Modbus configuration:

You can configure additional settings like: TLS connection, byte order, word order, and other. The table below describes the parameters for this purpose:

Parameter Description
Byte order LITTLE
Word order LITTLE
Vendor name  
Product code  
Vendor URL  
Product name  
Model name  
  • Click on the “Advanced connection settings” subsection to open it;

  • Make the necessary settings.

You can configure additional settings like: TLS connection, byte order, word order, and other. The table below describes the parameters for this purpose:

Parameter Description
Byte order LITTLE
Word order LITTLE
Vendor name  
Product code  
Vendor URL  
Product name  
Model name  

image

Subsection “Values”

In this section you can specify initial values for registers and coils. Each value refers to a specific type of register. There are:

  1. Holding registers
  2. Coils initializer
  3. Input registers
  4. Discrete inputs

Depending on which value the register belongs to, you must add it to the appropriate array.

Select basic or advanced Modbus configuration:

image

To add new value to the gateway slave, use the following steps:

  • Click on “pencil” icon in the row of the selected section;

  • In the opened window click “Add attribute” or other (depends on selected section);

  • Fill in key, type, function code, objects count, address and value fields. Click “Apply”.

image

Additional information

Here’s more details regarding Modbus functions and the supported data types.

Modbus functions

The Modbus connector supports the following Modbus functions:

Modbus function code Description
Read data  
1 Read Coils
2 Read Discrete Inputs
3 Read Multiple Holding Registers
4 Read Input Registers
Write data:  
5 Write Coil
6 Write Register
15 Write Coils
16 Write Registers

Data types

A list and description of the supported data types for reading/writing data.

Type Function code Objects count Note
string 3-4 1-… Read bytes from registers and decode it (‘UTF-8’ coding).
bytes 3-4 1-… Read bytes from registers.
bits 1-4 1-… Read coils. If the objects count is 1, result will be interpreted as a boolean. Otherwise the result will be an array with bits.
16int 3-4 1 Integer 16 bit.
16uint 3-4 1 Unsigned integer 16 bit.
16float 3-4 1 Float 16 bit.
32int 3-4 2 Integer 32 bit.
32uint 3-4 2 Unsigned integer 32 bit.
32float 3-4 2 Float 32 bit.
64int 3-4 4 Integer 64 bit.
64uint 3-4 4 Unsigned integer 64 bit.
64float 3-4 4 Float 64 bit.

Next steps

Explore guides related to main ThingsBoard features: