2

I have a bare bones C# OPC UA Client communicating with a OPC UA Server. The Server uses a Modbus data model. I'm actually the using Opc.UA.Fx package from NuGet.

I can connect, and get attributes from the Node I'm trying to read. However, I'm cannot figure out how to read the elements of the Array. I simply would like to read the elements of the Boolean Array. When I access the node value, it returns "System.Boolean[ ]"

  1. I have searched the OPC 10000-8 Pat 8: Data Access Manual, but it's not very clear to me on how to access the elements of my node.
  2. I've studied numerous source examples, but they are very bloated and cryptic.
  3. I've tried samples from the OPC.Ua.Fx literature, but I cannot get past the type conversion.
  4. I can verify my server is working properly by monitoring using UAExpert.

Here is the simple client to read the Coil Node (Discrete IO):

static void Main(string[] args)
{
  StringBuilder localEndpoint = new StringBuilder();
  var rawIpAddress = "127.0.0.1";
  localEndpoint.Append(epPrefix + rawIpAddress + epSuffix);

  // HmiClient is a class that constructs the OpcClient and Connects.
  var robot = new HmiClient(localEndpoint.ToString()); 

  // Create a list and store Attribute info
  List<string> coilNodeAttributes = new List<string>();    
  coilNodeAttributes = GetAttributeInfo(robot.hmiClient, NodeDef.Coils);

  foreach (var el in coilNodeAttributes)
  {
    Console.WriteLine(el);
  }
  Console.WriteLine("==================================\n");

  // Trying to determing the data type for reading the array elements.
  OpcValue discreteInputs = robot.hmiClient.ReadNode(1,302,OpcAttribute.Value);

  Type inputType = discreteInputs.GetType();      

  Console.WriteLine("ReadNode Value Relflection: {0}", inputType.ToString());  

  Console.Read();
}

Below is a screen shot of UAExpert connected to the server and my C# client connected.

enter image description here

Again, I do not understand why I simply cannot access the elements of the Boolean [ ]. I'm obviously struggling casting the value to the correct C# type.

Mitchtown98
  • 41
  • 1
  • 3
  • The `HmiClient` is a custom class? Normally you use the `Session.ReadValues(IList nodes, types, out values, out results)` to read the values. The NodeId has several constructor overloads (including an int (for your 302)) – Jeroen van Langen Dec 07 '21 at 13:58
  • Yes, it's just a simple class to connect to the server. Let me investigate and experiment with the method you suggested. – Mitchtown98 Dec 07 '21 at 14:05
  • You probably can read the value like `new NodeId("Coils[1]")` – Jeroen van Langen Dec 07 '21 at 14:07

1 Answers1

0

To answer my own question, my OpcValue is returned as an Object and needs to be cast to the type you are processing. I did not have my casting syntax correct.

Correct:

  OpcValue discreteInputs = robot.Client.ReadNode(1, 302, OpcAttribute.Value);

  bool[] discreteInputValues = (bool[])discreteInputs.Value;

Obviously, now I can just iterate over the array to display the elements.

Mitchtown98
  • 41
  • 1
  • 3