2

i'm trying to retrieve the data from all fields in a plc with opc foundation libray in c#.

For now I managet to obtain only the values and not the rest.

For obtain the values i use this:

 session.Read(
            null,
            0,
            TimestampsToReturn.Neither,
            nodesToRead,
            out results,
            out diagnosticInfos);

and in result there are the values, but how can i retrieve the other attributes of the node?

I tried also to look in ocp foundation samples, and I used something like that:

FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Static | BindingFlags.Public);
            int num = 0;
            string[] array = new string[fields.Length];
            FieldInfo[] array2 = fields;
            foreach (FieldInfo fieldInfo in array2)
            {
                array[num++] = fieldInfo.Name;
            }
            uint[] attributesIds = Attributes.GetIdentifiers();
            ILocalNode node = m_session.NodeCache.Find(nodeIds[0]) as ILocalNode;
            ItemInfo info = new ItemInfo();
            ServiceResult result = node.Read(null, 1, info.Value);

But in this way I obtain only the standard fields and not value of my server.

here the other fields

Thanks in advance!

Giovanni
  • 21
  • 3

1 Answers1

2

You can browse throught the objects:

        ReferenceDescriptionCollection refs;
        byte[] bts;
        session.Browse(null, null, ObjectIds.ObjectsFolder, 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out bts, out refs);
        Console.WriteLine("\nDisplayName: BrowseName, NodeClass\n");
        foreach (var rd in refs)
        {
            Console.WriteLine("*Object: {0} | NodeId: {1}\n", rd.DisplayName, rd.NodeId);
            ReferenceDescriptionCollection refs2;
            byte[] bts2;
            session.Browse(null, null, ExpandedNodeId.ToNodeId(rd.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out bts2, out refs2);
            foreach (var ref2 in refs2)
            {
                ReferenceDescriptionCollection refs3;
                byte[] bts3;
                Console.WriteLine("\t #Interface/Object: {0} | NodeId: {1}", ref2.DisplayName, ref2.NodeId);
                Console.WriteLine("\t ------------------------------");
                session.Browse(null, null, ExpandedNodeId.ToNodeId(ref2.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out bts3, out refs3);
                foreach (var ref3 in refs3.Where(n => n.DisplayName != "Icon"))
                {
                    NodeId nodeId = new NodeId(ref3.NodeId.ToString());
                    Console.WriteLine("\n\t\t +Node: {0} | NodeId: {1} ", ref3.DisplayName, ref3.NodeId);
                    var references = session.FetchReferences(nodeId);
                    foreach (var reference in references.Where(r => r.NodeClass.ToString() == "Variable"))
                    {
                        Console.WriteLine("\t\t\t -Node Array Item: {0} | NodeId: {1}", reference.DisplayName, reference.NodeId);
                    }
                }
                Console.WriteLine("\n\t ------------------------------ \n");
            }

This code (console program) will browse through all object and get all variables (even the arrayitems). All the info you need is in the "refs" except for the current value.

For the values you can use the read function like you used before.

Victor Pieper
  • 540
  • 2
  • 17