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

Modbus Connector Configuration

Doc info icon

Note that the configuration of the Modbus connector has changed since Gateway 3.0. The new configuration will be generated after installing the new version and running Gateway in the new_modbus.json file.

This guide will help you to get familiar with Modbus connector configuration for ThingsBoard IoT Gateway. Use general configuration to enable this connector. The connector configuration file will be described below.

Example of Modbus Connector config file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
{
  "master": {
    "slaves": [
      {
        "host": "host.docker.internal",
        "port": 5021,
        "type": "tcp",
        "method": "socket",
        "timeout": 35,
        "byteOrder": "LITTLE",
        "wordOrder": "LITTLE",
        "retries": true,
        "retryOnEmpty": true,
        "retryOnInvalid": true,
        "pollPeriod": 5000,
        "unitId": 1,
        "deviceName": "Demo Device",
        "deviceType": "default",
        "sendDataOnlyOnChange": false,
        "connectAttemptTimeMs": 5000,
        "connectAttemptCount": 5,
        "waitAfterFailedAttemptsMs": 300000,
        "attributes": [
          {
            "tag": "bits_read",
            "type": "bits",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 5
          },
          {
            "tag": "8int_read",
            "type": "8int",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 6
          },
          {
            "tag": "16int_read",
            "type": "16int",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 7
          },
          {
            "tag": "32int_read_divider",
            "type": "32int",
            "functionCode": 4,
            "objectsCount": 2,
            "address": 8,
            "divider": 10
          },
          {
            "tag": "8int_read_multiplier",
            "type": "8int",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 10,
            "multiplier": 10
          },
          {
            "tag": "32int_read",
            "type": "32int",
            "functionCode": 4,
            "objectsCount": 2,
            "address": 11
          },
          {
            "tag": "64int_read",
            "type": "64int",
            "functionCode": 4,
            "objectsCount": 4,
            "address": 13
          }
        ],
        "timeseries": [
          {
            "tag": "8uint_read",
            "type": "8uint",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 17
          },
          {
            "tag": "16uint_read",
            "type": "16uint",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 18
          },
          {
            "tag": "32uint_read",
            "type": "32uint",
            "functionCode": 4,
            "objectsCount": 2,
            "address": 20
          },
          {
            "tag": "64uint_read",
            "type": "64uint",
            "functionCode": 4,
            "objectsCount": 4,
            "address": 24
          },
          {
            "tag": "16float_read",
            "type": "16float",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 25
          },
          {
            "tag": "32float_read",
            "type": "32float",
            "functionCode": 4,
            "objectsCount": 2,
            "address": 26
          },
          {
            "tag": "64float_read",
            "type": "64float",
            "functionCode": 4,
            "objectsCount": 4,
            "address": 28
          }
        ],
        "attributeUpdates": [
          {
            "tag": "shared_attribute",
            "type": "16int",
            "functionCode": 16,
            "objectsCount": 1,
            "address": 29
          }
        ],
        "rpc": [
          {
            "tag": "shared_attribute_read",
            "type": "16int",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 29
          },
          {
            "tag": "setValue",
            "type": "bits",
            "functionCode": 5,
            "objectsCount": 1,
            "address": 31
          },
          {
            "tag": "getValue",
            "type": "bits",
            "functionCode": 1,
            "objectsCount": 1,
            "address": 31
          },
          {
            "tag": "setCPUFanSpeed",
            "type": "32int",
            "functionCode": 16,
            "objectsCount": 2,
            "address": 33
          },
          {
            "tag": "getCPULoad",
            "type": "32int",
            "functionCode": 4,
            "objectsCount": 2,
            "address": 35
          }
        ]
      }
    ]
  },
  "slave": {
    "type": "tcp",
    "host": "127.0.0.1",
    "port": 5026,
    "method": "socket",
    "deviceName": "Modbus_Slave_Example",
    "deviceType": "default",
    "pollPeriod": 5000,
    "sendDataToThingsBoard": false,
    "byteOrder": "BIG",
    "unitId": 0,
    "values": {
      "holding_registers": [
        {
          "attributes": [
            {
              "tag": "sm",
              "type": "string",
              "objectsCount": 1,
              "address": 1,
              "value": "ON"
            }
          ],
          "timeseries": [
            {
              "tag": "smm",
              "type": "int",
              "objectsCount": 1,
              "address": 2,
              "value": "12334"
            }
          ],
          "attributeUpdates": [
            {
              "tag": "shared_attribute_write",
              "type": "32int",
              "functionCode": 6,
              "objectsCount": 2,
              "address": 29,
              "value": 1243
            }
          ],
          "rpc": [
            {
              "tag": "setValue",
              "type": "bits",
              "functionCode": 5,
              "objectsCount": 1,
              "address": 31,
              "value": 22
            }
          ]
        }
      ],
      "coils_initializer": [
        {
          "attributes": [
            {
              "type": "string",
              "tag": "sm",
              "objectsCount": 1,
              "address": 5,
              "value": "12"
            }
          ],
          "timeseries": [],
          "attributeUpdates": [],
          "rpc": []
        }
      ]
    }
  }
}

Section “master”: description and configuration parameters

A Modbus Master is used to query data from devices. In order to configure the slaves that the master must query, you need to specify the “master” section which includes the “slaves” list.

Subsection “slaves”

This subsection provides an array of configurations for slaves.

Slave object settings

This configuration contains common connection parameters and settings for data processing.

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.

There are 2 variants of server section:

Parameter Default value Description
name Modbus Default Server Name of connector to server.
host host.docker.internal Hostname or ip address of Modbus server.
port 5021 Port of Modbus server for connection.
type tcp Type of connection may be TCP, UDP or Serial.
method socket Type of a framer Socket or RTU, if needed.
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.

Also, you can configure TLS connection using the following configuration:

Parameter Default value Description
certfile /etc/thingsboard-gateway/certificate.pem Path to certificate file.
keyfile /etc/thingsboard-gateway/privateKey.pem Path to private key file.
password YOUR_PASSWORD Server password.
server_hostname localhost Server host name.

Configuration example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "master": {
    "slaves": [
      {
        "host": "host.docker.internal",
        "port": 5021,
        "type": "tcp",
        "method": "socket",
        "tls": {
          "certfile": "/etc/thingsboard-gateway/certificate.pem",
          "keyfile": "/etc/thingsboard-gateway/privateKey.pem",
          "password": "YOUR_PASSWORD",
          "server_hostname": "localhost"
        },
        "timeout": 35,
        ...
Parameter Default value Description
name Modbus Default Server Name of connector to server.
type serial Type of connection may be tcp, udp or serial.
method rtu Type of application data unit - RTU or ASCII
port /dev/ttyUSB0 Serial port for connection.
baudrate 19200 Baudrate for The baud rate to use for the serial device.
stopbits 1 The number of bits sent after each character in a message to indicate the end of the byte.
bytesize 8 The number of bits in a byte of serial data. This can be one of 5, 6, 7, or 8.
parity N 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.
timeout 35 Timeout in seconds for connecting to Modbus server.
byteOrder BIG Order of bytes to read.

The next part of the slave section contains common connection parameters and settings for data processing. The available parameters are as follows:

Parameter Default value Description
retries true Retrying sending data to the master. The values can be either: true or false
retryOnEmpty true Retrying sending data to the master if it is empty
retryOnInvalid 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
unitId 1 Id of current slave on Modbus.
deviceName Demo Device Name of the current slave
deviceType default Type of the current slave
sendDataOnlyOnChange false Sending only if data has changed since the last check. If not, data will be sent after every check
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


Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
  "master": {
    "slaves": [
      {
        "host": "host.docker.internal",
        "port": 5021,
        "type": "tcp",
        "method": "socket",
        "timeout": 35,
        "byteOrder": "LITTLE",
        "wordOrder": "LITTLE",
        "retries": true,
        "retryOnEmpty": true,
        "retryOnInvalid": true,
        "pollPeriod": 5000,
        "unitId": 1,
        "deviceName": "Demo Device",
        "deviceType": "default",
        "sendDataOnlyOnChange": false,
        "connectAttemptTimeMs": 5000,
        "connectAttemptCount": 5,
        "waitAfterFailedAttemptsMs": 300000
      }
    ]
  }
}
Key settings for “attributes”

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

Parameter Default value Description
tag certificateNumber Tag, which will be used as attribute key for ThingsBoard platform instance
type 32int Type of value. Available data types
functionCode 4 The function to use in data processing, specifically Modbus functions
objectsCount 2 Count of objects to read.
address 0 Object address to check.

Optional parameters:
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.


Example:

1
2
3
4
5
6
7
8
9
10
11
        ...
          "attributes": [
          {
            "tag": "certificateNumber",
            "type": "32int",
            "functionCode": 4,
            "objectsCount": 2,
            "address": 0
          }
        ],
        ...

image

image

Key settings for “timeseries”

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

Parameter Default value Description
tag telemetry_1 Tag, which will be used as attribute key for ThingsBoard platform instance.
type 16uint Type of value. Available data types
functionCode 4 The function to use in data processing, specifically Modbus functions
objectsCount 1 Count of objects to read.
address 1 Object address to check.
     
tag telemetry_2 Tag, which will be used as attribute key for ThingsBoard platform instance.
type 16uint Type of value. Available data types
functionCode 4 The function to use in data processing, specifically Modbus functions
objectsCount 1 Count of objects to read.
address 3 Object address to check.


Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        ...
        "timeseries": [
          {
            "tag": "telemetry_1",
            "type": "16uint",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 1
          },
          {
            "tag": "telemetry_2",
            "type": "16uint",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 3
          }
        ],
        ...

image

image

Key settings for “attributeUpdates”

The configuration in this unit provides settings for Attribute update requests from ThingsBoard platform instance.

Parameter Default value Description
tag maxTemperature Shared attribute name.
type 16int Type of value. Available data types
functionCode 16 The function to use in data processing, specifically Modbus functions
objectsCount 1 Count of objects to write.
address 29 Object address.


Example:

1
2
3
4
5
6
7
8
9
10
11
        ...
        "attributeUpdates": [
          {
            "tag": "maxTemperature",
            "type": "16int",
            "functionCode": 16,
            "objectsCount": 1,
            "address": 29
          }
        ],
        ...

image

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

image

image

Key settings for “RPC”

The configuration in this unit provides settings for RPC requests from ThingsBoard platform instance to the device.

Parameter Default value Description
tag maxTemperature_read RPC method name.
type 16int Type of value. Available data types
functionCode 3 The function to use in data processing, specifically Modbus functions
objectsCount 1 Count of objects to write.
address 29 Object address.


Example:

1
2
3
4
5
6
7
8
9
10
11
        ...
        "rpc": [
          {
            "tag": "maxTemperature_read",
            "type": "16int",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 29
          }
        ],
        ...

image

To send RPC requests to the gateway, use the RPC Debug Terminal from the Control widgets bundle.

Execute “maxTemperature_read” RPC request from the ThingsBoard instance to the device.

In response, you should receive the previously recorded value - “25”.

image

Also, every telemetry and attribute parameter has built-in GET and SET RPC methods out of the box, so you don’t need to configure it manually. To use them, make sure you set all required parameters (in the case of Modbus Connector, these are the following: type, functionCode, objectsCount, address). See the guide.

Section “slave”: description and configuration parameters

Starting with version 3.0, Gateway can run as a Modbus slave. In order to configure Gateway as a Modbus slave, specify the “slave” section in the configuration file.

There are 2 variants of Gateway slave section:

Parameter Default value Description
type tcp Type of connection may be TCP, UDP or Serial.
host 127.0.0.1 Hostname or ip address of Modbus server.
port 5020 Port of Modbus server for connect.
method socket Type of framer Socket or RTU, if needed.
deviceName Modbus_Slave_Example Device name
deviceType default Device type
pollPeriod 5000 Period in milliseconds for checking the attributes and the telemetry.
sendDataToThingsBoard false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every millisecond
byteOrder BIG Order of bytes to read.
unitId 0 Unit id of the device

Also, you can configure TLS connection using the following configuration:

Parameter Default value Description
certfile /etc/thingsboard-gateway/certificate.pem Path to certificate file.
keyfile /etc/thingsboard-gateway/privateKey.pem Path to private key file.
password YOUR_PASSWORD Server password.
reqclicert false Request certificate file from the client.

Configuration example:

1
2
3
4
5
6
7
8
9
10
"slave": {
  "type": "tcp",
  "security": {
    "certfile": "/etc/thingsboard-gateway/certificate.pem",
    "keyfile": "/etc/thingsboard-gateway/privateKey.pem",
    "password": "YOUR_PASSWORD",
    "reqclicert": false
  },
  "host": "127.0.0.1",
  ...
Parameter Default value Description
deviceName Gateway Device name
deviceType default Device type
type serial Type of connection may be TCP, UDP or Serial.
method rtu Type of application data unit - RTU or ASCII
port /dev/ttyUSB0 Serial port for connection.
baudrate 19200 Baudrate for The baud rate to use for the serial device.
byteOrder BIG Order of bytes to read.
unitId 0 Unit id of the device
pollPeriod 5000 Period in milliseconds for checking the attributes and the telemetry.
sendDataToThingsBoard false If set to TRUE, the Gateway will perform autoconfiguration and send values to ThingsBoard every millisecond
Doc info icon

Don’t use “Gateway” as the value of “deviceName” parameter!


Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "slave": {
    "type": "tcp",
    "host": "127.0.0.1",
    "port": 5026,
    "method": "socket",
    "deviceName": "Modbus_Slave_Example",
    "deviceType": "default",
    "pollPeriod": 5000,
    "sendDataToThingsBoard": false,
    "byteOrder": "BIG",
    "unitId": 0
  }
}

You can also specify service information about the device using the “identity” subsection as follows:

Parameter Default value
vendorName Gateway
productCode GT
vendorUrl https://github.com/thingsboard/thingsboard-gateway
productName Gateway
modelName 1

Subsection “values”

In this section you can specify values that Gateway will save in the store. 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. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"values": {
      "holding_registers": [
        {
          "attributes": [
            {
              "tag": "sm",
              "type": "string",
              "objectsCount": 1,
              "address": 1,
              "value": "ON"
            }
          ],
          "timeseries": [
            {
              "tag": "smm",
              "type": "int",
              "objectsCount": 1,
              "address": 2,
              "value": "12334"
            }
          ],
          "attributeUpdates": [
            {
              "tag": "shared_attribute_write",
              "type": "32int",
              "functionCode": 6,
              "objectsCount": 2,
              "address": 29,
              "value": 1243
            }
          ],
          "rpc": [
            {
              "tag": "setValue",
              "type": "bits",
              "functionCode": 5,
              "objectsCount": 1,
              "address": 31,
              "value": 22
            }
          ]
        }
      ], 
        "coils_initializer": [
        {
          "attributes": [
            {
              "tag": "sm",
              "type": "string",
              "objectsCount": 1,
              "address": 5,
              "value": "12"
            }
         ],
          "timeseries": [],
          "attributeUpdates": [],
          "rpc": []
        }
      ]
    }
  }
}

Doc info icon

In this section of the configuration, the only difference is in the placement of values to a certain type of register. However, the configuration of telemetry, attributes, RPCs, etc., remains the same as in the “master” section.

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: