-

FILE_READ_2

Type Function block
Description Reads data from a file that has been opened using the FILE_OPEN_2 function block.

Assign the valid handle for the file from which data is to be read to the HANDLE input.

Create a data buffer of sufficient size for the BUFFER output. The data buffer for the data to be read can be declared in a number of ways. The type of data buffer can be defined by the user, e.g., a byte array (see the following example). Strings cannot be used directly as data buffers. If the data read is to be processed later as a string, it must be saved first in an array and then converted into a string using the BUF_TO_STRING function block.

Example: Byte array used as data buffer type

Enter the number of bytes to be read at the MAX_LENGTH input.

As soon as the REQ input is set to TRUE, the inputs are validated. The REQ input must remain set to TRUE, until the file read operation is completed (regardless of whether the operation succeeded or failed). The BUSY output is TRUE while the operation is being executed asynchronously on a Service Thread. Execution of the function block is complete when the DONE output is set to TRUE.

If an error occurred during execution, ERROR is set to TRUE, and the error code can be read from the STATUS output.

Following successful execution, the data is copied to the buffer and the number of bytes read is indicated at the LENGTH_READ output. The number of bytes read may be smaller than MAX_LENGTH if the end of the file was reached before the number of bytes indicated was processed. If no more data can be read (e.g., the value in MAX_LENGTH is greater than the size of the remaining file data), then the EOF output will be set to TRUE.
Notes
  • The status of the outputs DONE, BUSY, LENGTH_READ, ERROR, and STATUS are maintained as long as REQ is TRUE.

    Do not reset the REQ input until the FB is finished by setting the DONE state. Resetting the REQ input will result in resetting the outputs to their default values.

  • The connected buffer is used for asynchronous execution. No copy of the data within the buffer is created.
  • When the function block is used as global function block, it is not thread-safe. In this case, make sure to prevent concurrent calls (e.g., by using the MUTEX function block).
  • Function blocks have to be instantiated. The instance name of the function block has to be declared in the 'Variables' table of the POU where the FB is going to be used. The instance name must be unique within the POU.
Parameters Inputs

REQ

HANDLE

MAX_LENGTH

Input/Output

BUFFER

Outputs

DONE

BUSY

ERROR

STATUS

LENGTH_READ

EOF