文件传输 (DarraUa_OpcUaFile_*)
概述
OPC UA Part 5 §10 FileType 暴露 6 个 Method (Open/Read/Write/Close/GetPosition/SetPosition)。SDK 封装为一组 DarraUa_OpcUaFile_* C API, 外加整文件 DownloadTo / UploadFrom 两个高层封装。
API
| 函数 | 类别 | 读写 | 说明 |
|---|---|---|---|
| DarraUa_OpcUaFile_New(session, file_node_id, &out_h) | 构造 | — | 绑定 FileType 实例, 不发 RPC |
| DarraUa_OpcUaFile_Free(h) | 释放 | — | 析构 (未关会自动 Close) |
| DarraUa_OpcUaFile_Size(h) | 属性 | 读 | 文件总字节数, 返回 uint64_t |
| DarraUa_OpcUaFile_Writable(h) | 属性 | 读 | 服务端层面是否可写, 返回 int32_t |
| DarraUa_OpcUaFile_UserWritable(h) | 属性 | 读 | 当前用户是否可写, 返回 int32_t |
| DarraUa_OpcUaFile_OpenCount(h) | 属性 | 读 | 当前打开句柄数, 返回 uint16_t |
| DarraUa_OpcUaFile_IsOpen(h) | 属性 | 读 | 是否处于 Open 状态, 返回 int32_t |
| DarraUa_OpcUaFile_Open(h, mode) | 方法 | 写 | 打开, mode 是位或的打开模式 |
| DarraUa_OpcUaFile_Read(h, length, &out_data, &out_n) | 方法 | 读 | 读 length 字节 |
| DarraUa_OpcUaFile_Write(h, data, length) | 方法 | 写 | 写到当前位置 |
| DarraUa_OpcUaFile_GetPosition(h, &out_pos) | 方法 | 读 | 当前位置, uint64_t |
| DarraUa_OpcUaFile_SetPosition(h, pos) | 方法 | 写 | 定位 |
| DarraUa_OpcUaFile_Close(h) | 方法 | 写 | 关闭 (未打开时静默 Good) |
| DarraUa_OpcUaFile_DownloadTo(h, local_path, progress, ctx) | 高层 | 读 | 一键下载 |
| DarraUa_OpcUaFile_UploadFrom(h, local_path, progress, ctx) | 高层 | 写 | 一键上传 |
相关结构:
typedef uint32_t DarraUa_OpcUaFileHandle;
#define UA_INVALID_OPCUA_FILE_HANDLE 0u
typedef enum {
UA_FILE_OPEN_READ = 0x01, /* 读 (与 WRITE 互斥) */
UA_FILE_OPEN_WRITE = 0x02, /* 写 */
UA_FILE_OPEN_ERASE_EXISTING = 0x04, /* 清空已有内容, 必须与 WRITE 同设 */
UA_FILE_OPEN_APPEND = 0x08 /* 定位到末尾追加 */
} DarraUa_FileOpenMode;
typedef void (*DarraUa_OnFileProgress)(
uint64_t done_bytes,
uint64_t total_bytes, /* 0 = server 没提供 Size */
void* user_context);
Read 的内存归属: out_data 是 SDK 堆分配的字节数组, 用 DarraUa_Free 释放 (不是 DataValue/Variant 那套 _Delete)。EOF 或失败时 *out_data == NULL, *out_actual_len == 0。
属性失败返回 0: Size / OpenCount / Writable / UserWritable 读取失败一律返回 0, 与"真的是 0"无法区分; 要区分请用 DarraUa_Session_ReadSafe 直接读对应子节点看 DataValue 的 status。
代码示例
#include <opcua.h>
#include <opcua_file_transfer.h>
#include <stdio.h>
static void on_progress(uint64_t done, uint64_t total, void* ctx) {
(void)ctx;
if (total == 0) { /* server 没给 Size */
printf("\r%llu bytes", (unsigned long long)done);
return;
}
printf("\r%llu%% (%llu/%llu)",
(unsigned long long)(done * 100 / total),
(unsigned long long)done,
(unsigned long long)total);
}
int main(void)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://device:4840";
/* ... DarraUa_Session_Create + DarraUa_Session_Connect 略 ... */
/* 1) 一键下载固件 */
DarraUa_OpcUaFileHandle file = UA_INVALID_OPCUA_FILE_HANDLE;
if (DarraUa_OpcUaFile_New(session, "ns=2;s=Firmware.Image", &file) != UA_STATUS_GOOD)
return 1;
printf("Size = %llu\n", (unsigned long long)DarraUa_OpcUaFile_Size(file));
DarraUa_Status rc = DarraUa_OpcUaFile_DownloadTo(file,
"C:\\dl\\firmware.bin", on_progress, NULL);
if (rc != UA_STATUS_GOOD) printf("\n下载失败 0x%08X\n", rc);
DarraUa_OpcUaFile_Free(file);
/* 2) 手动流式读 */
DarraUa_OpcUaFileHandle f3 = UA_INVALID_OPCUA_FILE_HANDLE;
DarraUa_OpcUaFile_New(session, "ns=2;s=Logs.Current", &f3);
DarraUa_OpcUaFile_Open(f3, UA_FILE_OPEN_READ);
for (;;) {
uint8_t* chunk = NULL;
size_t n = 0;
DarraUa_Status st = DarraUa_OpcUaFile_Read(f3, 4096, &chunk, &n);
if (st != UA_STATUS_GOOD || n == 0) {
DarraUa_Free(chunk); /* EOF/失败时 chunk 为 NULL, Free 安全 */
break;
}
process_chunk(chunk, n);
DarraUa_Free(chunk); /* Read 的缓冲用 DarraUa_Free 释放 */
}
DarraUa_OpcUaFile_Close(f3);
DarraUa_OpcUaFile_Free(f3);
/* 3) 上传配方 */
DarraUa_OpcUaFileHandle up = UA_INVALID_OPCUA_FILE_HANDLE;
DarraUa_OpcUaFile_New(session, "ns=2;s=Recipe.Current", &up);
DarraUa_OpcUaFile_UploadFrom(up, "C:\\recipes\\r01.csv", on_progress, NULL);
DarraUa_OpcUaFile_Free(up);
return 0;
}
实现说明
DownloadTo/UploadFrom内部用 4 KB 分块循环DownloadTo内部走Open(UA_FILE_OPEN_READ)→ 循环Read→ 写本地 →Close;UploadFrom走Open(UA_FILE_OPEN_WRITE | UA_FILE_OPEN_ERASE_EXISTING)→ 循环Write→Close- ByteString 走
uint8_t* + size_t一对参数, 调用方用DarraUa_Free释放 Read 出来的缓冲 - Method NodeId 解析: 优先 TranslateBrowsePaths 解析实例 Method NodeId, 失败回落到 OPC UA 标准 ns=0 类级 NodeId
New本身不发 RPC, Method 解析延迟到首次Open
最佳实践
- 始终在每个分支调
Free, C 没有 RAII - 大文件分段重试
- 不要并发同一 file handle
- 进度回调要处理
total_bytes == 0的情况
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | DarraUa_OpcUaFile_New/_Free/_Open/_Close |
| DownloadTo | download_to | downloadTo | DownloadTo | download_to | DarraUa_OpcUaFile_DownloadTo |
| UploadFrom | upload_from | uploadFrom | UploadFrom | upload_from | DarraUa_OpcUaFile_UploadFrom |