FB_MBWriteCoils (Modbus function 15)

FB_MBWriteCoils (Modbus function 15) 1:

This function is used to write 1 to 2048 digital outputs (coils). A digital output corresponds to one bit of the written data bytes.

FB_MBWriteCoils (Modbus function 15) 2: Inputs

VAR_INPUT
    sIPAddr       : STRING(15);
    nTCPPort      : UINT:= MODBUS_TCP_PORT;
    nUnitID       : BYTE:=16#FF;
    nQuantity     : WORD;
    nMBAddr       : WORD;
    cbLength      : UDINT;
    pSrcAddr      : POINTER OF BYTE;
    bExecute      : BOOL;
    tTimeout      : TIME;
END_VAR

Name

Type

Description

sIPAddr

STRING (15)

Is a string containing the IP address of the target device.

nTCPPort

UINT

Port number of the target device.

nUnitID

BYTE

Identification number of a serial sub-network device. If a device is addressed directly via TCP/IP, this value must be 16#FF.

nQuantity

WORD

Number of digital outputs to be written (data bits). The value zero is not permitted.

nMBAddr

WORD

Start address of the digital outputs to be written (bit offset).

cbLength

UDINT

Contains the maximum available byte size of the source buffer that contains the data to be written. The buffer must have at least the byte size: (nQuantity + 7) / 8.

pSrcAdd

POINTER OF BYTE

Contains the address of the source buffer that contains the data to be written. The buffer can be a single variable, an array or a structure, whose address can be determined with the ADR operator.

bExecute

BOOL

The function block is enabled by a rising edge at this input.

tTimeout

TIME

States the length of the timeout that may not be exceeded by execution of the ADS command.

FB_MBWriteCoils (Modbus function 15) 3: Outputs

VAR_OUTPUT
    bBUSY      : BOOL;
    bError     : BOOL;
    nErrId     : UDINT;
END_VAR

Name

Type

Description

bBusy

BOOL

When the function block is enabled, this output is set and remains set until a feedback is received.

bError

BOOL

If an ADS error should occur during the transfer of the command, then this output is set once the bBusy output is reset.

nErrId

UDINT

Returns the ADS error number when the bError output is set.

Sample of calling the function block in FBD

PROGRAM Test
VAR
    fbWriteCoils        : FB_MBWriteCoils;
    bWriteCoils         : BOOL;
    bWriteCoilsBusy     : BOOL;
    bWriteCoilsError    : BOOL;
    nWriteCoilsErrorId. : UDINT;
    nWriteCoilsCount    : UDINT;
    nQuantity           : WORD := 10;
    nMBAddr             : WORD := 14;
    arrData             : ARRAY [1..2] OF BYTE := 16#75,16#03;
END_VAR
FB_MBWriteCoils (Modbus function 15) 4:

After a rising edge of "bExecute" and successful execution of the ReadCoils command, the content of the arrData array is written to outputs 15 - 24:

Bit

0

1

1

1

0

1

0

1

0

0

0

0

0

0

1

1

Output

22

21

20

19

18

17

16

15

X

X

X

X

X

X

24

23

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v3.0.0

PC or CX (x86)

Tc2_ModbusSrv