Overview
The following example demonstrates the implementation of a simple Peer-to-Peer application in the PLC and consists of two PLC projects (PeerA and PeerB) plus a .NET application which also acts as a separate peer. All peer applications send a test string to a remote peer and at the same time receive strings from a remote peer. The received strings are displayed in a message box on the monitor of the target computer. Feel free to use and customize this sample to your needs.
System requirements
- TwinCAT 3 Build 3093 or higher
- TwinCAT 3 Function TF6310 TCP/IP
- If two computers are used to execute the sample, the Function TF6310 needs to be installed on both computers
- If one computer is used to execute the sample, e.g. Peer A und Peer B running in two separate PLC runtimes, both PLC runtimes need to run in separate tasks
- To run the .NET sample client, only .NET Framework 4.0 is needed
Project downloads
The sources of the two PLC devices only differ in terms of different IP addresses of the remote communication partners.
https://github.com/Beckhoff/TF6310_Samples/tree/master/PLC/UDP/Sample01
https://github.com/Beckhoff/TF6310_Samples/tree/master/C%23/SampleClientUdp
Project description
The following links provide documentation for each component. Additionally, an own article explains how to start the PLC samples with step-by-step instructions.
- Integration in TwinCAT and Test (Starting the PLC samples)
- PLC devices A and B (Peer-to-Peer PLC application)
- .NET communication (.NET sample client)
Additional functions of the PLC sample projects
Some functions, constants and function blocks are used in the PLC samples, which are briefly described below:
Fifo function block
FUNCTION_BLOCK FB_Fifo
VAR_INPUT
new : ST_FifoEntry;
END_VAR
VAR_OUTPUT
bOk : BOOL;
old : ST_FifoEntry;
END_VAR
This is a simple Fifo function block. One instance of this function block is used as "send Fifo", another one as "receive Fifo". The messages to be sent are stored in the send Fifo, the received messages are stored in the receive Fifo. The bOk output variable is set to FALSE if errors occurred during the last action (AddTail or RemoveHead) (Fifo empty or overfilled).
A Fifo entry consists of the following components:
TYPE ST_FifoEntry :
STRUCT
sRemoteHost : STRING(15); (* Remote address. String containing an (Ipv4) Internet Protocol dotted address. *)
nRemotePort : UDINT; (* Remote Internet Protocol (IP) port. *)
msg : STRING; (* Udp packet data *)
END_STRUCT
END_TYPE
LogError function
FUNCTION LogError : DINT

The function writes a message with the error code into the logbook of the operating system (Event Viewer). The global variable bLogDebugMessages must first be set to TRUE.
LogMessage function
FUNCTION LogMessage : DINT

The function writes a message into the logbook of the operating system (Event Viewer) if a new socket was opened or closed. The global variable bLogDebugMessages must first be set to TRUE.
SCODE_CODE function
FUNCTION SCODE_CODE : DWORD

The function masks the least significant 16 bits of a Win32 error code returns them.