Beckhoff Twincat 2

Hello,

All you need is Modbus lib (Tc2_ModbusSrv) in your project.

Here is the configuration of my FactorIO which is my ModBus server.

Quick and dirty:

Declaration

PROGRAM F_004_FactoryIO_Modbus_ReadInputs
VAR
LAB_Data : ARRAY [0…15] OF BYTE; // 16 bytes = 128 bits
LFB_MBReadInputs : FB_MBReadInputs;
LD_cbRead : udint;
END_VAR

Programm:

LFB_MBReadInputs(
sIPAddr := 127.0.0.1,
nTCPPort := 505,
nUnitID := 255,
nQuantity := 16,
nMBAddr := 0,
cbLength := SIZEOF(LAB_Data),
pDestAddr := ADR(LAB_Data),
bExecute := true,
tTimeout := T#1S,
bBusy => ,
bError => ,
nErrId => ,
cbRead => ,
);
LFB_MBReadInputs(bExecute := false);
LFB_MBReadInputs(bExecute := true);

Your inputs are now available in the var “LAB_Data”.

Reading input reg you need the FB “FB_MBReadInputRegs”.
Writing calls it is “FB_MBWriteCoils” and for writing register its called “FB_MBWriteRegs”.