FB_FileOpen
With this function block a new file can be created, or a closed existing file can be re-opened for further processing.
VAR_INPUT
VAR_INPUT
sNetId : T_AmsNetId;
sPathName : T_MaxString;
nMode : DWORD;
ePath : E_OpenPath := PATH_GENERIC;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
sNetId : is a string containing the AMS network identifier of the target device to which the ADS command is directed.
sPathName : contains the path and file name of the file to be opened.
![]() | The path can only point to the local computer’s file system. This means that network paths cannot be used here! |
nMode : contains the mode in which the file is to be opened. The codes listed below are the various opening modes which are already pre-defined as constants in the library and which can accordingly be passed symbolically to the function block. The opening modes can be ORed. The opening modes can be combined, similar to the opening modes of the fopen function in C or C++.
Modes | Description |
---|---|
FOPEN_MODEREAD | "r": opens a file for reading. An error is returned if the file cannot be found or does not exist. |
FOPEN_MODEWRITE | "w": opens an empty file for writing. If the file already exists, it is overwritten. |
FOPEN_MODEAPPEND | "a": opens a file for writing at the end of the file (append). If the file does not exist, a new file is created. |
FOPEN_MODEREAD OR FOPEN_MODEPLUS | "r+": opens a file for reading and writing. The file must exist. |
FOPEN_MODEWRITE OR FOPEN_MODEPLUS | "w+": opens an empty file for reading and writing. If the file already exists, it is overwritten. |
FOPEN_MODEAPPEND OR FOPEN_MODEPLUS | "a+": opens a file for reading and writing at the end of the file (append). If the file does not exist, a new file is created. For this, the memory path must be known, otherwise error 1804 appears. All write operations are always performed at end of a file, if the file was opened in the modes "a" or "a+". The file pointer can be moved with FB_FileSeek, although for writing it is moved to the end of the file by default, i.e. existing data cannot be overwritten. |
FOPEN_MODEBINARY | "b": opens the file in binary mode |
FOPEN_MODETEXT | "t": opens the file in text mode |
ePath : this input can be used to select a TwinCAT system path on the target device for opening the file.
bExecute : the function block is enabled by a rising edge at this input.
tTimeout : states the timeout period that must not be exceeded when executing the ADS command.
Function specific ADS error code | Possible cause |
---|---|
0x703 | Unknown or invalid nMode or ePath parameter. |
0x70C | File not found. Invalid file name or file path. |
0x716 | No further free file handles. |
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
hFile : UINT; (* file handle *)
END_VAR
bBusy: This output remains TRUE until the block has executed a command, but at the longest for the duration supplied to the ‘tTimeout’ input. While bBusy = TRUE, no new command will be accepted at the inputs. Please note that it is not the execution of the service but its acceptance whose time is monitored.
bError: This output is switched to TRUE if an error occurs during the execution of a command. The command-specific error code is contained in ‘nErrId’.
nErrId: Contains the command-specific ADS error code of the most recently executed command.
hFile: Contains the file handle created for the file when opening has been successful.
Function specific ADS error code | Possible reason |
---|---|
0x703 | Unknown or invalid nMode or ePath parameter. |
0x70C | File not found. Invalid file name or file path. |
0x716 | No more free file handles. |
Example of calling the block in FBD:
PROGRAM Test
VAR
fbFileOpen : FB_FileOpen;
bFileOpen : BOOL;
bFileOpenBusy : BOOL;
bFileOpenError : BOOL;
nFileOpenErrId : UDINT;
hFile : UINT;
END_VAR
This should create (or overwrite) the file ”TestFile2.txt" in the root directory of drive ”C:" in the text mode.
![]() | For the opening mode a maximum of 3 parameters may be ORed: |
Parameter1 may have only one subordinate value:
- FOPEN_MODEREAD
- FOPEN_MODEWRITE
- FOPEN_MODEAPPEND
Parameter2 may have only one subordinate value:
- FOPEN_MODEPLUS
Parameter3 may have only one subordinate value:
- FOPEN_MODEBINARY
- FOPEN_MODETEXT
If no binary or text mode is specified, the file opens in a mode defined by an operating system variable. In most cases, the file will then open in text mode. However, it is not possible to make a clear statement. It is useful to always specify the text or binary mode. This system variable cannot be changed in the PLC!
This results in the following permissible combinations:
Text file opening modes | Binary file opening modes |
---|---|
FOPEN_MODEREAD OR FOPEN_MODETEXT | FOPEN_MODEREAD OR FOPEN_MODEBINARY |
FOPEN_MODEWRITE OR FOPEN_MODETEXT | FOPEN_MODEWRITE OR FOPEN_MODEBINARY |
FOPEN_MODEAPPEND OR FOPEN_MODETEXT | FOPEN_MODEAPPEND OR FOPEN_MODEBINARY |
FOPEN_MODEREAD OR FOPEN_MODEPLUS OR FOPEN_MODETEXT | FOPEN_MODEREAD OR FOPEN_MODEPLUS OR FOPEN_MODEBINARY |
FOPEN_MODEWRITE OR FOPEN_MODEPLUS OR FOPEN_MODETEXT | FOPEN_MODEWRITE OR FOPEN_MODEPLUS OR FOPEN_MODEBINARY |
FOPEN_MODEAPPEND OR FOPEN_MODEPLUS OR FOPEN_MODETEXT | FOPEN_MODEAPPEND OR FOPEN_MODEPLUS OR FOPEN_MODEBINARY |
All other combinations are wrong. Examples of invalid opening modes:
FOPEN_MODEBINARY OR FOPEN_MODETEXT
FOPEN_MODEWRITE OR FOPEN_MODEAPPEND
Requirements
Development environment | Target system type | PLC libraries to include |
---|---|---|
TwinCAT v2.8.0 | PC or CX (x86) | TcSystem.Lib |
TwinCAT v2.10.0 Build >= 1301 | CX (ARM) | TcSystem.Lib |