Sample: Multiple register access
This example illustrates how you can access the real-time environment of a TwinCAT system via Modbus.
With standard mapping of the Modbus server, the Modbus address 0x3000 points to the PLC's flag area (ADS index group 0x4020).
Calling up bWriteRegs therefore writes the arrValue array into the flag area and therefore into 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 is written to the PLC's flag area (M0) following a rising edge on bWriteRegs.
Requirements
Development environment | Target platform | PLC libraries to include |
|---|---|---|
TwinCAT v3.1.0 | IPC or CX (x86, x64, ARM®) | Tc2_ModbusSrv |