Modbus/TCP
Configure Connectware to read from and write to Modbus/TCP devices.
Modbus/TCP is a communication protocol that is a variant of the Modbus family. It is based on a client/server architecture and intended for use in industrial automation, especially with PLCs or I/O modules. Modbus/TCP is defined in IEC 61158.
Modbus uses big-endian byte order for addresses and data items.
Protocol Structure
Each Modbus/TCP frame consists of the following fields:
2 byte
2 byte (always 0x0000)
2 byte (n+2)
1 byte
1 byte
n byte
For configuration reference, see:
Supported Function Codes
For reading or writing data over Modbus/TCP, the protocol provides a set of functions. Which action should be performed on the other end of the connection is defined by the function code (FC).
The following function codes are supported:
1
Read coils
read, subscribe
2
Read discrete inputs
read, subscribe
3
Read holding registers
read, subscribe
4
Read input registers
read, subscribe
5
Write single coil
write
6
Write single holding register
write
15
Write multiple coils
write
16
Write multiple holding registers
write
Supported Data Types
In Modbus/TCP there are no predefined data types. Connectware supports sending the raw payload over MQTT as binary. If the data type is known and supported by the respective function code, the payload is converted to JSON format.
The desired data type on read or on write is specified with the property dataType. The possible values are as follows:
raw
✓
✓
✓
✓
variable
boolean
✓
✓
x
x
1 bit
base64
x
x
✓
✓
variable
int16BE
x
x
✓
✓
16 bit
int16LE
x
x
✓
✓
16 bit
int32BE
x
x
✓
✓
32 bit
int32LE
x
x
✓
✓
32 bit
uint16BE
x
x
✓
✓
16 bit
uint16LE
x
x
✓
✓
16 bit
uint32BE
x
x
✓
✓
32 bit
uint32LE
x
x
✓
✓
32 bit
floatBE
x
x
✓
✓
32 bit
floatLE
x
x
✓
✓
32 bit
floatBEWS
x
x
✓
✓
32 bit
floatLEWS
x
x
✓
✓
32 bit
doubleBE
x
x
✓
✓
64 bit
doubleLE
x
x
✓
✓
64 bit
The floatBEWS and floatLEWS types apply the same conversion as floatBE and floatLE, but swap the two 16-bit words before reading or writing the value. Use them for devices that store 32-bit floats in word-swapped register order.
If the payload is smaller than the required bits for the conversion (payload.length < dataType.length), an error is displayed in the logs and no message is sent over MQTT. If the payload has more bits (payload.length > dataType.length) than needed for the conversion, the extra bits are ignored and a message is sent over MQTT.
The following table maps Connectware data types to common names used in other documentation:
char
8 bit
0
255
byte
8 bit
-128
127
short (int16)
16 bit
-2^15
2^15-1
int (int32)
32 bit
-2^31
2^31-1
uint (uint32)
32 bit
0
2^32-1
long64
64 bit
-2^63
2^63-1
float
32 bit
IEEE 754
IEEE 754
double
64 bit
IEEE 754
IEEE 754
Input Format
This Modbus implementation supports writing using the standard function codes 5, 6, 15 and 16. In most cases you can directly send a number in the message payload when you are trying to write to a coil or a single register (function codes 5 or 6, respectively).
When trying to write several coils at once (function code 15), the message itself must be an array of boolean values, for example: [true, true, false, true].
Alternatively, if your goal is to write several registers at once, the data type of the endpoint needs to be considered. The property dataType (see Endpoint Properties) specifies how to properly serialize the data into an array of bytes suitable to write into the Modbus registers.
For the available integer and float data types (see Supported Data Types above) you can directly send the value in the message payload. A BigInt must be sent as a string value. You can also send base64 and utf8 encoded strings that will be parsed using the Node.js Buffer class.
Raw data in the form of an array of bytes is also supported.
Input Format on Write
To write data to Modbus, publish the value itself to the /set topic of the endpoint. The function code (fc) and dataType come from the endpoint configuration, so the message contains no other properties. For example, to write the number 42:
To write several coils with function code 15, publish an array of boolean values:
Output Format on Write
Connectware publishes the result on the /res topic of the endpoint. The result is a JSON object with details of the Modbus request and the response of the device. Its content depends on the function code.
Output Format
Output Format on Read
If dataType is set, a read endpoint publishes the result on its /res topic and a subscribe endpoint publishes on the endpoint topic. The message is an object with timestamp, in milliseconds since the Unix epoch, and value:
value is the JSON representation of the configured dataType, that is, a number, a string, or for raw the JSON representation of a JavaScript buffer object.
Alternatively, the property dataType can be left undefined, in which case the output is a JavaScript buffer object directly. With batch read processing enabled, interval-based subscribe endpoints publish an object with timestamp and value instead. The buffer output is useful for further processing using suitable mappings.
Batch Read Processing
By default, Connectware reads each Modbus endpoint individually, sending one request per endpoint to the device. In deployments with many endpoints, this can result in slow read cycles, especially when the device or network introduces even small delays with each request.
Batch read processing improves this by combining multiple read requests into a single, larger one wherever possible. Connectware automatically identifies which endpoints can be batched — those sharing the same unit ID and function code with addresses close enough to fit within a single read range. This reduces the total number of requests per cycle and can significantly shorten read times for large configurations.
Enabling Batch Read Processing
Enable batch processing by setting batchReadEnabled and maxBatchSize on the Modbus connection resource. For more information, see Connection Properties.
Example
The following example enables batch processing on a Modbus connection:
Service Commissioning File Example
The following example demonstrates how to configure a simple Modbus connection and endpoint that reads holding registers (FC 3) from the device.
Last updated
Was this helpful?

