-

FILE_WRITE_2

Type Function block
Description Writes data to a file that was opened previously.

Assign the valid handle for the file to which data is to be written to the HANDLE input. This handle is created when the file is opened with the FILE_OPEN_2 function block.

Create a data buffer of sufficient size, which contains the data to be written, for the BUFFER input. The data buffer for the data 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 a string is to be saved in a file, the string must first be saved in an array using the STRING_TO_BUF function block.

Example: Byte array used as data buffer type

Assign the number of bytes to be written to the 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 write 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 the function block could not be executed successfully, the number of bytes written will be indicated at the LENGTH_WRITTEN output. It is the same as the number of bytes to be written.

If an error occurred during execution, ERROR is set to TRUE, and the error code can be read from the STATUS output.
Notes
  • The status of the DONE, BUSY, ERROR, and STATUS outputs are maintained as long as REQ is TRUE. The status of the LENGTH_WRITTEN output is kept.

    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

LENGTH

Input/Output

BUFFER

Outputs

DONE

BUSY

ERROR

STATUS

LENGTH_WRITTEN