This guide will help you to get familiar with Modbus connector configuration for ThingsBoard IoT Gateway. Use general configuration to enable this connector. We will describe connector configuration file below.
- Example of Modbus Connector config file. Press to expand.
{
"server": {
"type": "tcp",
"host": "127.0.0.1",
"port": 5020,
"timeout": 35,
"method": "socket",
"byteOrder": "BIG",
"devices": [
{
"unitId": 1,
"deviceName": "Temp Sensor",
"attributesPollPeriod": 5000,
"timeseriesPollPeriod": 5000,
"sendDataOnlyOnChange": true,
"attributes": [
{
"tag": "string_read",
"type": "string",
"functionCode": 4,
"objectsCount": 4,
"address": 1
},
{
"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": 2,
"address": 18
},
{
"tag": "32uint_read",
"type": "32uint",
"functionCode": 4,
"objectsCount": 4,
"address": 20
},
{
"tag": "64uint_read",
"type": "64uint",
"functionCode": 4,
"objectsCount": 1,
"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_write",
"type": "32int",
"functionCode": 6,
"objectsCount": 2,
"address": 29
}
],
"rpc": [
{
"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
}
]
}
]
}
}
Section “server”: description and configuration parameters
In order to connect to Modbus server, the one must set required parameters of section “server”. Due to the nature of preferred way of communication with Modbus server there are 2 options how to configure this part: if using TCP/UDP or via Serial port.
There are 2 variants of server section:
| TCP/UDP
Connection over TCP/UDP protocol
|
Serial
Connection over serial port
|
| —- | —- |
Parameter | Default value | Description |
---|---|---|
name | Modbus Default Server | Name of connector to server. |
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. |
timeout | 35 | Timeout in seconds for connecting to Modbus server. |
method | socket | Type of a framer socket or rtu, if needed. |
byteOrder | BIG | Order of bytes to read. |
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. |
Sub-section “devices”
This sub-section provides array of configurations for devices.
Device object settings
This configuration contains common connection parameters and settings for data processing. Available parameters are as follows:
Parameter | Default value | Description |
---|---|---|
unitId | 1 | Id of current device on Modbus. |
deviceName | Temp Sensor | Name of the current device |
attributesPollPeriod | 5000 | Period in milliseconds for check the attributes on device. |
timeseriesPollPeriod | 5000 | Period in milliseconds for check the telemetry on device. |
sendDataOnlyOnChange | true | Sending only if data changed from last check, if no – data will send after every check |
Example:
"devices": [
{
"unitId": 1,
"deviceName": "Temp Sensor",
"attributesPollPeriod": 5000,
"timeseriesPollPeriod": 5000,
"sendDataOnlyOnChange": true,
Key settings for “attributes”
Configuration in this unit provides settings for processing data on Modbus server, which will be interpreted in ThingsBoard platform instance as attribute of device.
Parameter | Default value | Description |
---|---|---|
tag | test | Tag, which will use as attribute key for ThingsBoard platform instance. |
type | 32int | Type of value. Available data types |
functionCode | 4 | Function to use in processing data. Modbus functions |
objectsCount | 1 | Count of objects to read. |
address | 1 | Object address to check. |
Optional parameters:
multiplier - result of reading will be multiplied by value of this parameter.
divider - result of reading will be divided by value of this parameter.
Example:
"attributes": [
{
"tag": "test",
"type": "32int",
"functionCode": 4,
"objectsCount": 1,
"address": 1
}
],
Key settings for “timeseries”
Configuration in this unit provides settings for processing data on Modbus server, which will be interpreted in ThingsBoard platform instance as telemetry of device.
Parameter | Default value | Description |
---|---|---|
tag | test | Tag, which will use as attribute key for ThingsBoard platform instance. |
type | 16uint | Type of value. Available data types |
functionCode | 4 | Function to use in processing data. Modbus functions |
objectsCount | 1 | Count of objects to read. |
address | 1 | Object address to check. |
Example:
"timeseries": [
{
"tag": "test",
"type": "16uint",
"functionCode": 4,
"objectsCount": 1,
"address": 1
}
],
Key settings for “attributeUpdates”
Configuration in this unit provides settings for Attribute update requests from ThingsBoard platform instance.
Parameter | Default value | Description |
---|---|---|
tag | shared_attribute_write | Shared attribute name. |
type | 32int | Type of value. Available data types |
functionCode | 6 | Function to use in processing data. Modbus functions |
objectsCount | 2 | Count of objects to write. |
address | 1 | Object address. |
Example:
"attributeUpdates": [
{
"tag": "shared_attribute_write",
"type": "32int",
"functionCode": 6,
"objectsCount": 2,
"address": 1
}
],
Key settings for “rpc”
Configuration in this unit provides settings for RPC requests from ThingsBoard platform instance to device.
Parameter | Default value | Description |
---|---|---|
tag | setValue | RPC method name. |
type | bits | Type of value. Available data types |
functionCode | 5 | Function to use in processing data. Modbus functions |
objectsCount | 1 | Count of objects to write. |
address | 1 | Object address. |
Example:
"rpc": [
{
"tag": "setValue",
"type": "bits",
"functionCode": 5,
"objectsCount": 1,
"address": 1
}
],
Additional information
Additional information about Modbus functions and 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 objects count 1 - result will be interpret as a boolean, else 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:
- Connect Modbus device - how to connect Modbus device using ThingsBoard IoT Gateway
- Data Visualization - how to visualize collected data.
- Device attributes - how to use device attributes.
- Telemetry data collection - how to collect telemetry data.
- Using RPC capabilities - how to send commands to/from devices.
- Rule Engine - how to use rule engine to analyze data from devices.