FB_FileRead 1:

With this function block the contents of an already opened file can be read. Before a read access, the file must have been opened in the corresponding mode. In addition to FOPEN_MODEREAD, the desired format (FOPEN_MODEBINARY or FOPEN_MODETEXT) is also important for achieving the desired result.


    sNetId     : T_AmsNetId;
    hFile      : UINT;
    pReadBuff  : PVOID;
    cbReadLen  : 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.

pReadBuff: Address of the buffer into which the data are to be read. The buffer can be a single variable, an array or a structure, whose address can be determined with the ADR operator.

cbReadLen: Number of bytes to be read.

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;
    cbRead : UDINT;    
    bEOF   : BOOL;

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.

cbRead: Contains the number of bytes currently read.

bEOF: This output is set if the end of the file was reached and no further data bytes could be read (cbRead=0). This output is not set if further data bytes could be read (cbRead>0).

Command-specific error code

Possible cause


Invalid or unknown file handle.


No memory for read buffer.


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

Example of calling the block in ST:

Example of calling the function block in FBD:

    fbFileRead        : FB_FileRead;
    hFile             : UINT;
    bFileRead         : BOOL;
    bFileReadBusy     : BOOL;
    bFileReadError    : BOOL;
    nFileReadErrorId  : UDINT;
    nFileReadCount    : UDINT;
    bFileReadEOF      : BOOL;
    fileData          : ARRAY[0..9] OF BYTE;
FB_FileRead 2:

After a rising edge at bExecute and successful execution of the read instruction the currently read bytes from the file are found in FILEDATA. The parameter cbRead can be used to determine how many bytes were actually read during the last read operation.


Development environment

Target system type

PLC libraries to include (Category group)

TwinCAT v3.1.0

PC or CX (x86, x64, ARM)

Tc2_System (System)