MODBUS TCP/IP is a communication implementation under the MODBUS family. The protocol makes use of MODBUS messaging in an Internet like type of environment using the TCP/IP protocols. The implementation below is a Modbus TCP communication using the Ethernet Frame.
Introduction
Install the Modbus Library in the codesys or e!Cockpit Library Environment

We use the FbMbMasterTCP function to implement the Modbus TCP on the Master Device

Declare the function input and output variables and create the Modbus Object
PROGRAM Modbus
VAR
// INPUTS
ModbusTCPMaster : FbMbMasterTCP;
xConnect : BOOL := TRUE;
Host : STRING := '10.1.3.8'; // IP OF the remote server
Port : WORD := 2000; // port at the remote server
KeepAlive : typKeepAlive ;
FrameType : eMbFrameType:= eMbFrameType.ETHERNET;
TimeOut : TIME := T#1S;
Query : typMbQuery;
xTrigger : BOOL; (* Set this variable once for start a job.
This variable will be automaticly reset by the master
if the job is done.
*)
Response : typMbResponse; (* After the job is done you can find at this structure
the result.
*)
// OUTPUTS
Open : BOOL;
xError : BOOL;
Status : FbResult;
// supporting variables
Blink : FbBlinker;
ConfigBlinker: typConfigBlinker;
END_VAR
Object definition
// KEEP ALIVE
KeepAlive.xEnable:= TRUE; // use keep alive
KeepAlive.tMaxIdleTime := T#5S; // Maximum time of inactivity
KeepAlive.tInterval := T#2S; // Interval between two successive KA-Packets
KeepAlive.udiProbes := 5; // Number of KA retry before giving up
// QUERY
Query.bUnitId := 1; // Slave address
Query.bFunctionCode := 16#03; // read holding registers
Query.uiReadAddress := 0; // Start address
Query.uiReadQuantity := 10; // Quantity of wanted registers
Query.uiWriteAddress := 0;
Query.uiWriteQuantity := 0;
// blinker
ConfigBlinker.tTimeHigh := T#1S;
ConfigBlinker.tTimeLow := T#3S;
Blink(xEnable := TRUE, typConfigParameters :=ConfigBlinker, xOutput => xTrigger);
// define modbus object
ModbusTCPMaster(
xConnect := xConnect,
sHost := Host, // IP of the remote server
wPort := Port, // port at the remote server
utKeepAlive := KeepAlive,
eFrameType := FrameType,
tTimeOut := TimeOut,
utQuery := Query,
xTrigger := xTrigger,
utResponse := Response,
xIsOpen => Open,
xError => xError,
oStatus => Status
);
e!Cockpit