Search code examples
c#opc-uaopcsiemens

How to get the value of a node with OPC UA and C#?


I have a panel Siemens TP1200 Comfort that I have configure as OPC AU server. This panel has some tags (nodes) from which I would like to get the value from a C# application.

I have read the examples of the OPC UA github project: https://github.com/OPCFoundation/UA-.NETStandard.

I am able to connect to the panel and get the root, but if I debug and I check the structure of the root, I don't see any property for the value neither a collection of childs nodes, so I don't know how to find a node by its name.

Is there a method something like GetNodeVale(NodeName);

I don't show any code because I am really lost with OPC, it is my first attempt to implement a simple client in which I want to read that of a node (a tag) but I am not able to do it.

Thanks.


Solution

  • To answer your question on how to get a node I am going to use the OPCua fx library: https://docs.traeger.de/en/software/sdk/opc-ua/net/client.development.guide
    (below is a version with OPC foundation)

    It has very good documentation and is easy to understand.

    First of all install OPCua fx using nuget.

    Next you need a few things.

    1. is the server adress found in TIA portal,

    server adress

    1. The values you want to read. For this make a server interface in the OPC ua commucation tab and drag the values you want in there.

    server interface

    1. Get the node IDs you want te read. For this I am using the Softing OPC ua Client: https://industrial.softing.com/products/opc-ua-and-opc-classic-sdks/opc-ua-demo-client.html Connect to your server using the adress from before. Go to the server interface you made earlier and to the value you want to read and click on it. Here you find the node id you need.

    Nodeadress

    After this. Connect with the adress from before:

    string opcUrl = "opc.tcp://192.168.54.200:4840/";
    var client = new OpcClient(opcUrl);
    client.Connect();
    

    In the documentation is also examples with username, password and certificates.

    After you can connect to you OPCua server you can start reading nodes:

    var node = client.ReadNode("ns=4;i=3");
    

    Ns means namespace and I believe that the I stands for id. This is how you can read a node. It is also possible to put a subscription on the node. Which is also explained in the documentation.

    After this you can write them:

    Console.WriteLine("node" + node.ToString());
    

    Good Luck!

    EDIT: There is also a good tutorial from a very lovely guy named Hans: https://www.youtube.com/watch?v=KCW23eq4auw

    EDIT2: Since most of you not want to spend 900euros for a licence (including me). I made another version for OPC foundation: https://www.nuget.org/packages/OPCFoundation.NetStandard.Opc.Ua.Client/

    First of all I have to give credits to: https://github.com/mdjglover/OPC-UA-READER/blob/main/OPC%20UA%20Reader/OPCUAClient.cs

    Since this is almost impossible to figure out how this works but I found this repo that explains it very good!

    If you use the explanation from the first version for the values you can include them in this code:

    // Most basic configuration setup required to create session
            ApplicationConfiguration configuration = new ApplicationConfiguration();
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            configuration.ClientConfiguration = clientConfiguration;
    
            // Create an endpoint to connect to
            string serverURL = "opc.tcp://192.168.54.200:4840/";
    
            try
            {
                // As the server instance I'm connecting to isn't using security, I've passed false as the second argument here.
                EndpointDescription endpointDescription = CoreClientUtils.SelectEndpoint(serverURL, false);
                EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(configuration);
                ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
    
                // Session options
                // Sets whether or not the discovery endpoint is used to update the endpoint description before connecting.
                bool updateBeforeConnect = false;
    
                // Sets whether or not the domain in the certificate must match the endpoint used
                bool checkDomain = false;
    
                // The name to assign to the session
                string sessionName = configuration.ApplicationName;
    
                // The session's timeout interval
                uint sessionTimeout = 60000;
    
                // The identity of the user attempting to connect. This can be anonymous as is used here,
                // or can be specified by a variety of means, including username and password, certificate,
                // or token.
                UserIdentity user = new UserIdentity();
    
                // List of preferred locales
                List<string> preferredLocales = null;
    
                // Create the session
                Session session = Session.Create(
                    configuration,
                    endpoint,
                    updateBeforeConnect,
                    checkDomain,
                    sessionName,
                    sessionTimeout,
                    user,
                    preferredLocales
                ).Result;
    
                NodeId nodeId = new NodeId("ns=4;i=3");
                var value = session.ReadValue(nodeId);
                Console.WriteLine(value);
            }
            catch
            {
                return;
            }
    

    This code is mostly made by someone