Sample: Multiple register access

This sample explains the access to the register of aTwinCAT system via Modbus.

The Modbusaddress 0x3000 is mapped by the default-configuration to the memory area of the plc (ADS-Indexgroup 0x4020)

PROGRAM MAIN
VAR
    ipAddr         : STRING(15) := '';
    M0 AT%MB0      : ARRAY [0..3] OF WORD;
    nValue         : ARRAY [0..3] OF WORD :=0,10,100,1000;
    fbWriteRegs    : FB_MBWriteRegs;
    bWriteRegs     : BOOL;
END_VAR
IF NOT bWriteRegs THEN
    nValue[0]:= nValue[0]+1;
    nValue[1]:= nValue[1]+1;
    nValue[2]:= nValue[2]+1;
    nValue[3]:= nValue[3]+1;
    bWriteRegs :=TRUE;
    fbWriteRegs.sIPAddr :=ipAddr;
    fbWriteRegs.nQuantity := 4;
    fbWriteRegs.nMBAddr := 16#3000;
    fbWriteRegs.cbLength := SIZEOF(nValue);
    fbWriteRegs.pSrcAddr := ADR(nValue);
    fbWriteRegs.tTimeout := T#5s;
    fbWriteRegs(bExecute:=TRUE);
ELSE
    IF NOT fbWriteRegs.bBUSY THEN
       bWriteRegs :=FALSE;
    END_IF
    fbWriteRegs(bExecute:=FALSE);
END_IF

The array arrValue will be written to the memory area of the plc (M0) by a rising edge on bWriteRegs.

Download Sample