Multiple register access

his sample explains the access to the register of a TwinCAT system via Modbus.

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

After calling bWriteRegs, the array arrValue is written in the flag area and thus in the variable M0.

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

Download Sample