Search code examples
c#opc-ua

OPC UA - Serialize an ExtensionObject


I'm working on a wrapper library for the OPCFoundation.NetStandard.Opc.Ua Nuget package. The purpose of the library is to make reading and writing PLC tags a bit more straight forward. Being able to read / write scalar values and arrays of scalar values works well. I'm having trouble writing OPCUA's ExtensionObject type.

I read it in as an ExpandoObject which works. Reading an ExtensionObject would look like

dynamic tagValue = await plc.ReadAsync<dynamic>(someTag);

What I'm trying to do is being able to modify the values of the dynamic object and write it back to the PLC tag. I understand the value read by OPC-UA is a byte[]. I've been trying to figure out how to serialize my dynamic object into a properly serialized ExtensionObject.

I've noticed that the value returned by the OPCUA session doesn't really line up but I can't find out why. Say I have an extension object That has 3 extension objects. The first child has 8 bool tags, the second has 10 bool tags, and the third has a bool, two int, and one string. This is the hex value I get back when I read the parent node

00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-01-00-00-00-00-00-09-00-00-00-4E-6F-20-41-6C-61-72-6D-73-00

When I try to serialize my dynamic object

00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-01-00-00-00-00-00-4E-6F-20-41-6C-61-72-6D-73-00

Can anyone help me understand where the extra data is coming from? I thought the 0x09 byte was saying how many bytes were in the body but it didn't add up. I feel like as long as I can serialize my object to look identical to how it is read I should be able to update an ExtensionObject tag pretty easily.


Solution

  • I had found how data is binary encoded from the OPC Foundation's website. For things like strings or arrays the data is preceded by how long it is. That's where the extra data was coming from.

    https://reference.opcfoundation.org/Core/Part6/v104/docs/5