Block that performs a reading of up to 128 binary data (via Read Coils or Read Discrete Inputs) of a slave on the Modbus RTU network.
Ladder Representation

Block Structure
Variable Type |
Name |
Data Type |
Description |
VAR_INPUT |
Execute |
BOOL |
Block enabling |
SlaveAddress |
BYTE |
Slave address |
|
Function# |
BYTE |
Reading function code |
|
InitialDataAddress |
WORD |
Initial bit address of the data to be read |
|
NumberOfData |
BYTE |
Number of bits to be read (1 to 128) |
|
Timeout# |
WORD |
Maximum waiting time for the slave response [ms] |
|
Offset# |
BOOL |
Offset Indication in InitialDataAddress, i.e., need to subtract 1 from this number |
|
VAR_OUTPUT |
Done |
BOOL |
Output enabling |
Active |
BOOL |
Awaiting response flag |
|
Busy |
BOOL |
Flag indicating the RS485 interface is busy with another request |
|
Error |
BOOL |
Error in the execution flag |
|
ErrorID |
BYTE |
Identifier of the occurred error |
|
Value |
BOOL |
Variable that stores the received data |
|
VAR |
MB_READBINARY_INST_0 |
MB_READBINARY |
Instance of access to block structure |
Operation
When this block detects a leading edge on Execute, it checks whether the Modbus slave RTU in specified address in SlaveAddress is free to send data (Busy variable at FALSE level). If so, it sends the reading request of a number of bits indicated by NumberOfData in InitialDataAddress address using chosen function in Function# and sets the Active output, resetting it when receiving the response from the slave. The received data is stored in the Value variable. If the slave is not free, the block waits Busy go to FALSE level to resubmit the request.
|
NOTE! If Execute goes to FALSE level and Busy is still at TRUE level, the request is canceled. |
|
NOTE! Value is an array of size equal to NumberOfData. It is important to check this compatibility not to generate errors in the block. |
When Execute has FALSE value, Done remains FALSE. The Done output is only activated when the block finishes executing successfully, remaining at TRUE level until Execute receives FALSE.
If there is any error in the execution, the Error output is enabled and ErrorID displays an error code according to the table below.
Code |
Description |
0 |
Executed successfully |
1 |
Invalid input data |
2 |
Master not enabled |
4 |
Timeout in slave response |
5 |
Slave returned error |
Block Flowchart

Example in Ladder

The above example requests reading of a number of binary data described by DATA_COUNT positioned in the INIT Modbus RTU slave of SLAVE address through the Read Discrete Input function. These data are forwarded to VALUE. The block ends successfully, Done output is activated.
Example in ST
The example below displays the instructions for applying the example above in the ST language..
VAR SLAVE : BYTE := 1; INIT : WORD; VALUE : ARRAY [0..4] OF BOOL; MB_READBINARY_INST_0 : FB_MB_ReadBinary; END_VAR
MB_READBINARY_INST_0.Execute := DI1; MB_READBINARY_INST_0( SlaveAddress:=SLAVE, Function:=2, InitialDataAddress:=INIT, NumberOfData:=4, Timeout:=30, Offset:=0, Value=>VALUE); DO1 := MB_READBINARY_INST_0.Done;
|
|---|