FB_FileWrite 1:

The function block writes data into a file. For write access the file must have been opened in the corresponding mode, and it must be closed again for further processing by external programs.


    sNetId     : T_AmsNetId;
    hFile      : UINT;
    pWriteBuff : PVOID;
    cbWriteLen : UDINT;
    bExecute   : BOOL;
    tTimeout   : TIME := DEFAULT_ADS_TIMEOUT;

sNetId: String containing the AMS network identifier of the target device to which the ADS command is directed (type: T_AmsNetId).

hFile: File handle, which was generated when the function block FB_FileOpen was created.

pWriteBuff: Address of the buffer containing the data to be written. The buffer can be a single variable, an array or a structure, whose address can be determined with the ADR operator.

cbWriteLen: Number of bytes to be written.

bExecute: The function block is activated by a rising edge at this input.

tTimeout: States the length of the timeout that may not be exceeded by execution of the ADS command.


    bBusy    : BOOL;
    bError   : BOOL;
    nErrId   : UDINT;
    cbWrite  : UDINT;

bBusy: When the function block is activated, this output is set to TRUE and remains set until feedback is received. As long as bBusy is TRUE, no new command can be executed.

bError: If an error occurs during command execution, this output is set, once the bBusy output has been reset.

nErrId: Returns the ADS error code or the command-specific error code when the bError output is set.

cbWrite: Contains the number of the last successfully written data bytes. A write error indicates that the number of successfully written data bytes is less than the requested length (cbWriteLen) or zero. A write error can occur if the data storage device is full, for example. If a write error occurs, the bError and nErrID outputs are not set. Since the PLC application knows the number of data bytes to be written, it can compare the actual written length with the requested length and detect write errors. When a write error occurs, the internal file pointer has an undefined position.

Command-specific error code

Possible cause


Invalid or unknown file handle.


File was opened with wrong method (e.g. with obsolete FILEOPEN function block).

Example of calling the block in ST:

Example of calling the block in FBD:

    fbFileWrite      : FB_FileWrite;
    hFile            : UINT;
    bFileWrite       : BOOL;
    bFileWriteBusy   : BOOL;
    bFileWriteError  : BOOL;
    nFileWriteErrorId: UDINT;
    nFileWriteCount  : UDINT;
    fileData         : ARRAY[0..9] OF BYTE;
FB_FileWrite 2:

In the example, after a rising edge at bFileWrite, 10 bytes of the array FILEDATA are written to the file.


Development environment

Target system type

PLC libraries to include (Category group)

TwinCAT v3.1.0

PC or CX (x86, x64, ARM)

Tc2_System (System)