Example: Using the OPC UA PubSub SL Library
Product:CODESYS OPC UA PubSub SL
The OPC UA PubSub SL Example.project
example shows how to use the OPC UA PubSub SL library to exchange messages via the Pub/Sub protocol as defined by OPC UA Foundation.
The package also contains a second example: PubSubDataSet.project
. It shows how to use the OPC UA PubSub SL library in order to provide different data sets and connect them to GVL instances.
Description
In addition to Client/Server communication, communication via the OPC UA Pub/Sub protocol also provides an additional capability to exchange data between the subscribers of a network in compliance with the rules of the OPC UA Foundation.
The structure of the data (DataSet
) can be freely defined and is agreed in advance between the sender and the receiver. This eliminates the need to transport additional (meta) data. Data is transferred with the help of the OPC UA PubSub SL library via UDP/IP according to the rules as defined for UADP.
A Publisher publishes its data to an unknown number of Subscribers. As a result, the sender and receiver do not know each other. That is why the number of receivers also does not have any repercussions for the sender. A reply to the sender as to whether or not its messages have reached the receivers cannot be sent via the protocol due to the nature of the protocol. If necessary, such a reply has to be managed in an application-specific way.
The data is transferred in a binary format according to the rules of the OPC UA Foundation. The OPC UA PubSub SL
library handles the conversion of IEC data types into the corresponding OPC UA data types and back.
The implementation follows the following profiles:
Publisher: PubSub Publisher UADP Periodic Fixed Settings
Subscriber: PubSub Subscriber UADP Periodic Fixed Settings
The message length is limited to 1,500 bytes (MTU) (Chunked NetworkMessages are not supported). As long as the rules for a Time Sensitive Network is still not available, hard real-time conditions cannot be complied with. However, the implementation of the OPC UA PubSub SL
library attempts to keep the jitter as low as possible.
The library includes the following function blocks:
DataSet
:Defines a
DataSet
messageConfiguration
:Manages shared resources, such as Connection or Group
RootDiagnostics
:Notifies with superordinate diagnostic data
Connection
:Manages the connection to Publisher and Subscriber
ConnectionDiagnostics
:Notifies with diagnostic data about the connection (
Connection
)ReaderGroup
:Manages
Reader
function blocks (creation of a NetworkMessage fromDataSet
messages)ReaderGroupDiagnostics
:Notifies with diagnostic data about the
ReaderGroup
Reader
:Manages the
DataSet
function block (Subscriber)ReaderDiagnostics
:Notifies with diagnostic data about a
Reader
function blockWriterGroup
:Manages
Writer
function blocks (creation of a NetworkMessage fromDataSet
messages)WriterGroupDiagnostics
:Notifies with diagnostic data about the
WriterGroup
Writer
:Manages a
DataSet
function block (Publisher)WriterDiagnostics
:Notifies with diagnostic data about a
Writer
function block
![]() |
Supported functionality
Publish/Subscribe of messages according to OPC 10000-14: OPC Unified Architecture Part 14: PubSub Release 1.04
Maximum size of a NetworkMessage: 1,500 bytes
The current packet size and payload can be configured by means of the configuration of the respective ReaderGroup/WriterGroup and
DataSet
function blocks.Support with a background task for the respective
Connection
function blockThe following profiles are supported:
Publisher: PubSub Publisher UADP Periodic Fixed Settings
Subscriber: PubSub Subscriber UADP Periodic Fixed Settings
Additional information
The Device_1
application shows how to read a NetworkMessage
.
The Device_2
application shows how to send messages by means of the contained function blocks.
Hardware
You need two CODESYS PLCs which are connected over a network which supports UDP Multicast.
In the project, adapt the device configuration to the devices by updating
Device_1
andDevice_2
to the hardware used.
Customizations in the project
Set the IP addresses in the
GlobalIP
GVL to the hardware which you have configured. You will find this GVL in the POUs area.Set the Multicast address to a free address in your network.
Note: If you are not in a private network, request that your network administrator assign a free address for you.
Set the port. The default port 4840 is the recommended port for OPC UA PubSub.
Log in to both controllers and start the applications.
Start both the sender and the receiver:
Device_2: PLC_PRG.xEnable := TRUE
Device_1: Communication_PRG.xEnable := TRUE
On the receiver side, you should now see how the values of the sine change. You can manually change the other values in the sender and the see the changes in the receiver.
Add a variable to the DataSet
which has already been sent.
Extend
SensorDataSet._aIndex
by one entry. Select any data type.In
SensorDataSet.Init
, adapt the version ofDataSet
by updating the date entry.In the
PLC_PRG
program of the sender, create a variable of the selected data type. Assign the valuetxSensorDataSet.PrepareValues
to the variable.In
rxSensorDataSet.PrepareValues
, repeat this step on the receiver side. Create a new variable of the same type beforehand here as well.Perform a new download.
The new variable should be transmitted as well.
![]() |
System requirements and restrictions
Programming system | CODESYS Development System (version 3.5.17.0 or higher) |
Runtime system | CODESYS Control Win (version 3.5.14.0) NoteDepending on the capabilities of the respective runtime system, the messages can be sent via Unicast, Multicast, or Broadcast. |
Supported platforms/devices | TipUse the |
Restrictions |
|
Add-on components | CODESYS OPC UA PubSub SL |
Note
DOWNLOAD Project