OPC UA Server and Client Setup Guides
OPC UA Server and Client Setup Guides
Introduction
This tutorial describes how easy it is to create an OPC UA server in CDP Studio, the independent automation software for open PC-based real-time distributed control systems to publish and subscribe some variables. This tutorial also describes how easy it is to create and use a preconfigured OPC UA client using the OPC UA discovery feature.
OPC UA (Open Platform Communications Unified Architecture) is a machine to machine communication protocol for industrial automation developed by the OPC Foundation.
Project Overview
The tutorial is step-by-step guide how to create two applications:
- OPC UA server application that serves CDP sine component output and allows to set amplitude parameter via OPC UA.
- OPC UA client application that connects to the server and receives the Output value and also allows to set a new sine amplitude in server.
Creating the OPC UA Sine Server Application
Follow these steps to add a sine component:
- Create a new CDP Studio system and name it SineServer. See How to Create a System for instructions.
- Click on the default application that was created (called SineServerApp )
- Locate the Sine in the Resource tree
- Add a Sine component by dragging it onto the application
- Locate the OPC-UA-Server in the Resource tree
- Add an OPC-UA-Server component by dragging it onto the application
Configure OPC UA Object and Variable Nodes
Now you have to add OPC UA nodes to serve the sine value and amplitude variables.
First create an OPC UA object node called SineObject:
- Click on the OPC-UA-Server component
- Locate the ServerObjectNode in the Resource tree
- Start adding the ServerObjectNode to the server by selecting Add Multiple... from the context menu
- In the dialog, set the base name for the object to be created to SineObject and click OK
Add the SineValue variable to SineObject
- Locate the ServerVariableNode<double> in the Resource tree
- Add ServerVariableNode<double> by dragging it onto the SineObject
- Click on the pin of the added variable to select it and to view its properties on sidebar
- Rename the variable to SineValue
- Make the variable value routable from CDP by setting it as Input variable
Add the Amplitude variable to SineObject
- Add another ServerVariableNode<double> by dragging it onto the SineObject
- Click on the pin of the added variable to select it and to view its properties on sidebar
- Rename the variable to Amplitude
- Set a initial value for the Amplitude (for example 10)
Note: The Amplitude variable will be writable by OPC UA clients and its value can be used in CDP as source so it must be output for CDP (as default).
Route the Sine component values to the OPC UA SineObject by following the steps below:
- Click on the SineServerApp
- To be enable showing of Sine Amplitude parameter uncheck Hide Parameters filter
- Right-click and Pin Sine component, to be always shown despite of the selected node in project tree
Now connect (route) the Sine Output and Amlitude to the OPC-UA-Server:
- Click on the OPC-UA-Server component
- Click and drag from the Sine Output to the SineObject SineValue
- Click and drag from the SineObject Amplitude to the Sine Amplitude
- Corresponding routing lines will appear
Creating the OPC UA Client Application for the Sine Server Using Discovery
Now, that the OPC UA server is created, you can move on to create a corresponding OPC UA client. It can be configured manually, similarly to how the the server was configured. But, it can be configured even simpler (and possible configuration errors can be avoided) with OPC UA Server Discovery.
- To be able to use discovery you first have to run the server application by right-clicking on the SineServerApp and selecting Run from the context menu.
- The running icon will appear next to the application to indicate the application is running
- Launch the OPC UA discoverer by clicking on options menu next to resource search, and select Discover OPC UA Resources... in the menu
- In the popup dialog, enter 127.0.0.1 into OPC UA Server Address and click OK
- The discovery process is started. Result messages can be observed in General Messages pane.
Discovery result will appear and can be inspected in the Resource tree under section Discovered Resources (OPC UA). You can now use the discovery result to instantly create a 100% correct and matching OPC UA for the server.
First, create a new application called SineClientApp:
- From the File menu, select Create New...
- In the Wizard that pops up, select CDP Application, then click on Choose...
- Name the application SineClientApp, then click on Next >
- Click on Finish
Second, apply the discovered recipe into the application:
- Click on the application created called SineClientApp
- Locate the discovered OPC-UA-Server recipe under Discovered Resources (OPC UA) in the Resource tree.
- Add the recipe to the application by selecting Add Multiple... from the context menu
- In the dialog that appears, change the base name for the object to OPC-UA-Client (do not leave it OPC-UA-Server as it is actually a client) and click OK
- The OPC-UA-Client component with corresponding nodes is created and added to the SineClientApp
Although discovery can find all nodes from server it can not determine whether the variable nodes found are needed in your application to be read from (receive values) or to be writable (send values). This must therefore be adjusted manually, after adding discovered nodes.
In this tutorial project we need Amplitude to be set as a writable variable:
- Click on the OPC-UA-Client component
- Click on the Amplitude pin on SineObject to select it
- In the sidebar, check the variable to be Input
- Set also some initial amplitude for the sine, eg. 10
Run and Test the System
You can now run also the client application (your server is already started and running) and connect to the system to inspect it:
- Right-click on the SineClientApp and select Run from the context menu
- Right-click on the SineServer system and select Connect from the context menu
- Once connected, locate and click on the OPC-UA-Client component to select it
- Then click on the SineObject to select it
- You can now see the SineValue values are continuously received via OPC UA by inspecting it in sidebar
- You can also change the Amplitude value and verify that sine generation in server is changing accordingly by inspecting the SineValue changes
Note: Note, that this system updates sine output values from OPC UA server to client after every 200 ms. This is according to the server and client default sample rates that default to 5Hz. You have to increase OPC-UA-Server Fs property accordingly if you want the server to send values more often. You also have to increase OPC-UA-Client Fs property accordingly if you want to process and propagate received values more often.
Troubleshooting
What to do when discoverer fails to connect to the server?
As a general rule for any IOServer, first troubleshooting action should be to increase the debug level of the server. Debug level can be increased by clicking on OPC-UA-Server component, locating Debug property and setting it to non-zero value (value 9 or higher can be used to see OPC UA framework internal messages).
What to do when server prints debug lines about errors while binding a socket?
When OPC-UA-Server (with Debug=9 or higher) displays warning lines like
WARN: NETWORK: OPC-UA-Server: Error binding a server socket: An attempt was made to access a socket in a way forbidden by its access permissions.
or
WARN: NETWORK: OPC-UA-Server: Error binding a server socket: Address already in use.
then it usually means, that the TCP port configured for the server is in use by some other service in the system. You should change the EndpointPort property of the OPC-UA-Server to some other port and rerun the application.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.