Search code examples
c#opc-uaopc

Encoding and Decoding Custom OPC-UA DataTypes with UA-ModelCompiler


I am working on an OPC UA project where I'm managing custom DataTypes. I defined a complex object in UaModeler, and used UA-ModelCompiler tool to generate the model code in C# for my OPC-UA server made using UA-.NETStandard. However, when I read from it using a OPC UA client, I'm unsure how the encoding and decoding of this custom DataType should be handled. It appears the data needs to be sent as a serialized ExtensionObject, however I couldn't find much documentation on how exactly to encode the custom DataType to a byte array and wrap it with ExtensionObject.

Is there an example or design pattern for how I should implement this to best meet OPC UA standards?

I've read that the automatically generated code from UA-ModelCompiler should include encode and decode functionality, but I'm not sure how to employ that to serialize the custom DataType.


Solution

  • The implementation depends on the used SDK. In case you know the type in advance, most of the SDKs offer some kind of code generation for this task, so you do not need to implement this yourself.

    In case the data type is unknown to the client in advance, parsing the type can be done using the StructureFields (which hopefully are exposend by the server) https://reference.opcfoundation.org/Core/Part3/v105/docs/8.51#Table35

    See also the general information about Extension Objects: https://reference.opcfoundation.org/Core/Part6/v105/docs/5.1.8

    PS: Older servers store the structure of these data types as XML in a variable in the type namespace.

    Edit: For the .Net SDK from the OPC Foundation see the example here: https://github.com/OPCFoundation/UA-.NETStandard-Samples/tree/e100ac787507988da95223a031af76fe57b5e11d/Workshop/DataTypes