Skip to content

modbus-read

Description

The modbus-read block is used to read data from a Modbus RTU device over the RS485 port. It can perform reads cyclically, after an event on the main input, or in both modes at the same time.

The block has one main input and two outputs. It does not require the use of inputs labeled with the tag block.

The supported read method depends on the “Function code” parameter:

  • 1: read coils
  • 2: read discrete inputs
  • 3: read holding registers
  • 4: read input registers

Configuration parameters

Parameter name Value
Name (name) Name displayed on the block in the editor. If empty, modbus-read is displayed.
Input edge (input_edge) Required parameter that defines which event on the main input starts a read. Available values: rising, falling, both, true, false, null.
Function code (functionCode) Required parameter. Defines the read type: 1 for coils, 2 for discrete inputs, 3 for holding registers, 4 for input 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, input, or register in the range 0 to 65535.
Bit count (bitCount) Visible and required when “Function code” is set to 1 or 2; hidden for 3 and 4. Defines the number of bits to read. Allowed range: 1-32.
Value type (valueType) Visible and required when “Function code” is set to 3 or 4; hidden for 1 and 2. Allowed values: SIGNED_NUMBER16, SIGNED_NUMBER32, UNSIGNED_NUMBER16, UNSIGNED_NUMBER32, and FLOAT.
Divisor (divisor) Required parameter. After processing, the read value is divided by this parameter. Allowed range: 1-4294967295. The block does not round the result after division.
Precision (precision) Required parameter in the range 0-3. Defines the number of decimal places after applying “Divisor”. The value 0 removes the entire fractional part, while 1, 2, and 3 retain one, two, or three decimal places respectively. Truncation is towards zero, without rounding, for both bit and register reads.
Endianess (endianess) Byte and word order selection, visible for all functions. Required when the read covers more than 1 byte of data, that is for functions 3 and 4, and for functions 1 and 2 when “Bit count” exceeds 8. Allowed values: LittleEndian, BigEndian, SwapBytes, SwapWords.
Refresh interval [ms] (refreshInterval) Required parameter. Defines the time between consecutive cyclic reads in milliseconds. Allowed range: 0-3600000 ms. The value 0 disables cyclic reading.
Response timeout [ms] (responseTimeout) Required parameter. Maximum time to wait for the device response in milliseconds. Allowed range: 10-60000 ms.
Client config (server) Required selection of a modbus-client configuration providing the RS485 port settings. Edit the port settings in the selected client configuration.

Input handling

The block has one main input. The value appearing at this input is not interpreted as the data to read from Modbus. It is used only to trigger the read operation according to the “Input edge” parameter.

The “Input edge” parameter works as follows:

  • rising: read after a change interpreted as false -> true
  • falling: read after a change interpreted as true -> false
  • both: read on every logical state change
  • true: read after every input value interpreted as true
  • false: read after every input value interpreted as false
  • null: read only after receiving the null value

Additional operating rules:

  • For rising, falling, both, true, and false, the input must be convertible to a logical value. Other values are ignored.
  • If “Refresh interval [ms]” is greater than 0, the block also performs cyclic reads. The first read attempt happens about 2 seconds after project initialization is finished.
  • Each next cyclic read is scheduled only after the previous operation has finished.
  • If the block is already waiting for a response from the Modbus device, further input triggers and further read attempts are ignored until the current operation finishes.

Interpretation of the read value

How the response is interpreted depends on the selected “Function code”.

For functions 1 and 2:

  • the block reads from 1 to 32 bits
  • the received bits are interpreted as one non-negative numeric value
  • the “Endianess” parameter matters only when “Bit count” exceeds 8

For functions 3 and 4:

  • SIGNED_NUMBER16: read one register as a signed 16-bit number
  • SIGNED_NUMBER32: read two registers as a signed 32-bit number
  • UNSIGNED_NUMBER16: read one register as an unsigned 16-bit number
  • UNSIGNED_NUMBER32: read two registers as an unsigned 32-bit number
  • FLOAT: read two registers as a 32-bit floating-point number

After reading and interpreting the data, the block:

  • divides the value by “Divisor”, if “Divisor” is greater than 1
  • truncates the fractional part according to “Precision”
  • sends the result to the output as a numeric value

Block outputs

Output number and name Output description
1. Read value. Returns the last correctly read and processed numeric value from the Modbus device. The output is updated only after a successful read and correct response interpretation.
2. Data connection status. Returns true when the last read finished successfully and the block has a current value available. Returns false when a communication error, response error, or data processing error occurs. In case of an error, the first output keeps the last correctly read value.

Block status description

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

  • after a successful read, it is the textual representation of the read value
  • after an error, it is the error message

Errors visible to the user

The block can display the following error messages:

Error code Error description
Invalid input edge configuration. The “Input edge” parameter is invalid or has not been configured.
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 1 or 2 but not configured correctly.
Invalid value type configuration. The “Value type” parameter is invalid or required for function 3 or 4 but not configured correctly.
Invalid divisor configuration. The “Divisor” parameter is invalid or outside the allowed range.
Invalid precision configuration. The “Precision” parameter is invalid or outside the 0-3 range.
Invalid endianess configuration. The “Endianess” parameter is required for the selected read mode but has not been configured correctly.
Invalid refresh interval configuration. The “Refresh interval [ms]” parameter is invalid or outside the 0-3600000 ms range.
Invalid response timeout configuration. The “Response timeout [ms]” parameter is invalid or outside the 10-60000 ms range.
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.
RS485 setup incomplete. The RS485 configuration is incomplete. Check the selected modbus-client settings.
RS485 port missing. The RS485 port is unavailable. Check the connection of the device providing the port.
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, input, or register address.
Invalid quantity The Modbus device reported an invalid number of items to read.
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 read 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.
Value parse failed The device response was received, but the value could not be read from it correctly.
Unknown register value type The read result could not be processed. If the message keeps appearing, record its text and contact technical support.
Invalid register value type The read result could not be processed. If the message keeps appearing, record its text and contact technical support.
Invalid precision The read result could not be processed. If the message keeps appearing, record its text and contact technical support.

Examples

  • To read the state of one coil, set “Function code” to 1, “Bit count” to 1, “Divisor” to 1, “Precision” to 0, and “Endianess” to LittleEndian. Set the device and coil addresses according to the device manual. After successfully reading an energized coil, output 1 contains the number 1 and output 2 contains true.
  • If a sensor provides temperature in a holding register as an integer in tenths of a degree, set “Function code” to 3, “Value type” to SIGNED_NUMBER16, “Divisor” to 10, and “Precision” to 1. Select the addresses and “Endianess” according to the sensor manual. A read value of 215 produces 21.5 at the output.
  • With the same sensor settings and “Precision” set to 0, a value of 215 produces 21, and -15 produces -1. This setting lets you forward temperatures in whole degrees.
  • With “Function code” set to 4, “Value type” to FLOAT, “Divisor” to 1, “Precision” to 2, and the addresses and “Endianess” selected according to the device manual, a read value of 23.987 produces 23.98 at output 1.
  • If the read ends with a communication error, output 2 will contain false, and the block description will be replaced with the error message.