i'm struggling in the attempt to write an ARRAY of Custom Objects to a server.
The array already exists on the server and can be easily written with Ua Expert, but since i (suppose) can't access Ua Expert CODE, i need to figure out how to it.
Since parsing a custom structure seems hard, i tried first a simpler approach: reading the array, and writing it with just a little change. This doesn't work either.
Sorry guys if this code is both in c# and vb.net
Actually my write result gets "BadWriteNotSupported".
This is the array i need to change:
Code: Select all
//Function to read the array from server (Works)
ParametriOutput = ClientData.ReadValues(OPCUA.Connessione, ParametriDaLeggere)
//This is when i create the new DataValue (Not in the proper way, i guess)
Dim modifiedArray As DataValue = OpcuaLib.Test.ConvertDataValue_v2(OPCUA.Connessione.Sessione,
ParametriOutput.FirstOrDefault(), "Name", "ciao", 0)
Dim nuovoWrite As New WriteValue With {
.NodeId = New NodeId("WRITEVAR", NameSpaceIndex),
.AttributeId = Attributes.Value,
.Value = modifiedArray
}
Dim nuovaListWrite As New List(Of WriteValue) From {nuovoWrite}
Dim result As List(Of StatusCode) = OPCUA.Connessione.Sessione.Write(nuovaListWrite)
//here i get error "{BadWriteNotSupported}"
The function i use (found on this forum and changed a little to suit my needs) to modify and encode the object is this:
Code: Select all
public static DataValue ConvertDataValue_v2(Session Sessione, DataValue dv, string fieldName, object fieldValue, int index = -1)
{
DataTypeManager dataTypeManager = new DataTypeManager(Sessione);
if (dv.WrappedValue.IsArray)
{
var values = dv.WrappedValue.ToExtensionObjectArray();
ExtensionObject e = values[index];//.ToExtensionObject();
GenericEncodeableObject encodeable = dataTypeManager.ParseValue(e);
encodeable[fieldName] = new Variant(fieldValue, encodeable[fieldName].TypeInfo);
ExtensionObject eo = EncodeableObject.Encode(Sessione.MessageContext, encodeable, false);
values[index] = eo;
return new DataValue(values);
}
else throw new Exception("NOT AN ARRAY");
}