Martin Christiansen
Martin Christiansen

Reputation: 1117

How to retrieve an OPC item with OPCFoundation.NetStandard.Opc.Ua

I try to use the NuGet package OPCFoundation.NetStandard.Opc.Ua for retrieving data from an opc-server. But I can't figrure out how to directly address an individual item by name ("Channel1.Device1.Tag1") in order to read it or make a subscription.

I have downloaded and studied the complete sample code from OPCFoundation's GitHub repository, but I only found code that retrieves all available items (by browsing the server) and then let the user pick one of those.

I need to somehow lookup an item directly from its name ("Channel1.Device1.Tag1") in order to obtain a NodeId-instance, which I can then use for reading or subscribing (I know how to do that part once I have the NodeId).

If anyone could just post a short example or point me to some sample code that does the job, I would be very happy!

Upvotes: 0

Views: 1122

Answers (1)

pollirrata
pollirrata

Reputation: 5286

Once you've established the session, one option would be to look for the _System object within the Objects folder in order to get the namespace index since it seems can change.

ReferenceDescriptionCollection refdescs;

      byte[] continuationPoint;


      session.Browse(null, null, ObjectIds.ObjectsFolder, 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out continuationPoint, out refdescs);


      foreach (var item in refdescs)
      {
        if (item.DisplayName.Text == "_System")
        {
          var nsi = item.NodeId.NamespaceIndex.ToString();
          Console.WriteLine($"Namespace Index {nsi}");
        }
      }

Once you get it, your identifier for reading/writing would be in the following format

ns=[NamespaceIndex];s=Channel1.Device1.Tag1

Upvotes: 1

Related Questions