Skip to content

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 functionCode parameter:

  • 5: write a single coil
  • 6: write a single register
  • 15: write multiple coils
  • 16: write multiple registers

Configuration parameters

Parameter name Value
Modbus 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.
Starting address (registerAddress) Required parameter. Address of the first coil or first register in the range 0 to 65535.
Bit count (bitCount) Required only for function 15. Defines the number of bits to write. Allowed range: 1-32.
Register value type (valueType) Required only for function 16. Allowed values: NUMBER16, NUMBER32, and FLOAT.
Response timeout (responseTimeout) Required parameter. Maximum time to wait for the device response in milliseconds. Allowed range: 10-60000 ms.
Multiplier (multiplier) Required parameter. The input value is multiplied by this parameter before being written. Allowed range: 1-4294967295.
Byte order (endianess) Required when the written value uses more than 1 byte, that is for functions 6, 16, and for function 15 when bitCount > 8. Allowed values: LittleEndian, BigEndian, SwapBytes, SwapWords.
RS485 configuration (server) Required parameter. Identifier of the modbus-client block from which the RS485 port settings are taken.

Input handling

The block has one main input. It is enough for this input to be connected to at least one data source. Each received change starts a separate attempt to write to the Modbus device.

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 the 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 the multiplier, the fractional part is discarded and the result must fit in the number of bits defined by bitCount
  • for function 16, the input must be a numeric value; the write method depends on valueType

Detailed behavior for function 16:

  • NUMBER16: after applying the multiplier, the fractional part is discarded and the result is written as a 16-bit register value
  • NUMBER32: after applying the multiplier, the fractional part is discarded and the result is written as a 32-bit register value
  • FLOAT: after applying the 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 bitCount <= 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 write error, communication error, or request rejection by the device

The description visible below the block shows the result of the last completed operation:

  • for function 5, it is 0 or 1
  • 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 functionCode parameter has an invalid value.
Invalid device address configuration. The deviceAddress parameter is invalid or outside the 1-255 range.
Invalid register address configuration. The registerAddress parameter is invalid or outside the 0-65535 range.
Invalid bit count configuration. The bitCount parameter is invalid, out of range, or required for function 15 but not configured correctly.
Invalid value type configuration. The valueType parameter is invalid or required for function 16 but not configured correctly.
Invalid response timeout configuration. The responseTimeout 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 server 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. No RS485 port is available in the runtime to execute the operation.
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 bitCount.
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 An internal error occurred while preparing or executing the operation.
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

  • If functionCode = 5 and the value 1 appears 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 to 1.
  • If functionCode = 6, multiplier = 10, and the value 12.3 appears at the input, the integer value 123 will be written to the register.
  • If functionCode = 15, bitCount = 8, and the value 13 appears at the input, the block will write eight bits representing that value. If the value 300 appears instead, the block will return the Value too large for bit count. error.
  • If functionCode = 16, valueType = FLOAT, multiplier = 10, and the value 21.5 appears at the input, the value 215.0 encoded as a 32-bit floating-point number will be sent to two registers.