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

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.

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
Overview 1:

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
Overview 2:

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
Overview 3:

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