MonitoredItem with Structs

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
bwilli
Jr. Member
Jr. Member
Posts: 3
Joined: 14 Dec 2017, 17:33

MonitoredItem with Structs

Post by bwilli »

Dear Community,

I'm accessing structs defined in the PLC of a controller. I've created clones of that structs in my C# project. This was done by creating classes with the same member types as the structs on the server side. The classes and members have been annotated with the UaTypeDefinition resp. the UaInstanceDeclaration attributes.

Reading and writing of these structs work fine with

Code: Select all

var modelHandle = session.Model.Read(nodeId, obj);    // where obj is an instance of my C# class
or

Code: Select all

var modelHandle = session.Model.Write(nodeId, obj);
I'm also trying to create a MonitoredItem with the nodeId of the structs, the notification works fine, when changing a value within the struct on the server.
In the subscription.DataChanged eventhandler I can get the DataChange values and thus the DataValue of each DataChange.

But: All I can see in my DataValue is a body of type byte[].
I retrieve this like

Code: Select all

byte[] GetBuffer(DataValue dataValue)
{
    var wrappedValue = dataValue.WrappedValue;
    ExtensionObject extensionObject = wrappedValue.GetValue<ExtensionObject>(null);
    return (byte[])extensionObject.Body;
}
The buffer contains the exakt memory layout of the structs. So what I did was to create a custom deserializer, that creates my instances based on that buffer (using BitConverter's methods). This approach basically works, but is error prone (changing types within structs on server, changing order of fields in structs needs to be reflected in the deserializer).

Is there an elegant way to deserialize structs to local C# instances in DataChanged events of the subscription?

Any tip is appreciated.
Thanks,
Bruno

Post Reply