OpcUaNode (No equivalent in C)
C# provides single-node OpcUaNode wrapping, triggered by attributes RPC(node.Value such as /, etc node.DisplayName.). There is no C SDK such object encapsulation, but you can quickly retrieve complete node information with a single "batch read of multiple attributes."
Prerequisite Reading
- For the entry point, see Nodes Overview.
- To avoid multiple chain accesses, please see RPC TranslateBrowsePaths.
C# Syntax → C Syntax Comparison
Read the full node information
// C# - 6 times RPC
var n = ua.Nodes["ns=2;s=Temperature"];
Console.WriteLine(n.NodeId);
Console.WriteLine(n.DisplayName);
Console.WriteLine(n.BrowseName);
Console.WriteLine(n.NodeClass);
Console.WriteLine(n.DataType);
using var dv = n.Value;
C Recommendation: Read RPC multiple attributes in a single DarraUa_Session_Read pass (requires the ReadValueId structure, which is somewhat cumbersome), or, most simply, use a loop _ReadNode:
/* C - A Simple Approach */
struct { DarraUa_AttributeId attr; const char* name; } attrs[] = {
{ UA_ATTR_DISPLAY_NAME, "DisplayName" },
{ UA_ATTR_BROWSE_NAME, "BrowseName" },
{ UA_ATTR_NODE_CLASS, "NodeClass" },
{ UA_ATTR_DATA_TYPE, "DataType" },
{ UA_ATTR_VALUE, "Value" },
};
for (int i = 0; i < (int)(sizeof(attrs)/sizeof(attrs[0])); ++i) {
DarraUa_Status st = 0;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, "ns=2;s=Temperature", attrs[i].attr, &st);
if (dv) {
printf("%-12s = ...\n", attrs[i].name);
DarraUa_DataValue_Delete(dv);
}
}
A more efficient RPC approach is to use DarraUa_Session_Read (once, 5 ReadValueId calls); see Read/Write Batch Structures.
Write Node Value
// C#
var v = new Variant();
v.Double = 25.5;
ua.Nodes["ns=2;s=Setpoint"].Write(v);
/* C */
DarraUa_Variant* v = DarraUa_Variant_New();
DarraUa_Variant_SetDouble(v, 25.5);
DarraUa_Session_WriteNode(h, "ns=2;s=Setpoint", UA_ATTR_VALUE, v);
DarraUa_Variant_Delete(v);
Browse Subnodes
// C#
foreach (var c in ua.Nodes["ns=2;s=Boiler1"].Children)
Console.WriteLine($" {c.BrowseName} -> {c.NodeId}");
/* C */
DarraUa_BrowseResult* br = NULL;
DarraUa_Session_BrowseNode(
h, "ns=2;s=Boiler1", UA_NODE_CLASS_UNSPECIFIED, &br);
if (br) {
uint32_t n = DarraUa_BrowseResult_GetCount(br);
char buf[128];
for (uint32_t i = 0; i < n; ++i) {
const DarraUa_ReferenceDescription* rd =
DarraUa_BrowseResult_GetReference(br, i);
const char* name = DarraUa_Ref_GetBrowseName(rd);
DarraUa_Ref_GetNodeIdString(rd, buf, (int32_t)sizeof(buf));
printf(" %s -> %s\n", name ? name : "", buf);
}
DarraUa_BrowseResult_Delete(br);
}
The Cost of Chained Access
// C# Method A: Chained Indexer - 4 times RPC
using var time = ua.Nodes.Server["ServerStatus"]["CurrentTime"].Value;
// 1 time 2 times 3 times 4 times Read
C Recommendations:
/* Method B: Path Resolution - Layer by layer ResolveBrowsePath based on path segments ( Browse 2 segments = 2 times Browse RPC) + 1 read */
char id[128];
DarraUa_Session_ResolveBrowsePath(
h, "i=2253", "/ServerStatus/CurrentTime", id, (int32_t)sizeof(id));
/* Returns 1 = success (the parsed string NodeId is stored in `id`), 0 = failure */
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, id, UA_ATTR_VALUE, &st);
/* Method C: Use directly NodeId - 1 time RPC */
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, "i=2258", UA_ATTR_VALUE, &st);
If the path is known → Approach C; exploratory development → Approach B.