跳到主要内容

结构体解码

概述​

OPC UA 1.04+ 把每个自定义结构体的字段定义放到 DataType 节点的 DataTypeDefinition 属性 (AttributeId=27, Part 5 §6.4.2)。SDK 实现完整协议层。

API​

解码器句柄类型是 DarraUa_StructDecoderHandle (不透明 uint32_t, UA_INVALID_STRUCT_DECODER_HANDLE = 0), 内部缓存已读过的 DataTypeDefinition。

函数类别读写说明
DarraUa_StructDecoder_New(session, &out_handle)构造—创建解码器, 绑定会话
DarraUa_StructDecoder_Free(h)释放—释放解码器 + 定义缓存; NULL 安全
DarraUa_StructDecoder_ClearCache(h)方法—清 DataTypeDefinition 缓存
DarraUa_StructDecoder_CachedCount(h)方法读已缓存的结构体定义数 (uint32_t)
DarraUa_StructDecoder_Decode(h, v, &out_st)方法读解码 ExtensionObject Variant
DarraUa_StructDecoder_DecodeBody(h, body, body_len, data_type_node_id, &out_st)方法读直接给 body 字节 + DataType NodeId
DarraUa_DecodedStruct_Free(st)释放—递归释放; NULL 安全
DarraUa_DecodedStruct_FindField(st, field_name)索引读按字段名查找, 不存在返回 NULL

解码结果是 DarraUa_DecodedStruct (fields / fields_count 字段数组 + type_id / status_text)。每个 DarraUa_DecodedField 是 name + DarraUa_FieldValueKind kind + value 联合体; kind 共 12 个值: UA_FIELD_VALUE_NULL / _BOOLEAN / _INT64 / _UINT64 / _DOUBLE / _STRING / _BYTES / _DATETIME / _GUID / _NODE_ID_STR / _NESTED_STRUCT / _ARRAY。

相关结构:

typedef enum {
UA_STRUCT_KIND_STRUCTURE = 0, // 普通
UA_STRUCT_KIND_STRUCTURE_WITH_SUBTYPED_VALUES = 1, // 含子类型
UA_STRUCT_KIND_STRUCTURE_WITH_OPTIONAL_FIELDS = 2, // 含可选 (前置 EncodingMask)
UA_STRUCT_KIND_UNION = 3, // Union (前置 selector)
UA_STRUCT_KIND_UNION_WITH_SUBTYPED_VALUES = 4 // Union + 子类型
} DarraUa_StructureKind;

代码示例​

#include <opcua_struct_decoder.h>

DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
DarraUa_SessionHandle ua = 0;
DarraUa_Session_Create(&cfg, &ua);
DarraUa_Session_Connect(ua);

DarraUa_StructDecoderHandle dec = UA_INVALID_STRUCT_DECODER_HANDLE;
DarraUa_StructDecoder_New(ua, &dec);

// 1) 读自定义结构
DarraUa_Status st = UA_STATUS_GOOD;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
ua, "ns=2;s=Robot.CurrentPose", UA_ATTR_VALUE, &st);

const DarraUa_Variant* v = DarraUa_DataValue_GetValue(dv);
DarraUa_DecodedStruct* pose = NULL;
DarraUa_StructDecoder_Decode(dec, v, &pose);

const DarraUa_DecodedField* x_val =
DarraUa_DecodedStruct_FindField(pose, "X");
if (x_val && x_val->kind == UA_FIELD_VALUE_DOUBLE)
printf("x = %f\n", x_val->value.v_dbl);

// 2) 嵌套
const DarraUa_DecodedField* quat_val =
DarraUa_DecodedStruct_FindField(pose, "Quaternion");
if (quat_val && quat_val->kind == UA_FIELD_VALUE_NESTED_STRUCT) {
const DarraUa_DecodedField* w_val =
DarraUa_DecodedStruct_FindField(quat_val->value.v_nested, "W");
if (w_val && w_val->kind == UA_FIELD_VALUE_DOUBLE)
printf("qw = %f\n", w_val->value.v_dbl);
}

// 3) 全字段遍历
for (size_t i = 0; i < pose->fields_count; ++i)
printf("%s = ...\n", pose->fields[i].name);

// 4) 已有原始字节时直接解码 (body + DataType NodeId)
const uint8_t body[] = { /* ExtensionObject body */ };
DarraUa_DecodedStruct* pose2 = NULL;
DarraUa_StructDecoder_DecodeBody(
dec, body, sizeof(body), "ns=2;i=3001", &pose2);
DarraUa_DecodedStruct_Free(pose2);

DarraUa_DecodedStruct_Free(pose);
DarraUa_DataValue_Delete(dv);
DarraUa_StructDecoder_Free(dec);
DarraUa_Session_Close(ua);

实现限制​

  • ExtensionObject 取不到字节时, Decode 返回 UA_STATUS_NOT_IMPLEMENTED, out_st 可能带 status_text 说明原因
  • 已有原始字节时用 DecodeBody 直接解码 (body + DataType NodeId)
  • DiagnosticInfo 跳过

最佳实践​

  • 共享一个 decoder, 字段表缓存复用
  • 服务端 DataType 变更后调 DarraUa_StructDecoder_ClearCache, 用 DarraUa_StructDecoder_CachedCount 观察缓存量
  • 嵌套结构 kind == UA_FIELD_VALUE_NESTED_STRUCT 后用 value.v_nested 递归访问
  • Union 看 DarraUa_StructureKind 的 UA_STRUCT_KIND_UNION

跨语言对照​

C#PythonJavaC++RustC
StructDecoderStructDecoderStructDecoderStructDecoderStructDecoderDarraUa_StructDecoderHandle
DecodedecodedecodeDecodedecodeDarraUa_StructDecoder_Decode
Dictionary<string, object>dictMap<String, Object>std::unordered_map<std::string, std::any>HashMap<String, DecodedValue>DarraUa_DecodedStruct

下一步​