Read Node OpcUa to JsonObject

41 Views Asked by At

I have an Opc client, when I read the node is ExtensionObject.

the node is custom structure in Opc Ua Server PLC siemens 1500.

the value is systematically encoded as a byte.

In the example provided, the node is read with an Xml encoding.

I tried to specify to the client not to use the Binary encoding with :

ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
endpoint.Configuration.UseBinaryEncoding = false;

This changes nothing.

I Have try this:

DataValue value = _session.ReadValue(nodeId);
ExtensionObject extObject = (ExtensionObject)value.Value;
var Decoder = new BinaryDecoder(extObject.Body as Byte[], ServiceMessageContext.GlobalContext);
var Result = Decoder.ReadXmlElement(null);
return value;

Exception :MaxByteStringLength 1048560 < 54657037

Do you have any idea what I might have missed?

Thanks in advance.

Best regards.

I try to Use BinaryDecoder and desactivating binary decoder in client.

1

There are 1 best solutions below

0
Teo230 On

Consider that I use reflection to auto convert the ExtensionObject by passing the required opc type

In case of single ExtensionObject

if (value is ExtensionObject extObj)
{
    var type = property.GetType().GenericTypeArguments[0];
    value = extObj.DecodeBinaryValue(type);
}

In case of ExtensionObject[]

if (value is ExtensionObject[] extArr)
{
    var listType = property.GetType().GenericTypeArguments[0];
    var type = listType.GenericTypeArguments[0];
    List<object> tempList = new List<object>();
    for (var index = 0; index < extArr.Length; index++)
    {
        var valueBytes = extArr[index];
        var ext = valueBytes.DecodeBinaryValue(type);
        tempList.Add(Convert.ChangeType(ext, type));
    }

    var castValue = Activator.CreateInstance(listType) as IList;
    foreach (var item in tempList) castValue.Add(item);
    value = castValue;
}

Finaly the extesion to convert the ExtensionObject

private static object DecodeBinaryValue(this ExtensionObject extensionObject, Type type)
{
    using var decoder = new BinaryDecoder(extensionObject.Body as byte[], ServiceMessageContext.GlobalContext);
    var decodeValue = decoder.ReadEncodeable("", type);
    return decodeValue;
}