Skip to main content

Example: File Utilities

Product: CODESYS Control

This example shows how you can read and write files and work with directories. Synchronous and asynchronous function calls are used in the example.

_example_img_file_utilities.png

Description

The example shows different functions for file management, such as creating files and directories, renaming, deleting, and copying them, as well as reading and writing files. In addition, a directory can be browsed to find all included files.

When you use a synchronous function (for example, SysFileOpen), the function will wait until a result is returned. Sometimes this lasts longer than one cycle. When you use an asynchronous call (for example, the Open function block of the CAA_File library), the function block will not wait for a result. At each call, the function block asks again for a result. If the result is available, then the xDone variable is set to TRUE; otherwise it remains FALSE until a result is available.

Additional information

SystemDir: The program uses several synchronous functions to create a directory. The directory is browsed to find all included files. After renaming, the directory is deleted.

SystemFile: The program uses several functions for file modification. First, the file is opened with write access. After that, a string is written to the file and the file is closed again.

In the next step, the file is opened again and the written string is read. After copying the file, a new string is appended at the end. Some file information, such as creation time and size, is also fetched.

CAA_Dir: The program shows how to create, open, close, rename, browse, and remove a directory. This is done asynchronously using a state machine. When the function block is done or sends an error, the Next or Error state is reached.

CAA_File: This program performs some file modifications asynchronously. After opening the file, a string is written to this file and is read again later. File information is fetched and another string is appended to the file.

A function can be executed via the visualization.

System requirements and restrictions

Programming system

CODESYS Development System (version 3.5.14.0 or higher)

Runtime system

CODESYS Control Win (version 3.5.14.0)

Add-on components

-

Note

_example_icon.png DOWNLOAD Project