Page 1 of 1

How to write to a 1-dimension array?

Posted: 16 Feb 2023, 14:17
by Senero
Hi,

I am working on the .Net 6 framework.

I have a custom C# application reading and writing to a OPC Server.
It works OK so far.

However, there is now a new NodeId added with 1-dimensional array and I need to write values to all the variable inside the array groups.
I've researched the topics regarding arrays in this forum but, I don't exactly see the same case as I have.

Here is my new NodeId;
Image

I've tried this piece of code which is working OK with scalar and I've improvised it a bit in order to adapt to array case.
But, of course, if this is even the right way of doing this, I can still see two issues at a glance;

1. int[] array is not what I want. It could be maybe an object[] for allowing different typed values (how about then type casting?)
2. How can I choose array index so I can write to any array group selectively? e.g. ACS_PRODUCT_DATA[3].ACS_ORDER_NUMBER = 5

Code: Select all

      int[] testArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
      Variant valuesInArray = new Variant(testArray);
      DataValue dataValue = new DataValue();
      dataValue.Value = valuesInArray;

      nodesToWrite.Add(new WriteValue()
      {
        NodeId = NodeId.Parse(nodeId),
        Value = dataValue,
        AttributeId = Attributes.Value
      });
      List<StatusCode> results = _iopcService.Session.Write(nodesToWrite);
Any input greatly appreciated.

Re: How to write to a 1-dimension array?

Posted: 17 May 2023, 09:03
by Support Team
Hello,

The value to write is an array of structured DataTypes.

If the structured DataType is know at compile time, you can use UaModeler to generate code this DataType. This code can be used in your client application. Please see the client tutorials in UaModeler in this case.

If the structured DataType is not known at compile time, the client needs to get the structure information about the DataType from the server addressspace at runtime. The server can do this in two ways:
* DataTypeDictionary. You can use the class DataTypeManager in this case. Please see the examples in Client Getting Started and ConsoleClient.
* DataTypeDefinition attribute. There is currently no toolkit class in the sdk supporting this feature in a a generic way. We are working on it.