UDP
This quick start tutorial demonstrates how to set up a PLC project, OPC UA RT device and a publisher that sends a data set containing one variable via UDP. The variable is linked to a variable from the PLC project via the process image.
Preparing the PLC project
Prepare a PLC project that defines one output variable with data type INT, e.g.:
PROGRAM MAIN
VAR
nCounter AT%Q* : INT;
END_VAR
nCounter := nCounter + 1;
Compile the PLC project so that the PLC process image is created.
Configuring a publisher
Please perform the following configuration steps to set up an OPC UA Publisher device:
- 1. Make sure that TwinCAT is in config mode.
- 2. In Solution Explorer, expand the I/O node and select the child item Devices.
- 3. Right-click Devices and select Add new element...
- 4. Expand the category OPC UA, select the Real-Time OPC UA Device and click on Ok.
- 5. Double-click the added device and navigate to the tab Adapter.
- 6. Bind the device to one of your Network Interface Cards. Make sure that you have installed the TwinCAT-Intel PCI Ethernet Adapter driver on that NIC.
- 7. Right-click the device and select Add new element...
- 8. Select the entry OPC UA Publisher (Module) and click on Ok.
- 9. To configure the added Publisher node, simply double click it in the tree view. There are many configuration options on the Publisher, e.g. the transport to use (UDP, MQTT), the data format (binary, JSON), optional header fields and so on. We want to leave these settings on their default values for the time being. Only make sure that the transport UDP/UADP is selected.
- 10. In the next step you want to configure a so-called Dataset in order to define variables that should be send out by the Publisher. Right-click the Publisher node and select Add new element....
- 11. Select the entry Published DataSet with Variables (Module) and click on Ok.
- 12. On the added data set, right-click the Outputs node and select Add new element...
- 13. Set a name for the process variable, e.g. Counter and select INT as the Datatype. Click on Ok to add the variable to the process image of the Dataset.
- Your configuration should now look as follows:
- 14. Right-click the added variable and select Change link.... Select the PLC variable that you have created earlier and click on Ok.
Activate the project
Activate the TwinCAT project by clicking the Activate button on the TwinCAT XAE toolbar. Make sure that you have selected the correct target device (we assume that you are using your local device) and that you are using the correct Real-Time settings so that you can activate the TwinCAT Runtime on your device. Please consult the regular TwinCAT documentation for more information about how to activate a TwinCAT configuration and switch TwinCAT into Run mode.
In this quick start tutorial, we have used UDP as the transport protocol. You can now connect either an OPC UA Pub/Sub subscriber to the data or also have a look at the data using Wireshark. If you want to connect a subscriber, we highly recommend to make use of the Configuration import/export.