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 | Lazy Load 节点访问器 |
Namespaces | NamespaceCollection | R | 命名空间 URI ↔ Index 互转 | |
History | HistoryAccessor | R | 5 模式 HistoryRead + HistoryUpdate | |
| 事件 | Events | OpcUaEvents | R | 会话级事件统一通道 (Connected / Disconnected / DataChange / ServerEvent / ...) |
| 配置 | KeepAliveIntervalMs | uint | RW | KeepAlive 心跳周期 (默认 10000 ms, 0 = 禁用) |
AutoPublish | bool | RW | 启停后台 Publish 线程 (默认 true) |
公共方法
连接管理
| 方法 | 说明 |
|---|---|
Connect() | 建立连接 (Hello → SecureChannel → Session → Activate) |
Disconnect() | 主动断开, 实例可重连 |
Dispose() | 释放资源, 自动 Disconnect |
心跳与发布
| 方法 | 说明 |
|---|---|
Publish(timeoutMs = 2000) | 手动触发一次 Publish (一般无需调用) |
读 / 写
| 方法 | 说明 |
|---|---|
Read(nodeId, attribute = Value) | 读单节点指定 Attribute |
ReadMany(nodeIds, attribute) | 批量读 (单 Attribute) |
ReadMany(items) | 批量读 (任意 NodeId + AttributeId 组合) |
Write(nodeId, variant, attribute = Value) | 写单节点 |
浏览
| 方法 | 说明 |
|---|---|
Browse(nodeId, filter = Unspecified) | 单层浏览 |
BrowseMany(nodeIds, filter) | 批量浏览 |
BrowseWithPaging(nodeId, filter) | 含 ContinuationPoint 翻页支持 |
BrowseNext(continuationPoint, release = false) | 续翻分页 |
TranslateBrowsePaths(paths) | 路径批量解析为 NodeId |
Resolve(path) | 单条绝对路径解析 |
Resolve(startNodeId, path) | 单条相对路径解析 |
RegisterNodes(nodeIds) | 注册临时高效 NodeId |
UnregisterNodes(registeredIds) | 注销临时 NodeId |
订阅
| 方法 | 说明 |
|---|---|
CreateSubscription(intervalMs = 500) | 创建数据订阅 |
SubscribeEvents(nodeId = null, existingSub = null) | 订阅事件 (报警 / 条件) |
TransferSubscriptions(subIds, sendInitialValues) | 跨 Session 迁移订阅 |
方法调用
| 方法 | 说明 |
|---|---|
Call(objectId, methodId, ...inputs) | 调用 OPC UA Method 节点 |
历史
| 方法 | 说明 |
|---|---|
ReadHistory(nodeId, start, end, maxValues = 0) | 读原始历史 (简化入口) |
History.ReadModified(...) | 读含修改记录的历史 |
History.ReadAtTime(...) | 读指定时间点的内插值 |
History.ReadProcessed(...) | 聚合 (Avg / Min / Max / TimeAverage / ...) |
History.ReadEvents(...) | 读历史事件 |
History.UpdateData(...) | Insert / Replace / Update 历史 |
History.DeleteRange / DeleteAtTime / DeleteEvents | 删除历史 |
控制
| 方法 | 说明 |
|---|---|
Cancel(requestHandle) | 取消未完成请求 |
完整示例
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.Reason}");
ua.Connect();
// 读
using var dv = ua.Read("ns=2;s=Temperature");
Console.WriteLine($"T = {dv.Value.AsDouble} °C");
// 写
var st = ua.Write("ns=2;s=Setpoint", new Variant(42.0));
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");
// 方法
var outputs = ua.Call("ns=2;s=Calc", "ns=2;s=Calc.Add",
new Variant(3), new Variant(4));
foreach (var v in outputs) using (v)
Console.WriteLine($"sum = {v.AsInt32}");
Thread.Sleep(TimeSpan.FromMinutes(1));