FB_MBReadWriteRegs (Modbus function 23)

FB_MBReadWriteRegs (Modbus function 23) 1:

This function first reads 1 to 128 output registers (16 bit) and then writes 1 to 128 output registers (16 bit).

VAR_INPUT

VAR_INPUT
    sIPAddr         : STRING(15);
    nTCPPort        : UINT:= MODBUS_TCP_PORT;
    nUnitID         : BYTE:=16#FF;
    nReadQuantity   : WORD;
    nMBReadAddr     : WORD;
    nWriteQuantity  : WORD;
    nMBWriteAddr    : WORD;
    cbDestLength    : UDINT;
    pDestAddr       : UDINT;
    cbSrcLength     : UDINT;
    pSrcAddr        : UDINT;
    bExecute        : BOOL;
    tTimeout        : TIME;
END_VAR

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

nTCPPort : Port number of the target device.

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

nReadQuantity : Number of output registers (data words) to be read. The value of nReadQuantity must be > 0.

nMBReadAddr : Start address of the output registers to be read (word offset).

nWriteQuantity : Number of output registers (data words) to be written. The value of nWriteQuantity must be > 0.

nMBWriteAddr : Start address of the output registers to be written (word offset).

cbDestLength : Contains the max. byte size of the destination buffer. The minimum destination buffer byte size must be nReadQuantity * 2.

pDestAddr : Contains the address of the destination buffer into which the data are to be read. The buffer can be a single variable, an array or a structure, whose address can be found with the ADR operator.

cbSrcLength : Contains the max. byte size of the source buffer. The minimum source buffer byte size must be nWriteQuantity * 2.

pSrcAddr : Contains the address of the source buffer containing the data to be written. The buffer can be a single variable, an array or a structure, whose address can be found with the ADR operator.

bExecute: The function block is activated by a rising edge at this input.

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

VAR_OUTPUT

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

bBusy : When the function block is activated this output is set. It remains set until an acknowledgement is received.

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

nErrId : Supplies the ADS error number when the bError output is set.

cbRead: Contains the number of bytes currently read.

Function specific ADS error code

Possible reason

0x8001

Modbus function not implemented

0x8002

Invalid address or length

0x8003

Invalid parameters:

- wrong number of registers

0x8004

Modbus server error

Example of calling the block in FBD:

PROGRAM Test
VAR
    fbReadWriteRegs         : FB_MBReadWriteRegs;
    bReadWriteRegs          : BOOL;
    bReadWriteRegsBusy      : BOOL;
    bReadWriteRegsError     : BOOL;
    nReadWriteRegsErrorId   : UDINT;
    nReadWriteRegsCount     : UDINT;
    nRdQuantity             : WORD;
    nRdMBAddr               : WORD;
    nWrQuantity             : WORD;
    nWrMBAddr               : WORD;
    arrRdData               : ARRAY [1..9] OF WORD;
    arrWrData               : ARRAY [1..9] OF WORD;
END_VAR

FB_MBReadWriteRegs (Modbus function 23) 2:

After a rising edge of "bExecute" and successful execution of the ReadWriteRegs command, arrRdData contains the read register data, and the data from arrWrData are written to the registers.

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.8.0

PC (i386)

TcModbusSrv.Lib