Skip to main content

MB_WriteBinary

MB_WriteBinary writes binary values to a Modbus RTU slave using Write Single Coil or Write Multiple Coils.

Ladder Representation

MB_WriteBinary ladder representation

Block Structure

Variable TypeNameData TypeDescription
VAR_INPUTExecuteBOOLEnables block execution.
VAR_INPUTSlaveAddressBYTEModbus RTU slave address.
VAR_INPUTFunction#BYTEFunction code used for the write request.
VAR_INPUTInitialDataAddressWORDInitial bit address where the data will be written.
VAR_INPUTNumberOfDataBYTENumber of bits to write (1 to 128).
VAR_INPUTTimeout#WORDMaximum time to wait for the response, in milliseconds.
VAR_INPUTOffset#BOOLIndicates whether the block must subtract 1 from InitialDataAddress.
VAR_INPUTValueBOOLVariable that stores the binary data to write.
VAR_OUTPUTDoneBOOLIndicates that the block completed successfully.
VAR_OUTPUTActiveBOOLIndicates that the block is waiting for a response.
VAR_OUTPUTBusyBOOLIndicates that the RS485 interface is busy with another request.
VAR_OUTPUTErrorBOOLIndicates an execution error.
VAR_OUTPUTErrorIDBYTEExecution error identifier.
VARMB_WRITEBINARYMB_WRITEBINARYBlock structure access instance.

Operation

On a rising edge of Execute, the block checks whether the Modbus RTU slave at SlaveAddress is free for a request. If Busy is FALSE, it writes the values from Value to the bit range starting at InitialDataAddress, using NumberOfData and the function selected in Function#.

The block sets Active while it waits for the slave response and resets Active when the response is received. If the slave is busy, the block waits until Busy becomes FALSE before sending the request.

When Execute is FALSE, Done remains FALSE. Done is TRUE only after the block completes successfully and remains TRUE until Execute becomes FALSE.

Request Cancellation

If Execute becomes FALSE while Busy is still TRUE, the request is canceled.

Value Size

Value must be an array with the same size as NumberOfData. Check this compatibility to avoid block errors.

Error Codes

CodeDescription
0Executed successfully.
1Invalid input data.
2Master not enabled.
4Response timeout from the slave.
5Slave returned an error.

Execution Flowchart

MB_WriteBinary execution flowchart

Examples

MB_WriteBinary ladder example

This example writes the data contained in VALUE, with size DATA_COUNT, starting at INIT on the Modbus RTU slave address SLAVE using the Write Single Coil function. When the block finishes successfully, Done is activated.