我有一个Opc客户端,当我读到节点是ExtensionObject时。
该节点是 Opc Ua Server PLC siemens 1500 中的自定义结构。
该值被系统地编码为字节。
在提供的示例中,使用 Xml 编码读取节点。
我尝试向客户端指定不要使用二进制编码:
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);
endpoint.Configuration.UseBinaryEncoding = false;
这不会改变任何事情。
我已经尝试过这个:
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;
异常:MaxByteStringLength 1048560 < 54657037
你知道我可能错过了什么吗?
提前致谢。
致以诚挚的问候。
我尝试使用 BinaryDecoder 并在客户端中停用二进制解码器。
考虑我使用反射通过传递所需的 opc 类型来自动转换 ExtensionObject
单身的情况下
ExtensionObject
if (value is ExtensionObject extObj)
{
var type = property.GetType().GenericTypeArguments[0];
value = extObj.DecodeBinaryValue(type);
}
如果出现
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;
}
最后是扩展来转换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;
}