Having seen how to write an OPC UA server, today we focus on its counterpart and analyze how to develop an OPC UA client using the same .NET libraries from the OPC Foundation.

Starting the OPC UA Server

To get started, let’s boot up our OPC UA server.

If you’ve been following my series of articles, this translates to running the project we discussed in the dedicated article.

Once running, the endpoint we need to use to connect will be visible in the console, in my case:

Copy to Clipboard

At this point, we start an OPC UA client and use the endpoint to connect to the server. We will need this in a moment to retrieve some useful information for writing our client.

Still following what we saw in the previous article, I will make use of UaExpert. For details on its use I refer you to this page.

Before proceeding to analyze the project code, it is necessary to prepare the development environment.

For the installation steps of the OPC UA libraries in Visual Studio, please refer to the Environment Setup paragraph of the last article.

The OPC UA Client

We can start the analysis of the client project which you can download from this link.

Specify the Application Type

First, let’s start by defining the type of application we want to develop.

Copy to Clipboard

In lines 2 and 3 we specify that our application will be of client type. The next step is to load an XML file containing some operating parameters that we will need later.

You can of course view the XML by downloading the project from the link I mentioned at the beginning of this paragraph.

Session Creation

We are ready to create the session with the OPC UA server.

Copy to Clipboard

Lines 3-6 generate the connection endpoint by taking as input both the server URL and a boolean specifying whether we want to make a secure connection (line 3). Since this is a test and we don’t handle the encryption part of the connection between client and server, we disregard this last parameter.

The instruction from line 9 to 17, finally deals with making the connection to the OPC UA server by returning a session object that will be used, as you will see, for the next operations.

Reading and Writing

Let’s continue with two very common types of operations: reading and writing a variable.

In this regard, using the UaExpert client presented earlier, we select the Speed node of the Conveyor object from the node tree at the bottom left.

The selection just made will allow us to understand which are the extremes to point to the node in question that in OPC UA take the name of namespace and identifier.

With this information, we can now take a reading of the Speed value.

Copy to Clipboard

The ReadValue method takes as input a NodeId that is characterized by the values retrieved earlier.

In output we get the value contained in the variable Speed, output that we then print on the screen.

As far as writing is concerned, we need a few more instructions:

Copy to Clipboard

First, in line 2, we create a Collection of values in writing.

This list is immediately filled in line 9 by an element created in lines 4-8.

The element in question is a WriteValue that is pointed to the Speed node, always through the usual namespace and identifier (line 5).

The value to write to the variable is specified in line 8.

The actual writing is done in lines 15-18.

Calling a Method

The last thing we have left to discuss is remote method calls.

The idea is to make a call to the Start method of the Conveyor object.

Similar to reading and writing a variable, using UaExpert, we go to select the Conveyor object and then the Start method.

In both cases we note down the namespace and identifier separately: these will be needed to execute the remote call.

At this point, we can proceed with the following instruction:

Copy to Clipboard

The call involves specifying two input parameters:

  1. Namespace (2) and identifier (96) of the Conveyor node.
  2. Namespace (2) and identifier (103) of the Start method.
    In output we obtain the eventual elements returned by the called method.

Final Test

Once the project is downloaded, let’s compile it. Before running it, we use UaExpert to verify the correct functioning of our client.

In this regard, we select the Speed attribute of the Conveyor node and drag it into the central window of UaExpert.

By doing so we will be able to see the value of the speed.

It is possible to use UaExpert to write the value, just double-click on the cell relative to the value column and enter the desired numerical value. Pressing enter will finalize the writing.

At this point, we proceed with the execution of our client.

The result will be, in sequence:

  • The reading of the speed of the Conveyor node, which will turn out to be 0 or the value we inserted with UaExpert.
  • The writing of the same node, imposing the value 7; the new data will be visible by UaExpert.
  • The call of the Start method that, on the server side, will print on screen the string “Conveyor started“.

Conclusions

In this article we have taken a close look at how the .NET libraries of the OPC Foundation work.

Specifically, we have developed a simple OPC UA client demonstration that dialogs with the server written in the last article.

You can download the full project at this link.

For more examples, I recommend you take a look at the official github repository, in particular at this address you can find several projects from which to draw inspiration.