The example below shows how to open a file stream for reading, and read its content using a text reader object, using OPC UA file provider model.
// Shows how to open a file stream for reading, and read its content using a text reader object, using OPC UA file provider // model. using System; using System.IO; using OpcLabs.BaseLib.Extensions.FileProviders; using OpcLabs.EasyOpc.UA; using OpcLabs.EasyOpc.UA.FileTransfer; namespace UADocExamples.FileProviders._FileInfo2 { class ReadText { 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(); Console.WriteLine("Getting file info..."); IFileInfo2 fileInfo2 = fileTransferClient.GetFileInfo2(endpointDescriptor, fileNodeDescriptor); // From this point onwards, the code is independent of the concrete realization of the file provider, and would // be identical e.g. for files in the physical file system, if the corresponding file provider was used. try { // Get a stream reader object that corresponds to an OPC UA file. Console.WriteLine("Opening the file for reading..."); // We know that the file contains text, so we read it using a stream reader. If the file content was // binary, you would process the stream according to the data format. using (StreamReader streamReader = fileInfo2.CreateStreamReader()) { // Read in the text from the file and display it line by line. Console.WriteLine(); Console.WriteLine("Reading text lines:"); int i = 0; while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); Console.WriteLine($"[{i}] {line}"); i++; } } } // Methods in the file provider model throw IOException and other exceptions, but not UAException. catch (Exception exception) { Console.WriteLine($"*** Failure: {exception.GetBaseException().Message}"); return; } Console.WriteLine(); Console.WriteLine("Finished..."); } } }
# Shows how to open a file stream for reading, and read its content using a text reader object, using OPC UA file provider # model. #requires -Version 5.1 using namespace System.IO using namespace OpcLabs.BaseLib.Extensions.FileProviders using namespace OpcLabs.EasyOpc.UA using namespace OpcLabs.EasyOpc.UA.FileTransfer # The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows . Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll" # 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. $fileTransferClient = New-Object EasyUAFileTransferClient Write-Host "Getting file info..." [IFileInfo2]$fileInfo2 = [IEasyUAFileTransferExtension]::GetFileInfo2($fileTransferClient, $endpointDescriptor, $fileNodeDescriptor) # From this point onwards, the code is independent of the concrete realization of the file provider, and would # be identical e.g. for files in the physical file system, if the corresponding file provider was used. try { # Get a stream reader object that corresponds to an OPC UA file. Write-Host "Opening the file for reading..." # We know that the file contains text, so we read it using a stream reader. If the file content was # binary, you would process the stream according to the data format. try { [StreamReader]$streamReader = [IFileInfoExtension]::CreateStreamReader($fileInfo2) # Read in the text from the file and display it line by line. Write-Host Write-Host "Reading text lines:" $i = 0 while (-not $streamReader.EndOfStream) { $line = $streamReader.ReadLine() Write-Host "[$($i)] $($line)" $i++ } } finally { if ($streamReader -ne $null) { $streamReader.Dispose() } } } # Methods in the file provider model throw IOException and other exceptions, but not UAException. catch [Exception] { Write-Host "*** Failure: $($PSItem.Exception.GetBaseException().Message)" return } Write-Host Write-Host "Finished."
# Shows how to open a file stream for reading, and read its content using a text reader object, using OPC UA file provider # model. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from System.IO import * from OpcLabs.BaseLib.Extensions.FileProviders import * from OpcLabs.EasyOpc.UA import * from OpcLabs.EasyOpc.UA.FileTransfer import * from OpcLabs.EasyOpc.UA.Navigation import * # Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe). endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030') # A node that represents an instance of OPC UA FileType object. fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile') # Instantiate the file transfer client object. fileTransferClient = EasyUAFileTransferClient() print('Getting file info......') fileInfo2 = IEasyUAFileTransferExtension.GetFileInfo2(fileTransferClient, endpointDescriptor, UANamedNodeDescriptor(fileNodeDescriptor)) # From this point onwards, the code is independent of the concrete realization of the file provider, and would # be identical e.g. for files in the physical file system, if the corresponding file provider was used. # Open the file, read two separate sections of it, and close it. streamReader = None try: # Get a stream reader object that corresponds to an OPC UA file. print('Opening the file for reading...') # We know that the file contains text, so we read it using a stream reader. If the file content was # binary, you would process the stream according to the data format. streamReader = IFileInfoExtension.CreateStreamReader(fileInfo2) # Read in the text from the file and display it line by line. print() print('Reading text lines...') i = 0 while not streamReader.EndOfStream: line = streamReader.ReadLine() print('[', i, '] ', line, sep='') i = i + 1 # Methods in the file provider model throw IOException and other exceptions, but not UAException. except Exception as exception: print('*** Failure: ' + exception.GetBaseException().Message) exit() finally: streamReader and streamReader.Dispose() print() print('Finished.')
' Shows how to open a file stream for reading, and read its content using a text reader object, using OPC UA file provider model. Imports System.IO Imports OpcLabs.BaseLib.Extensions.FileProviders Imports OpcLabs.EasyOpc.UA Imports OpcLabs.EasyOpc.UA.FileTransfer Namespace FileProviders._FileInfo2 Friend Class ReadText Public Shared Sub Main1() ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe) Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030" ' A node that represents an instance of OPC UA FileType object. Dim fileNodeDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile" ' Instantiate the file transfer client object Dim fileTransferClient = New EasyUAFileTransferClient Console.WriteLine("Getting file info...") Dim fileInfo2 As IFileInfo2 = fileTransferClient.GetFileInfo2(endpointDescriptor, fileNodeDescriptor) ' From this point onwards, the code is independent of the concrete realization of the file provider, and would ' be identical e.g. for files in the physical file system, if the corresponding file provider was used. Try ' Get a stream reader object that corresponds to an OPC UA file. Console.WriteLine("Opening the file for reading...") ' We know that the file contains text, so we read it using a stream reader. If the file content was ' binary, you would process the stream according to the data format. Using streamReader As StreamReader = fileInfo2.CreateStreamReader() ' Read in the text from the file and display it line by line. Console.WriteLine() Console.WriteLine("Reading text lines:") Dim i As Integer = 0 While Not streamReader.EndOfStream Dim line As String = streamReader.ReadLine() Console.WriteLine($"[{i}] {line}") i += 1 End While End Using ' Methods in the file provider model throw IOException and other exceptions, but not UAException. Catch exception As Exception Console.WriteLine("*** Failure: {0}", exception.GetBaseException.Message) Exit Sub End Try Console.WriteLine() Console.WriteLine("Finished...") End Sub End Class End Namespace
Copyright © 2004-2023 CODE Consulting and Development, s.r.o., Plzen. All rights reserved. Web page: www.opclabs.com
Send Documentation Feedback. Resources: Knowledge Base. Technical support: Online Forums, FAQ.