Using OpcCmd Utility for OPC UA File Transfer

From OPC Labs Knowledge Base
Revision as of 12:18, 18 May 2021 by User (talk | contribs)
Jump to navigation Jump to search

For general information about the OpcCmd tool, see Category:OpcCmd Utility.

Tutorial

If you execute the commands listed in the tutorial below, you will get an overview of basic operations that can be made with an OPC UA Server that provides OPC UA File Transfer model:

  • Accessing a standalone OPC UA file node, and accessing an OPC UA file system.
  • Getting file properties (metadata).
  • Saving file contents to a local operating system file.
  • Loading file contents from a local operating system file.
  • Clearing directory contents.
  • Creating a subdirectory in the OPC UA file system.
  • Copying files in the OPC UA file system.
  • Displaying contents of a directory in OPC UA file system in a table or as a tree.
  • Deleting a file from OPC UA file system.

This tutorial works with Unified Automation UA .NET SDK Bundle server. You should be able to use the principles explained in this tutorial with other OPC UA servers that provide the File Transfer model, by modifying the relevant parameters used in the commands.


  1. Start the Unified Automation UA .NET SDK Bundle server, by typing start UaServerNET.exe at the operating system command prompt, when in the 'bin' directory of the Unified Automation UA .NET SDK Bundle.

  2. Start the OpcCmd utility in interactive mode. The interactive mode is always in effect e.g. when you use ClickOnce, or when running the utility from QuickOPC Launcher/Start menu. In other cases, you need to use the -i (or --interactive) option on the command line.

  3. At the OpcCmd> prompt, enter uaFileTransferClient, or shortened: uaftc. This command tells the utility that from now on, we will be accessing the File Transfer functionality of OPC UA. The programs responds with a confirmation of the command.

    Subsequent commands are entered at the uaFileTransferClient> prompt. You can enter -?, -h or --help (possibly preceded by the command name) at any time to obtain help for this prompt (or its commands).

  4. In order to access a file that is represented by a standalone node in the server (i.e. not part of the OPC UA file system), enter getFileInfo opc.tcp://localhost:48030 "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile" --readOnly (getFileInfo can be shortened to gfi, and --readOnly can be shortened to -ro). The node Id used in this command refers to a predefined node in the server which contains a demo text file. The --readOnly option tells OpcCmd that we do not intend to make (and do not want to allow) any modification to the file metadata or contents inside the OPC UA server (this is optional).
    Subsequent commands are entered at the fileInfo> prompt.

  5. Display the file properties by entering get.

  6. The OpcCmd Utility will now ask you whether you want the client to trust the server certificate. Verify that that you have been presented with the right server server certificate, and if so, accept the certificate by pressing Y for "Yes".
    After the certificate is accepted, the get command proceeds. If OpcCmd is set to the "OPC compliance" mode, in which the secure connections are the default, and this is the first operation made from this client on the server, you will most likely receive file properties where the relevant values (such as file length, and last modified date) will be missing. This is caused by the fact that the server does not trust the client. In order to establish the trust:
    1. The Unified Automation UA .NET SDK Bundle server will show the rejected client certificate on the "Rejected Certificates" tab of its window. On this tab, locate the rejected OpcCmd application certificate, right-click on it and select "Trust Permanently"; when asked, confirm the operation.
    2. Enter the get command again.

      OpcCmd-fileInfo-get.png

  7. Save the contents of the OPC UA file to a file in your local file system by entering saveTo --osFilePath c:\work\file1.txt, or shortened: st -os c:\work\file1.txt. You can replace the file path (for your operating in system) in the command with a different one, as you need. The directory where you want the file be placed must already exist. Note that an existing file on the disk, if it exists, will be overwritten without confirmation. After the operation is finished, you can use the means of your operating system to view the file.

  8. Enter exit to return to the uaFileTransferClient> prompt.

  9. In order to access the file system in the OPC UA server (a hierarchical structure consisting of file and directories), enter getDirectoryContents opc.tcp://localhost:48030 "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.FileSystem" --EndpointUser john:master (getDirectoryContents can be shortened to gdc, and --EndpointUser can be shortened to -eu). The node Id used in this command refers to a predefined node in the server which contains a root of the file system (different servers will use a different node, and there can be multiple file system in an OPC UA server). This command also specifies user name and password, because we will be doing modifications to the file system, and the server requires user authentication for such operations.
    Subsequent commands are entered at the directoryContents> prompt.

  10. In order to remove all files and directories from the file system, enter clear or clr. Caution: Before executing the command, make sure the file system does not contain any information you may later need. This step is included in the tutorial so that the file system starts in a defined state, and we can be sure e.g. that the commands will not collide with files or directories created in previous executions of the tutorial. You can skip this step if you can assure that such conflicts won't arise. The command will ask for confirmation; press Y for "Yes".

  11. Create a file named 'MyTestFile.txt' in the root directory of the OPC UA file system and load it with the data from the existing file that we have previously saved to the disk, by entering fileInfo MyTestFile.txt loadFrom --osFilePath c:\work\file1.txt, or shortened: fi MyTestFile.txt lf -os c:\work\file1.txt. Use the path to your local file that you have used in the tutorial step with the saveTo command.

  12. To display the list of files and directories under the current directory (file system root), enter fileInfos?, or shortened: fi?. The table will contain the 'MyTestFile.txt' file that we have created in the previous step.
    OpcCmd-directoryContents-fileInfosQ.png

  13. Create a subdirectory named 'MyTestDirectory' in the root directory, by entering createSubdirectory MyTestDirectory, or shortened: cs MyTestDirectory.

  14. Copy the file 'MyTestFile.txt' from the root directory to the 'MyTestDirectory' directory and give it a name 'CopyOfMyTestFile.txt', by entering fileInfo MyTestFile.txt copyTo MyTestDirectory/CopyOfMyTestFile.txt, or shortened: fi MyTestFile.txt ct MyTestDirectory/CopyOfMyTestFile.txt.

  15. To display a tree of the objects currently in the file system, enter browseTree or bt. At this point, the tree will contain the original 'MyTestFile.txt' file created, and a 'MyTestDirectory' subdirectory with the file copy, 'CopyOfMyTestFile.txt'.
    OpcCmd-directoryContents-browseTree.png

  16. In order to delete the 'CopyOfMyTestFile.txt' file from 'MyTestDirectory', enter fileInfo MyTestDirectory/CopyOfMyTestFile.txt delete, or shortened: fi MyTestDirectory/CopyOfMyTestFile.txt d.

  17. Enter browseTree or bt again, and verify that the file from the previous step has been deleted.

Other Operations

Other OPC UA File Transfer operations that can be made using OpcCmd, but not shown in this tutorial, include:

  • Moving and renaming files.
  • Refreshig file metadata information.
  • Accessing subdirectories.
  • Deleting directories.
  • Moving and renaming directories.
  • Saving contents of whole directory trees to the local file system.
  • Loading contents of whole directory trees from the local file system.

Comparison with Windows and Linux commands

(TBD)

Operation Windows command
(CMD)
Linux command OpcCmd command
(directoryContents prompt)
Change current directory chdir <path> (tbd) directoryContents <path>
Copy a file copy <source> <destination> (tbd) fileInfo <source> copyTo <destination>
Delete a file erase <fileName> (tbd) fileInfo <fileName> delete
Display current directory contents dir (tbd) filesInfos?
Display specified directory contents dir <path> (tbd) directoryContents <path> filesInfos?
Move a file move <source> <destination> (tbd) fileInfo <source> moveTo <destination>
Obtain file information (tbd) (tbd) fileInfo <fileName> get
Rename a file rename <oldFileName> <newFileName> (tbd) fileInfo <oldFileName> rename <newFileName>