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)

After you call bWriteRegs, the array arrValue is written to the flag area and thus to the variable M0.

PROGRAM MAIN
VAR
  ipAddr      : STRING(15) := '';
  M0 AT%MB0   : ARRAY [0..3] OF WORD;
  arrValue    : ARRAY [0..3] OF WORD;
  fbWriteRegs : FB_MBWriteRegs;
  bWriteRegs  : BOOL;
END_VAR
IF NOT bWriteRegs THEN
  arrValue[0]:= arrValue[0]+1;
  arrValue[1]:= arrValue[1]+1;
  arrValue[2]:= arrValue[2]+1;
  arrValue[3]:= arrValue[3]+1;

  bWriteRegs :=TRUE;

  fbWriteRegs.sIPAddr := ipAddr;
  fbWriteRegs.nQuantity := 4;
  fbWriteRegs.nMBAddr := 16#3000;
  fbWriteRegs.cbLength := SIZEOF(arrValue);
  fbWriteRegs.pSrcAddr := ADR(arrValue);
  fbWriteRegs.tTimeout := T#5s;
  fbWriteRegs(bExecute:=TRUE);

ELSIF NOT fbWriteRegs.bBUSY THEN
  bWriteRegs :=FALSE;
END_IF
  fbWriteRegs(bExecute:=FALSE);

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

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v3.0.0

PC or CX (x86)

Tc2_ModbusSrv