-

FILE_READ

Type Function block
Description Reads data from a file that has been opened using the FILE_OPEN 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 further as a string, it must first be saved 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 MaxLength input.

Activate the function block on a rising edge at the Execute input. Execution of the function block is complete when the Done output is set to 1. If an error occurred during execution, the value at the Error output = 1. ErrorID indicates the number of the cause of the error.

Following successful execution, the data is copied to the buffer and the number of characters read is indicated at the LengthRead output. The number of characters read may be smaller than MaxLength if the end of the file was reached before the number of characters indicated was processed.
Notes
  • The status of the outputs Done, LengthRead, Error, and ErrorID is maintained until a falling edge is detected at the Execute input.
  • 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

Execute

Handle

MaxLength

Input/Output

Buffer

Outputs

Done

LengthRead

Error

ErrorID