2

I have a Siemens PLC which has an OPC UA Server on it. The image below shows the data I'm getting from browsing the nodes when I use the ReferenceClient.csproj found here https://github.com/OPCFoundation/UA-.NETStandard under the /Applications/ReferenceClient.

- Image of the objects on the Siemens OPC UA Server -

My goal is to read the entire object on the OPC UA Server called "Data". Currently, I'm only able to read each child node of "Data", for example "pressure", using this code:

using Opc.Ua;
using Opc.Ua.Client;

NodeId pressureNodeId = new NodeId("ns=4;i=17");
short pressure = (short)session.ReadValue(pressureNodeId).GetValue(typeof(short));

which will give me the value of the pressure node inside the "Data" object.

What I want to do, is to read the entire "Data" object all at once.

I have a class in C# called "DataClass" which I want to construct using the data from the "Data" node object on the OPC UA Server. Here's the code I have for the class:

namespace OPCUA_Testing
{
    public class DataClass
    {
        public Int16 pressure { get; set; }
        public Int16 Startup_Counter { get; set; }
        public Startup_Time Startup_Time { get; set; }
        public Int16 temperature { get; set; }
        public Int16 Value1 { get; set; }
    }

    public class Startup_Time
    {
        public byte DAY { get; set; }
        public byte HOUR { get; set; }
        public byte MINUTE { get; set; }
        public byte MONTH { get; set; }
        public UInt32 NANOSECOND { get; set; }
        public byte SECOND { get; set; }
        public byte WEEKDAY { get; set; }
        public UInt16 YEAR { get; set; }
    }
}

What I would like to do is something like this (to instantiate the entire "Data" node, and all its children, all at once):

using Opc.Ua;
using Opc.Ua.Client;

NodeId dataClassNodeId = new NodeId("ns=4;i=2");
DataClass data = (DataClass)session.ReadValue().GetValue(typeof(DataClass));

When I run the above code trying to access the entire "Data" object from the OPC UA Server, I get the error: "Opc.Ua.ServiceResultException: 'BadAttributeIdInvalid'"

So my ultimate question is, how can I read/write an entire object from an OPC UA Server using an OPC UA Client and instantiate that object into a C# class?

Any insights are appreciated, thank you.

1 Answers1

0

Best I can think of is browsing into the data object:

var references = session.FetchReferences(nodeId);
                        if (references != null)
                        {
                            foreach (var reference in references.Where(r => r.NodeClass.ToString() == "Variable"))
                            {
                                NodeId nodeId = new NodeId(references.NodeId.ToString());
                                var value = session.ReadValue(nodeId);
                                Console.WriteLine(reference.BrowseName + " NodeId: " + reference.NodeId.ToString() + "value: " + value);
                            }
                        }

Using this code it will display all references (variables underneath data object). In reference is more data you can use to identify the right variable using a string.

Victor Pieper
  • 540
  • 2
  • 17