Read Complex structure |OPC UA Implementation #2331
Replies: 1 comment 6 replies
-
I haven't used public class MyObject
{
public int SomeNumber { get; set; }
public string SomeText { get; set; }
public float SomeFloat { get; set; }
} Then I use a combination of recursion and reflection to parse the var vector = await plc.ReadAsync<Vector>(vectorTag);
Console.WriteLine($"[{vector.X}, {vectory.Y}, {vector.Z}]"); The object could have a property that is an object itself which is where the recursion comes in. I just have to account for the various types in my switch. Hopefully this helps! One thing to note is that I'm still working on figuring out how to go the other direction. I can serialize the data structure to the correct byte array but it won't let me write to the tag. I think I have to have the public TOutput FromBytes<TOutput>(byte[] bytes, IServiceMessageContext ctx)
{
var decoder = new BinaryDecoder(bytes, ctx);
var outputResult = DeserializeTo(typeof(TOutput), bytes, decoder);
return (TOutput)outputResult;
}
static object DeserializeTo(Type t, byte[] bytes, BinaryDecoder decoder)
{
var outputInstance = Activactor.CreateInstance(t);
var properties = t.GetTypeInfo().GetProperties();
foreach (var p in properties)
{
object? value = p.PropertyType switch
{
Type { Name: nameof(Int16) } => decoder.ReadInt16(p.Name),
Type { Name: "Int16[]" } => decoder.ReadInt16Array(p.Name),
/* etc. */
Type { IsClass: true } childType => DeserializeTo(childType, bytes, decoder),
_ => null
};
p.SetValue(outputInstance, value);
}
return outputInstance;
} |
Beta Was this translation helpful? Give feedback.
-
Hi, I am new to OPC ua and I am having problems reading structure data in a server from an opc ua client.
My code is very simple:
The properties value of Conf variable after call method are good but the type of Conf[0].body variable is "Opc.Ua.ComplexTyper.ne=6.i=99999.6.ConfigType" so confEncod is of the same type and configuration is null.
If i try a direct cast the error is
'Unable to cast object of type 'Opc.Ua.ComplexTypes.ns=6;i=99999.6.ConfigType' to type 'Quickstarts.ConsoleReferenceClient.ConfigType'
Anyone can help me?
Thanks a lot
Beta Was this translation helpful? Give feedback.
All reactions