Session 会话 — DarraOpcUa
DarraOpcUa 是 Client 主类, 一个实例对应一个 OPC UA Session. 所有访问 (Read / Write / Browse / Subscribe / Call / History) 都从这个对象出发.
子页跳转
- 构造与连接参数请参考 构造函数.
- 连接 / 断开请参考 连接管理.
- 单点 / 批量读写请参考 Read / Write.
- 事件统一通道请参考 Events.
- 命名空间反查请参考 Namespaces.
- 心跳与会话保活请参考 KeepAlive.
公共属性
| 类别 | 属性 | 类型 | 访问 | 说明 |
|---|---|---|---|---|
| 连接 | EndpointUrl | string | R | 构造时指定的 endpoint URL |
SecurityMode | MessageSecurityMode | R | 安全模式 (None / Sign / SignAndEncrypt) | |
SecurityPolicyUri | string | R | SecurityPolicy URI (例如 #None / #Basic256Sha256) | |
ClientCertificatePfxPath | string | R | 客户端 PFX 证书路径 | |
| 状态 | State | SessionState | R | 当前会话状态 (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed) |
IsConnected | bool | R | 是否已连接 (= State == Connected) | |
| 子对象 | Nodes | NodeCollection | R | 节点访问器 (按 NodeId 字符串索引取节点) |
Namespaces | NamespaceCollection | R | 命名空间 URI ↔ Index 互转 | |
| 事件 | Events | OpcUaEvents | R | 会话级事件统一通道 (StateChanged / Connected / Disconnected / Reconnecting / Reconnected / KeepAlive / CommunicationError / SubscriptionLost / Any / NodeRemoved / ...) |
| 配置 | KeepAliveIntervalMs | uint | RW | KeepAlive 心跳周期 (默认 10000 ms, 0 = 禁用) |
AutoPublish | bool | RW | 启停后台 Publish 线程 (Connect 成功后自动开启) |
公共方法
连接管理
Connect()— 建立连接 (Hello → SecureChannel → Session → Activate)Disconnect()— 主动断开, 实例可重连Dispose()— 断开并释放会话句柄 (幂等, 可重复调用)
心跳与发布
Publish(timeoutMs = 2000)— 手动触发一次 Publish (一般无需调用), 返回StatusCode
读 / 写
Read(nodeId, attribute = Value)— 读单节点指定 AttributeReadMany(nodeIds, attribute)— 批量读 (单 Attribute)ReadMany(items)— 批量读 (任意 NodeId + AttributeId 组合)Write(nodeId, value, attribute = Value)— 写单节点
浏览
Browse(nodeId, filter = Unspecified)— 单层浏览BrowseMany(nodeIds, filter)— 批量浏览BrowseWithPaging(nodeId, filter)— 含 ContinuationPoint 翻页支持BrowseNext(continuationPoint, release = false)— 续翻分页TranslateBrowsePaths(paths)— 路径批量解析为 NodeIdResolve(path)— 单条绝对路径解析Resolve(startNodeId, path)— 单条相对路径解析RegisterNodes(nodeIds)— 注册临时高效 NodeIdUnregisterNodes(registeredNodeIds)— 注销临时 NodeId
订阅
CreateSubscription(publishingIntervalMs = 500.0)— 创建数据订阅SubscribeEvents(nodeId = null, existingSubscription = null)— 订阅事件 (报警 / 条件)TransferSubscriptions(subscriptionIds, sendInitialValues = true)— 跨 Session 迁移订阅
方法调用
Call(objectId, methodId, ...inputs)— 调用 OPC UA Method 节点
历史
历史入口直接挂在 DarraOpcUa 上, 不是独立的 History 属性:
ReadHistory(nodeId, startTime, endTime, maxValues = 0)— 读原始历史 (简化入口)ReadHistoryModified(...)— 读含修改记录的历史ReadHistoryAtTime(...)— 读指定时间点的内插值ReadHistoryProcessed(...)— 聚合 (Average / Minimum / Maximum / TimeAverage / ...)ReadHistoryEvents(...)— 读历史事件UpdateHistory(nodeId, updateType, values)— Insert / Replace / Update / Remove 历史DeleteHistoryRange(nodeId, start, end, deleteModifiedToo = false)— 按时间段删除历史
控制
Cancel(requestHandle)— 取消未完成请求, 返回uint
完整示例
using System;
using System.Threading;
using DarraOpcUa_Client;
using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Events.Connected += (s, e) => Console.WriteLine($"Connected to {e.EndpointUrl}");
ua.Events.Disconnected += (s, e) => Console.WriteLine($"Disconnected: {e.StatusCode} {e.Message}");
ua.Connect();
// 读
using var dv = ua.Read("ns=2;s=Temperature");
Console.WriteLine($"T = {dv.Value.AsDouble} °C");
// 写
using var sp = new Variant();
sp.Double = 42.0;
var st = ua.Write("ns=2;s=Setpoint", sp);
Console.WriteLine($"Write status = {st}");
// 订阅
using var sub = ua.CreateSubscription(500);
sub.DataChanged += (s, e) =>
Console.WriteLine($"{e.NodeId} = {e.ValueString} ({e.Status})");
sub.Add("ns=2;s=Counter");
// 方法
using var a = new Variant(); a.Int32 = 3;
using var b = new Variant(); b.Int32 = 4;
var outputs = ua.Call("ns=2;s=Calc", "ns=2;s=Calc.Add", a, b);
foreach (var v in outputs) using (v)
Console.WriteLine($"sum = {v.AsInt32}");
Thread.Sleep(TimeSpan.FromMinutes(1));