How to write a simple OPC UA client in C#

Blog Post created by jhuang on Oct 22, 2009

In our previous blog post we talked about the CTP release of PI OPC UA Server.  We also had a vCampus webinar dedicated to OPC UA.  During that webinar we showed a simple OPC UA client that can be built with only several lines of code.  In this blog post, we will go through that code and explain how it works.  The client uses OPC UA assemblies (Opc.Ua.Core.dll and Opc.Ua.Client.dll) that implement OPC UA communication stack, client API classes, and helper classes.  Since it’s a simple client, it’s built to do very basic things, such as connecting to the OPC UA server, reading the value of a known node (e.g. PI point) and disconnecting from the server.  We will use the PI OPC UA Server as a target server.  A Visual Studio 2008 solution for this client is attached. 

  • Connecting to PI OPC UA Server

To establish a connection between an OPC UA client and an OPC UA server, we must create a secure channel in the communication layer. This is a logical channel that is needed to ensure confidentiality and integrity of a message exchange between two applications.  Once the secure channel has been established, we need to create and activate a session between client and server.  The session allows us to call services provided by an OPC UA server.  In Step 1, we pass the URL of the PI OPC UA Server to the CreateSession() helper method to establish the secure channel, and then create and activate the session.


// Step 1 -- Connect to UA server


string discoveryUrl = "";




Session mySession = ClientUtils.CreateSession(discoveryUrl, "OSIsoft.UA.ConsoleClientDemo"); 

  • Read Value

After the session is created, we can try calling different OPC UA services provided by PI OPC UA Server.  One of the basic services is Read Service.  It can be used to read values of OPC UA nodes.  Step 2 shows how the service can be called using Session object.  In order to make this call we have to pass a NodeId.  NodeIds are represented by a string containing a browse path of AF element or attribute.  The PI OPC UA Server has several nodes that are mapped to PI points.  Let’s choose one of them.   


// Step 2 -- Read the value of a node representing a PI Point data under the hood

NodeId nodeToRead = new NodeId(@"\\jmaytumwin2k8\intense3000\osisoft\piservers\jmaytumwin2k8\intense0001|value", 3);

DataValue value = mySession.ReadValue(nodeToRead); 


Console.WriteLine("\nRead: Current value of intense0001 = {0}, status = {1} \n", value.Value, value.StatusCode); 



  • Clean Up

Step  3 - Close the current session


// Step 3 -- Clean up

mySession.Close ();

Okay, we showed you how to write a simple OPC UA client in three easy steps.  It's important to note that by using OPC UA assemblies, we didn't have to implement complex web service communication mechanism.  All of that has been abstracted from us by the client API.  In the upcoming blog posts we will show you how to use other OPC UA services and implement more advanced functionality. Please let us know if you have any questions and we will be happy to provide you with more details.  Thanks for reading and happy coding!