HistoryReadRaw (C)
OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Session_HistoryReadRaw(
DarraUa_SessionHandle h,
const char* node_id_str,
DarraUa_DateTime start_time,
DarraUa_DateTime end_time,
uint32_t num_values_per_node, /* 0 = 不限 */
DarraUa_Boolean return_bounds,
DarraUa_DataValue*** out_values,
uint32_t* out_count);
最常用的历史读, 返回 [start_time, end_time] 区间内的原始数据点; 实际点数受 num_values_per_node 限制.
前置 / 配套
- 高频数据点过多时改用 ReadProcessed 聚合.
- 想要含修改记录请用 ReadModified.
参数
| 参数 | 默认 | 说明 |
|---|---|---|
| node_id_str | — | 目标 NodeId 字符串 |
| start_time | — | 区间起 (UTC, 100ns since 1601) |
| end_time | — | 区间止 |
| num_values_per_node | 0 (不限) | 单次返回上限 |
| return_bounds | 0/1 | 是否返回边界点 (端点必含) |
| out_values | — | 出参, out_values 指向 Stack malloc 的 DataValue 数组 |
| out_count | — | 出参, 数组长度 |
内存释放
DarraUa_DataValue** arr = NULL;
uint32_t cnt = 0;
DarraUa_Session_HistoryReadRaw(h, "ns=2;s=Temperature",
start, end, 1000, /*bounds*/ 1, &arr, &cnt);
/* ... 使用 ... */
DarraUa_DataValueArray_Delete(arr, cnt); /* 一次性释放数组 + 每项 */
完整用法
#include <opcua/opcua.h>
#include <opcua/opcua_history.h>
#include <stdio.h>
#include <time.h>
static DarraUa_DateTime utc_now_ft(void) {
return (DarraUa_DateTime)((int64_t)time(NULL) * 10000000LL + 116444736000000000LL);
}
int main(void)
{
/* ... Connect 略 ... */
DarraUa_SessionHandle h = /* ... */ 0;
DarraUa_DateTime end = utc_now_ft();
DarraUa_DateTime start = end - 36000000000LL; /* 1 小时 = 3600 秒 */
DarraUa_DataValue** values = NULL;
uint32_t count = 0;
DarraUa_Status st = DarraUa_Session_HistoryReadRaw(
h, "ns=2;s=Temperature",
start, end,
/*num*/ 1000,
/*return_bounds*/ 1,
&values, &count);
if (!UA_STATUS_IS_GOOD(st)) {
fprintf(stderr, "HistoryReadRaw failed: %s\n", DarraUa_StatusName(st));
return -1;
}
printf("Got %u points\n", (unsigned)count);
for (uint32_t i = 0; i < count; ++i) {
const DarraUa_Variant* v = DarraUa_DataValue_GetValue(values[i]);
DarraUa_DateTime ts = DarraUa_DataValue_GetSourceTimestamp(values[i]);
double d = 0.0; DarraUa_Variant_GetDouble(v, &d);
printf(" ft=%lld v=%g\n", (long long)ts, d);
}
DarraUa_DataValueArray_Delete(values, count);
return 0;
}
错误码
服务端返回的 StatusCode 由 SDK 原样透传 (BadHistoryOperationUnsupported / BadNotSupported / BadNotReadable 等, 属于服务端侧码, 客户端头里没有对应宏). 客户端栈自己产生的错误走下列已定义宏:
| 状态码 | 含义 |
|---|---|
| UA_STATUS_GOOD | 成功 |
| UA_STATUS_BAD_SERVER_NOT_CONNECTED | 会话未连接 |
| UA_STATUS_INVALID_HANDLE | 会话句柄无效 |
| UA_STATUS_INVALID_ARGUMENT | 参数无效 (含 node_id_str 解析失败) |
| UA_STATUS_BAD_COMMUNICATION_ERROR | 网络故障 |
| UA_STATUS_BAD_TIMEOUT | 单次 RPC 超时 |
| UA_STATUS_BAD_ENCODING_ERROR / UA_STATUS_BAD_DECODING_ERROR | 编解码失败 |
返回值用 UA_STATUS_IS_GOOD / UA_STATUS_IS_UNCERTAIN / UA_STATUS_IS_BAD 判定严重性, DarraUa_StatusName 转英文短名打印.
性能与最佳实践
- 高频数据一天的原始点数量极大 → 必须用
num_values_per_node限制, 或改用 ReadProcessed 聚合 - 必须用
DarraUa_DataValueArray_Delete一次性释放, 不要循环单 Delete + free 数组 - 时间戳一律 UTC