Skip to main content

File Transfer (OpcUaFile)

Overview​

OPC UA Part 5 §10 FileType (i=11575) exposes 6 methods. The SDK wraps them as an OpcUaFile class.

API​

OpcUaFile It is a class (file_transfer.hpp) in the namespace darra::opcua; is passed during construction Session&.Move-only, Automatic destructor/exception handling Close().

MethodsCategoryReading and WritingNote
OpcUaFile(Session& s, std::string file_node_id)Structure—Bind a File object to the server
file.IsOpen()MethodsReadIs it currently open?
file.FileHandle()MethodsReadFile handle returned by the server uint32_t
file.FileNodeId()MethodsReadstd::string const&
file.Size()MethodsReadFile size uint64_t
file.OpenCount()MethodsReadCurrent number of opens uint16_t
file.Writable()MethodsReadIs it possible to write?
file.UserWritable()MethodsReadThe current user has write access
file.Open(mode)MethodsWriteOpen, Returns Status
file.Read(length)MethodsReadReturns std::pair<Status, std::vector<uint8_t>>
file.Write(bytes)MethodsWriteReturns Status
file.GetPosition()MethodsReadReturns std::pair<Status, uint64_t>
file.SetPosition(pos)MethodsWriteReturns Status
file.Close()MethodsWriteClose (Destructor called automatically)
file.DownloadTo(local_path, progress)High-levelReadDownload with one click, Returns Status
file.UploadFrom(local_path, progress)High-levelWriteUpload with one click, then return Status

All of the above areMember Functions, not a property field.ProgressFn = std::function<void(uint64_t done, uint64_t total)>, total == 0 indicates that the total size is unknown.

Related Structures (FileOpenMode flags,operator| combinable):

  • Read (0x01) — Read
  • Write (0x02) — Write
  • EraseExisting (0x04) — Clear
  • Append (0x08) — Append

Semantics of Method Failures:

CallWhen closedOther
Open(mode)—If it's already open, just returns Status::DarraInvalidArgument; don't open it again.
Close()Silent Returns Status::GoodOnce the adjustment is complete, it is considered closed.
Read(length){Status::DarraInvalidArgument, {}}length <= 0 →; {Status::Good, {}} ReturnsEmpty data = Reached the end; If the operation fails, the data is empty
Write(bytes)Status::DarraInvalidArgumentdata If it's empty, return immediately Status::Good
GetPosition() / SetPosition(){DarraInvalidArgument, 0} / DarraInvalidArgument
DownloadTo / UploadFrom—local_path Returns null Status::DarraInvalidArgument

Code Example​

#include <darra/opcua/client.hpp>
#include <darra/opcua/file_transfer.hpp>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

using namespace darra::opcua;

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

// 1) One-click firmware download (RAII Close "Management")
{
OpcUaFile file(s, "ns=2;s=Firmware.Image");
std::cout << "Size = " << file.Size() << "\n";

auto progress = [](uint64_t done, uint64_t total) {
if (total == 0) return; // total=0 indicates unknown
std::cout << "\r" << (done * 100 / total) << "% ("
<< done << "/" << total << ")" << std::flush;
};
Status rc = file.DownloadTo("C:/dl/firmware.bin", progress);
if (rc != Status::Good)
std::cerr << "\n下载失败: " << Stack::StatusName(static_cast<uint32_t>(rc)) << "\n";
}

// 2) Upload recipes with one click
{
OpcUaFile f2(s, "ns=2;s=Recipe.Current");
Status rc = f2.UploadFrom("C:/recipes/new.csv");
}

// 3) Manual streaming read
{
OpcUaFile f3(s, "ns=2;s=Logs.Current");
if (f3.Open(FileOpenMode::Read) != Status::Good) return;
while (true) {
std::pair<Status, std::vector<uint8_t>> r = f3.Read(4096);
if (r.first != Status::Good || r.second.empty()) break; // Empty = EOF
ProcessChunk(r.second);
}
f3.Close();
}

Implementation Notes​

  • Default block size 4 KB,DownloadTo In the short version, read (insufficient 4 KB) as "ended EOF early"
  • ByteString Coded as ISO-8859-1(Follows the same conventions as the C# side), preserves all 0x00-0xFF bytes without loss; etc. — expose Variant them first, SetByteString then slice them
  • Method NodeId resolution: first use TranslateBrowsePaths to find the instance Method NodeId; on failure, fall back to the ns=0 class-level NodeId (Open i=11580 / Read i=11581 / Write i=11582 / Close i=11583 / GetPosition i=11588 / SetPosition i=11591)

Best Practices​

  • Use RAII to manage the lifecycle; destruction is automatic Close()
  • Retrying in Segments for Large Files
  • Do not use the same one concurrently file_handle

Cross-Language Comparison​

C#PythonJavaC++RustC
OpcUaFileOpcUaFileOpcUaFileOpcUaFileOpcUaFileDarraUa_OpcUaFile_New / _Free
DownloadTodownload_todownloadToDownloadTodownload_toDarraUa_OpcUaFile_DownloadTo
UploadFromupload_fromuploadFromUploadFromupload_fromDarraUa_OpcUaFile_UploadFrom

C's family name is DarraUa_OpcUaFile_*, No DarraUa_File_*. Handle type DarraUa_OpcUaFileHandle, progress callback DarraUa_OnFileProgress, mode enumeration DarraUa_FileOpenMode. Complete set of methods:_New / _Free / _Open / _Close / _Read / _Write / _GetPosition / _SetPosition / _Size / _OpenCount / _Writable / _UserWritable / _IsOpen / _DownloadTo / _UploadFrom.

Next Step​