文件传输 (OpcUaFile)
概述
OPC UA Part 5 §10 定义 FileType (i=11575) 标准对象, 暴露 6 个 Method:
Open / Read / Write / Close / GetPosition / SetPosition。
OpcUaFile 提供面向流式调用的 .NET API, 适用场景:
- 固件升级 (PLC / IO / Drive 镜像下载)
- 配方 (Recipe) 上传 / 下载
- 任意文件镜像同步
对应规范段: Part 5 §10 (FileType)。
API
| 方法 / 属性 | 类别 | 读写 | 说明 |
|---|---|---|---|
| new OpcUaFile(ua, fileNodeId) | 构造 | — | 绑定到服务端某个 FileType 实例; 任一参数为空抛 ArgumentNullException |
| file.IsOpen | 属性 | 读 | 是否已 Open (bool) |
| file.FileHandle | 属性 | 读 | 服务端分配的句柄 (uint, 未 Open 时 0) |
| file.FileNodeId | 属性 | 读 | 构造时传入的实例 NodeId |
| file.Size | 属性 | 读 | 文件大小 (ulong); 读不到返回 0 |
| file.Writable | 属性 | 读 | 服务端层面是否可写 (bool); 读不到返回 false |
| file.UserWritable | 属性 | 读 | 当前用户是否可写 (bool, 受 ACL 影响); 读不到返回 false |
| file.OpenCount | 属性 | 读 | 当前打开句柄数 (ushort); 读不到返回 0 |
| file.Open(mode) | 方法 | 写 | 打开句柄, 返回 StatusCode; 成功后 FileHandle / IsOpen 才有效 |
| file.Close() | 方法 | 写 | 关闭句柄; 未打开时静默返回 Good (Dispose 会调) |
| file.Read(length) | 方法 | 读 | 读 N 字节, 返回 (StatusCode Status, byte[] Data); 空数组 = EOF |
| file.Write(bytes) | 方法 | 写 | 写入字节, 返回 StatusCode; null / 0 长度按 no-op 返回 Good |
| file.GetPosition() | 方法 | 读 | 返回 (StatusCode Status, ulong Position) |
| file.SetPosition(pos) | 方法 | 写 | 跳转文件指针, 返回 StatusCode |
| file.DownloadTo(localPath, progress) | 高层 | 读 | 一键下载到本地文件, 返回 StatusCode |
| file.UploadFrom(localPath, progress) | 高层 | 写 | 一键从本地文件上传, 返回 StatusCode |
完整签名:
public OpcUaFile(DarraOpcUa ua, string fileNodeId);
public bool IsOpen { get; }
public uint FileHandle { get; }
public string FileNodeId { get; }
public ulong Size { get; }
public ushort OpenCount { get; }
public bool Writable { get; }
public bool UserWritable { get; }
public StatusCode Open(FileOpenMode mode);
public StatusCode Close();
public (StatusCode Status, byte[] Data) Read(int length);
public StatusCode Write(byte[] data);
public (StatusCode Status, ulong Position) GetPosition();
public StatusCode SetPosition(ulong pos);
public StatusCode DownloadTo(string localPath, IProgress<(ulong done, ulong total)> progress = null);
public StatusCode UploadFrom(string localPath, IProgress<(ulong done, ulong total)> progress = null);
public void Dispose();
DownloadTo / UploadFrom 的 progress 是 IProgress<(ulong done, ulong total)>; total 可能是 0 (服务端没给 Size 时), 自己算百分比要先判零。
相关结构:
[Flags]
public enum FileOpenMode : byte
{
Read = 0x01, // 读
Write = 0x02, // 写
EraseExisting = 0x04, // 清空 (必须与 Write 同时设)
Append = 0x08 // 追加
}
UploadFrom 固定以 FileOpenMode.Write | FileOpenMode.EraseExisting 打开 —— 上传会清空服务端原文件内容。
代码示例
using System;
using DarraOpcUa_Client;
using var ua = new DarraOpcUa("opc.tcp://device:4840");
ua.Connect();
// 1) 一键下载固件
using (var file = new OpcUaFile(ua, "ns=2;s=Firmware.Image"))
{
Console.WriteLine($"Size = {file.Size} bytes");
var rc = file.DownloadTo(@"C:\dl\firmware.bin",
new Progress<(ulong done, ulong total)>(p =>
{
// total 可能是 0 (服务端没给 Size), 必须先判零
double pct = p.total == 0 ? 0 : (double)p.done * 100 / p.total;
Console.Write($"\r{pct:F0}% ({p.done}/{p.total})");
}));
if (rc != StatusCode.Good) Console.WriteLine($"\n下载失败: {rc}");
}
// 2) 一键上传配方 (Write|EraseExisting 打开, 会清空服务端原内容)
using (var f2 = new OpcUaFile(ua, "ns=2;s=Recipe.Current"))
{
var rc = f2.UploadFrom(@"C:\recipes\new.csv");
if (rc != StatusCode.Good) Console.WriteLine($"上传失败: {rc}");
}
// 3) 手动流式读 (大文件分块自定义处理)
using (var f3 = new OpcUaFile(ua, "ns=2;s=Logs.Current"))
{
var rcOpen = f3.Open(FileOpenMode.Read);
if (rcOpen == StatusCode.Good)
{
while (true)
{
var (st, chunk) = f3.Read(4096);
if (st != StatusCode.Good || chunk == null || chunk.Length == 0) break;
ProcessChunk(chunk); // 应用侧自己的处理函数
}
f3.Close();
}
}
错误处理
OpcUaFile 的方法不抛 OpcUaException —— 异常一律折叠成 StatusCode 返回。业务级与 transport 级失败都是返回值:
- 入参非法 / 未 Open → 返回 raw
0x80AB0000(BadInvalidArgument) - 其它异常 → 返回 raw
0x80020000(BadInternalError) - 服务端拒绝 → 原样返回服务端的
StatusCode
唯一会抛的异常是构造期的 ArgumentNullException (ua 或 fileNodeId 为空)。
两个 raw 码不在
StatusCode 枚举里BadInvalidArgument (0x80AB0000) 与 BadInternalError (0x80020000) 在 StatusCode 枚举里没有对应成员。比较时写 (StatusCode)0x80AB0000u, 不要写 StatusCode.BadInvalidArgument (编译不过)。
using 退出 / 异常路径 → 自动 Close 已打开的句柄 (实现 IDisposable, 幂等)。
实现说明
- 块大小默认 4 KB (大多数 Server 限 8 KB 以内, 留余量)
- ByteString 无损传输, 保留 0x00-0xFF 全字节
- Method 定位: 自动解析文件实例的 Method; 服务端只暴露标准 FileType 类级 Method 时同样可用
DownloadTo以“单次读回不足一个块 (默认 4 KB)”判 EOF —— 服务端若在文件中间返回短读, 会被当成 EOF 提前截断。要下大文件, 建议自己用Open(Read)+Read(4096)+Size对账, 或按SetPosition分段
最佳实践
- 始终用
using— 漏 Close 会让服务端句柄表泄漏 - 进度回调切 UI 线程 —
Progress<T>默认捕获当前 SynchronizationContext, WPF/WinForms 调用方在 UI 线程构造即可 - 大文件考虑分段重试 — 上层在每个 4KB 块边界做 checkpoint, 网络断后续传
- 不要并发同一 fileHandle — 一个 Open 对应一个串行流
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | DarraUa_OpcUaFile_* |
| DownloadTo | download_to | downloadTo | DownloadTo | download_to | DarraUa_OpcUaFile_DownloadTo |
| UploadFrom | upload_from | uploadFrom | UploadFrom | upload_from | DarraUa_OpcUaFile_UploadFrom |