OPC Studio User's Guide and Reference
ReadFile Method (IEasyUAFileTransfer)
Example 



View with Navigation Tools
OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA.FileTransfer Namespace > IEasyUAFileTransfer Interface : ReadFile Method
Endpoint descriptor. Identifies the OPC-UA server.
Node descriptor of the OPC UA file.
A handle indicating the access request and thus indirectly the position inside the file.
Defines the length in bytes that should be returned, starting from the current position of the file handle. If the end of file is reached, all data until the end of the file is returned. The server is allowed to return fewer data than specified length. Only positive values are allowed.
Reads a part of the file, starting from the current file position.
Syntax
'Declaration
 
<NotNullAttribute()>
Function ReadFile( _
   ByVal endpointDescriptor As UAEndpointDescriptor, _
   ByVal fileNodeDescriptor As UANodeDescriptor, _
   ByVal fileHandle As UAFileHandle, _
   ByVal length As Integer _
) As Byte()
 
'Usage
 
Dim instance As IEasyUAFileTransfer
Dim endpointDescriptor As UAEndpointDescriptor
Dim fileNodeDescriptor As UANodeDescriptor
Dim fileHandle As UAFileHandle
Dim length As Integer
Dim value() As Byte
 
value = instance.ReadFile(endpointDescriptor, fileNodeDescriptor, fileHandle, length)

Parameters

endpointDescriptor
Endpoint descriptor. Identifies the OPC-UA server.
fileNodeDescriptor
Node descriptor of the OPC UA file.
fileHandle
A handle indicating the access request and thus indirectly the position inside the file.
length
Defines the length in bytes that should be returned, starting from the current position of the file handle. If the end of file is reached, all data until the end of the file is returned. The server is allowed to return fewer data than specified length. Only positive values are allowed.

Return Value

Returns the data read from the file. If the returned array is empty (zero length), it indicates that the end of the file is reached.
Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

The value of an argument is outside the allowable range of values as defined by the invoked method.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

The OPC UA operation has failed. This operation exception in uniformly used to allow common handling of various kinds of errors. The System.Exception.InnerException always contains information about the actual error cause.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

Remarks

This method corresponds to (but is not fully identical with) the OPC UA File Transfer Method .

The fileNodeDescriptor must be an existing node in the server, of object type OpcLabs.EasyOpc.UA.AddressSpace.Standard.UAObjectTypeIds.FileType.

The file position is advanced by the number of bytes read.

Recommendation: Whenever possible, do not use this method directly, and use a higher-level abstraction instead. You can create System.IO.Stream objects for accessing the file data by using methods like OpcLabs.EasyOpc.UA.IO.Extensions.IEasyUAFileTransferExtension2.OpenOrCreateStream, or obtain them through a file provider (using IEasyUAFileTransferExtension.GetFileProvider2 or IEasyUAFileTransferExtension.GetWritableFileProvider).

Example

.NET

// Shows how to read different sections from an OPC UA file, using the file transfer client.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples.FileTransfer._EasyUAFileTransferClient
{
    class ReadAndSetFilePosition
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            // Open the file, read two separate sections of it, and close it.
            try
            {
                Console.WriteLine("Opening file...");
                using (UAFileHandle fileHandle = fileTransferClient.OpenFile(endpointDescriptor, fileNodeDescriptor, UAOpenFileModes.Read))
                {
                    Console.WriteLine("Reading first file section...");
                    byte[] bytes1 = fileTransferClient.ReadFile(endpointDescriptor, fileNodeDescriptor, fileHandle, length:16);
                    Console.WriteLine($"First section: {BitConverter.ToString(bytes1)}");

                    Console.WriteLine("Reading second file section...");
                    byte[] bytes2 = fileTransferClient.ReadFile(endpointDescriptor, fileNodeDescriptor, fileHandle, length: 10);
                    Console.WriteLine($"Second section: {BitConverter.ToString(bytes2)}");

                    Console.WriteLine("Setting file position...");
                    fileTransferClient.SetFilePosition(endpointDescriptor, fileNodeDescriptor, fileHandle, position:100);

                    Console.WriteLine("Reading third file section...");
                    byte[] bytes3 = fileTransferClient.ReadFile(endpointDescriptor, fileNodeDescriptor, fileHandle, length: 20);
                    Console.WriteLine($"Third section: {BitConverter.ToString(bytes3)}");

                    Console.WriteLine("Closing file...");
                    fileTransferClient.CloseFile(endpointDescriptor, fileNodeDescriptor, fileHandle);
                }
            }
            catch (UAException uaException)
            {
                Console.WriteLine("*** Failure: {0}", uaException.GetBaseException().Message);
                return;
            }

            Console.WriteLine("Finished...");
        }
    }
}
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also