跳到主要内容

ReadHistory (Raw)

以下原型是 Session 的成员方法。

std::vector<DataValue> ReadHistory(
std::string const& node_id,
int64_t start_utc_100ns,
int64_t end_utc_100ns,
uint32_t max_values = 0,
bool return_bounds = false);

最常用的历史读, 返回 [start, end] 区间内全部原始数据点.

前置 / 配套

参数​

参数默认说明
node_id—目标 NodeId
start_utc_100ns—区间起 (FileTime)
end_utc_100ns—区间止 (FileTime)
max_values0单次返回上限; 0 = 不限 (服务端仍可自行设上限)
return_boundsfalsetrue = 端点一定包含在结果内

用法​

#include <darra/opcua/client.hpp>
#include <chrono>
#include <iostream>

using namespace darra::opcua;

Session s("opc.tcp://localhost:4840");
s.Connect();

constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;

auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t end_ft = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t start_ft = end_ft - 3600LL * 10000000LL; // 1 小时前

auto dvs = s.ReadHistory("ns=2;s=Temperature",
start_ft, end_ft, /*max_values=*/1000);

for (auto const& dv : dvs) {
std::cout << "ft=" << dv.SourceTimestamp()
<< " val=" << dv.Value().AsString()
<< " status=0x" << std::hex << dv.StatusCode() << "\n";
}

异常​

失败时抛 Exception, 消息形如 HistoryRead 'ns=2;s=Temperature' failed: 0x80720000; 状态码是服务端原样返回的, 用 e.status_code() (uint32_t) 或 e.status() (Status 枚举) 取, 人读的名字用 Stack::StatusName(e.status_code())。

状态码含义
BadHistoryOperationUnsupported服务端无 Historian
BadNotReadable节点无 HistoryRead 权限
BadCommunicationError网络故障

性能与最佳实践​

  • 高频数据 (1000 Hz+) 拉一天 = 86 M+ 点 → 必须用 max_values 限制或改用 ReadHistoryProcessed 聚合
  • std::vector<DataValue> 整体析构时元素自动释放, 不需要手动 Delete
  • 时间戳一律 UTC FileTime, 不要传本地时间

下一步​