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 to 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 a step-by-step guide on 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 the 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 view its properties on the sidebar
- Rename the variable to SineValue
- Make the variable controllable-by-server by setting it as Input property. Note that this Input checked and Bidirectional unchecked will make the variable AccessLevel automatically to be read-only i.e. its W (write) bit will be removed on startup.
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 view its properties on the sidebar
- Rename the variable to Amplitude
- Set a initial value for the Amplitude (for example 10)
- Make the variable despite being controllable-by-clients (as its Input is by default set to 0) to propagate its current value to clients by setting it Bidirectional.
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 the selected node in the Project tree
Now connect (route) the Sine Output and Amplitude 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 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
- Check Save variable discovery-time values to ease the configuration and get the startup value configured into the client-controllable Amplitude variable.
- Now start the discovery process by clicking OK.
- The discovery process result messages can be observed by activating the General Messages pane.
Discovery results 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 OPC UA client recipe to 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
The discoverer sets all the discovered variable control directions according to their access level in the server. In our case, the discoverer sets the Amplitude variable as Input and SineValue as Output in the OPC UA Client. The discoverer also sets the initial value of the Amplitude variable to the value that was set in the server (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 the sidebar
- You can also change the Amplitude value and verify that sine generation in the server is changing accordingly by inspecting the SineValue changes
Note: Note, that this system updates sine output values from the OPC UA server to the 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 the discoverer fails to connect to the server?
As a general rule for any IOServer, the 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 a non-zero value (value 9 or higher can be used to see OPC UA framework internal messages).
What to do when the 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.