modbus-write
Description
The modbus-write block is used to send values to a Modbus RTU device over the RS485 port. After each change received at the input, the block attempts to write the value to the configured device address using the selected Modbus function.
The block has one main input and one output. It does not require the use of inputs labeled using the tag block.
The supported write mode depends on the “Function code” parameter:
5: write a single coil6: write a single register15: write multiple coils16: write multiple registers
Configuration parameters
| Parameter name | Value |
|---|---|
Name (name) |
Name displayed on the block in the editor. If empty, modbus-write is displayed. |
Function code (functionCode) |
Required parameter. Defines the write type: 5 for a single coil, 6 for a single register, 15 for multiple coils, 16 for multiple registers. |
Device address (deviceAddress) |
Required parameter. Modbus slave device address in the range 1 to 255. |
Register address (registerAddress) |
Required parameter. Address of the first coil or first register in the range 0 to 65535. |
Bit count (bitCount) |
Visible and required only when “Function code” is set to 15; hidden for 5, 6, and 16. Defines the number of bits to write. Allowed range: 1-32. |
Value type (valueType) |
Visible and required only when “Function code” is set to 16; hidden for 5, 6, and 15. Allowed values: NUMBER16, NUMBER32, and FLOAT. |
Response timeout [ms] (responseTimeout) |
Required parameter. Maximum time to wait for the device response in milliseconds. Allowed range: 10-60000 ms. |
Multiplier (multiplier) |
Required parameter, visible for all functions. For functions 6, 15, and 16, the input value is multiplied by this parameter before being written; for function 5, it does not affect the written value. Allowed range: 1-4294967295. |
Endianess (endianess) |
Byte and word order selection, visible for all functions. Required when the written value uses more than 1 byte, that is for functions 6, 16, and for function 15 when “Bit count” exceeds 8. Allowed values: LittleEndian, BigEndian, SwapBytes, SwapWords. |
Client config (server) |
Required selection of a modbus-client configuration providing the RS485 port settings. Edit the port settings in the selected client configuration. |
Write retries
The write retry limit is set by maxWriteAttempts in the selected modbus-client configuration in flows.json. This is a client setting, rather than a separate parameter of the modbus-write block. It accepts an integer in the 0–65535 range or a string of digits, such as "3". It is independent of the read retry limit maxAttempts.
The value specifies the number of additional retries after the first attempt:
| Retry limit | Maximum number of attempts for one write |
|---|---|
0 |
1 |
1 |
2 |
3 (default) |
4 |
65535 |
65536 |
A missing field or an invalid value uses the default limit of 3 and does not by itself block the client configuration. Older flows without this field also use three additional write retries. Availability of the field in the editor form depends on deployment of support for this setting in the Node-RED nodes.
The shared limit is the highest maxWriteAttempts value across all modbus-client configurations in the current project. If all have 0, writes are not retried. With two clients set to 0 and 2, all writes can have two additional retries, including writes from the client set to zero. If there is no project or no client configuration, the limit is 3. Each new write receives its limit once; later configuration changes do not change the retries available to writes already waiting to be performed.
Retries cover all four supported write functions when confirmation times out, the device responds with an error, or the RS485 controller reports a communication error. Invalid data and local request preparation or send failures end the write without further retries. Success ends the write immediately, without using the remaining attempts.
Other reads and writes, including those to other devices, wait until the current write succeeds or finally fails. A higher limit can therefore delay the result and data refresh. If confirmation is lost, the device may receive the same command again; execution exactly once is not guaranteed.
Input handling
The block has one main input. It is enough for this input to be connected to at least one data source. Each valid change received creates one separate write to the Modbus device, which may have several attempts according to the retry limit.
The block does not have inputs labeled as tag1, tag2, and so on.
Operating rules for each function:
- for function
5, the input must be readable as an integer value 0 or 1 - for function
6, the input must be a numeric value; after applying “Multiplier”, the fractional part is discarded and the result must fit in one 16-bit register - for function
15, the input must be a non-negative numeric value; after applying “Multiplier”, the fractional part is discarded and the result must fit in the number of bits defined by “Bit count” - for function
16, the input must be a numeric value; the write method depends on “Value type”
Detailed behavior for function 16:
NUMBER16: after applying “Multiplier”, the fractional part is discarded and the result is written as a 16-bit register valueNUMBER32: after applying “Multiplier”, the fractional part is discarded and the result is written as a 32-bit register valueFLOAT: after applying “Multiplier”, the value is written as a 32-bit floating-point number
Byte order is used only when the write covers more than one byte of data. For function 15, the “Endianess” parameter has no effect if “Bit count” does not exceed 8.
An invalid input value is not ignored. It causes the block to enter the error state and return false at the output.
Block outputs
The block has one output.
A logical value appears at the output:
- true after a successful write confirmed by the Modbus device
- false after a final write error, communication error, or request rejection by the device
The write result is determined only after success or final failure. Failed attempts followed by a retry do not cause an intermediate change of the output to false. Rejection of an invalid input value still sets the result to false without sending a write.
A new output event is delivered only when the value changes. If the output is already true, another successful write does not cause a new event; the same applies to another error when the output is false.
The description visible below the block shows the result of the last completed operation:
- for function
5, it is0or1 - for the remaining functions, it is the textual representation of the last received input value
- in case of an error, the error message is shown below the block
Errors visible to the user
The block can display the following error messages:
| Error code | Error description |
|---|---|
Invalid function code configuration. |
The “Function code” parameter has an invalid value. |
Invalid device address configuration. |
The “Device address” parameter is invalid or outside the 1-255 range. |
Invalid register address configuration. |
The “Register address” parameter is invalid or outside the 0-65535 range. |
Invalid bit count configuration. |
The “Bit count” parameter is invalid, out of range, or required for function 15 but not configured correctly. |
Invalid value type configuration. |
The “Value type” parameter is invalid or required for function 16 but not configured correctly. |
Invalid response timeout configuration. |
The “Response timeout [ms]” parameter is invalid or outside the 10-60000 ms range. |
Invalid multiplier configuration. |
The “Multiplier” parameter is invalid or outside the allowed range. |
Invalid endianess configuration. |
The “Endianess” parameter is required for the selected write mode but has not been configured correctly. |
Missing RS485 configuration. |
No valid modbus-client block was selected in the “Client config” parameter. |
Baudrate configuration mismatch. |
Different Modbus blocks in the project use inconsistent RS485 baudrate settings. |
Parity configuration mismatch. |
Different Modbus blocks in the project use inconsistent RS485 parity settings. |
Stop bits configuration mismatch. |
Different Modbus blocks in the project use inconsistent RS485 stop bit settings. |
Invalid baudrate configuration. |
The RS485 settings provided by the modbus-client block are invalid. |
Invalid parity configuration. |
The RS485 settings provided by the modbus-client block are invalid. |
Invalid stop bits configuration. |
The RS485 settings provided by the modbus-client block are invalid. |
Invalid parity or stop bits configuration. |
The configured combination of parity and stop bits is not allowed. |
Input disconnected. |
The block input is not connected to any data source. |
RS485 port missing. |
The RS485 port is unavailable. Check the connection of the device providing the port. |
Expected 0 or 1 on input. |
For function 5, the input received a value other than 0 or 1. |
Failed to parse value. |
The block could not read the input value as a number. |
Negative value. |
For function 15, a negative value was provided. |
Value too large for bit count. |
For function 15, the converted value does not fit in the number of bits defined by “Bit count”. |
Value exceeds 16-bits. |
The converted value does not fit in 16 bits for function 6 or for the NUMBER16 type. |
Value exceeds 32-bits. |
The converted value does not fit in 32 bits for the NUMBER32 type. |
Value exceeds 32-bit float. |
The converted value does not fit in the range of a 32-bit floating-point number for the FLOAT type. |
Failed to create request data. |
The block could not prepare the data buffer to send. |
Function code not supported |
The Modbus device rejected the request because it does not support the selected function. |
Invalid address |
The Modbus device reported an invalid coil or register address. |
Invalid quantity |
The Modbus device reported an invalid number of items to write. |
IO error |
An input/output error occurred during communication. |
Unknown modbus exception code |
The Modbus device returned an unknown exception code. |
Master/RS485 timeout |
The operation timed out on the master side or on the RS485 bus. |
Slave timeout |
The slave device did not respond within the expected time. |
Invalid request |
The request sent to the device was considered invalid. |
Internal error |
The write could not be prepared or performed. If the message keeps appearing, record its text and contact technical support. |
RS485 busy |
The RS485 bus is currently busy. |
RS485 RX overrun |
An RS485 receive overrun occurred. |
RS485 frame error |
An RS485 frame error occurred during transmission. |
Examples
- With an effective write retry limit of
1, if the first attempt receives no response and the second receives a valid confirmation, the block sets the final result to true. If the output is already true, no new event is sent. - With the default effective limit of
3, if all four attempts receive no response, the block sets the final result to false and displays the error from the last attempt. If the output is already false, no new event is sent. - If “Function code” is set to
5and the value1appears at the input, the block will send a single-coil write that sets the coil to the active state. After successful confirmation, true will appear at the output and the block description will change to1. - If “Function code” is set to
6, “Multiplier” is set to10, and the value12.3appears at the input, the integer value123will be written to the register. - If “Function code” is set to
15, “Bit count” is set to8, and the value13appears at the input, the block will write eight bits representing that value. If the value300appears instead, the block will return theValue too large for bit count.error. - If “Function code” is set to
16, “Value type” is set toFLOAT, “Multiplier” is set to10, and the value21.5appears at the input, the value215.0encoded as a 32-bit floating-point number will be sent to two registers.