# LGF_FileWrite (FB / 1.1.0)

Overview

Author: Siemens SIMATIC Systems Support

## Short description

This function block offers writing data as binary / serialized data stream to a file which is then stored on the PLC's memory card in the folder `UserFiles`.

### Block Interface

| LGF_FileWrite (FB) |
--- | --- | ---
Bool |  |  | execute |  | done |  |  | Bool
Bool |  |  | clearBufferBefore |  | busy |  |  | Bool
String |  |  | fileName |  | error |  |  | Bool
|  |  |  |  | status |  |  | Word
|  |  |  |  | dataLength |  |  | DInt
|  |  |  |  | diagnostics |  |  | LGF_typeDiagnostics
Array[*] of Byte |  |  | bufferByteArray |  |  | Array[*] of Byte
Variant |  |  | data |  |  | Variant

### Input parameter

Identifier | Data type | Default value | Description
--- | --- | --- | ---
execute | Bool | FALSE | Rising edge starts file write once
clearBufferBefore | Bool | FALSE | TRUE: Buffer would be cleared with 16#00 Byte before writing the data
fileName | String | '' | Name of file including path: `UserFiles/test.dat`

### Output parameter

Identifier | Data type | Description
--- | --- | ---
done | Bool | TRUE: Commanded functionality has been completed successfully
busy | Bool | TRUE: FB is not finished; new output values can be expected
error | Bool | TRUE: An error occurred during the execution of the functionality
status | Word | 16#0000 - 16#7FFF: Status of the FB, 16#8000 - 16#FFFF: Error identification
dataLength | DInt | Data length written to file (serialized length of `data`)
diagnostics | LGF_typeDiagnostics | Diagnostic structure to store and transfer diagnostic information from blocks through the interface.

### In/Out parameter

Identifier | Data type | Description
--- | --- | ---
bufferByteArray | Array[*] of Byte | Byte array buffer for read / write from / to file
data | Variant | Data set to write into file

### Status & Error codes

Code / Value | Identifier / Description
--- | ---
16#0000 | STATUS_EXECUTION_FINISHED Execution finished without errors
16#7000 | STATUS_NO_CALL No job being currently processed
16#7001 | STATUS_FIRST_CALL First call after incoming new job (rising edge 'execute')
16#7002 | STATUS_SUBSEQUENT_CALL Subsequent call during active processing without further details
16#8201 | ERR_BUFFER_LOWERBOUND Error: Buffer array lower bound Lower bound must be 0
16#8202 | ERR_BUFFER_ARRAY_TO_SMALL_TO_COPY Error: Buffer size less than needed size for data
16#8401 | ERR_FILE_PATH Error: File path error: File path must start with 'UserFiles/'.
16#8600 | ERR_UNDEFINED_STATE Error due to an undefined state in state machine
16#8601 | ERR_MOVE_BLK_VARIANT Error: Move block variant (data to buffer) - see `diagnostics.subFunctionStatus`
16#8603 | ERR_DATA_SERIALIZE Error: Serialize data - see `diagnostics.subFunctionStatus`
16#8604 | ERR_FILE_WRITE_INIT Error: Write file to SMC - see `diagnostics.subFunctionStatus`
16#8605 | ERR_FILE_WRITE Error: Write file to SMC - see `diagnostics.subFunctionStatus`

## User defined datatype(s)

### LGF_typeDiagnostics (UDT / V1.0.1)

Diagnostic structure to store and transfer diagnostic information from blocks through the interface.

Identifier | Data type | Default value | Description
--- | --- | --- | ---
status | Word | 16#0000 | Status of the Block or error identification when error occurred
subfunctionStatus | Word | 16#0000 | Status or return value of called FB's, FC's and system blocks
stateNumber | DInt | 0 | State in the state machine of the block where the error occurred

## Functional description

With the function `LGF_FileWrite` the data budget of a variable can be written to `data` in a file. For writing the data it is necessary to serialize it, which the function already takes from the user.
For serialization an external buffer in the form of a byte array must be connected which can take up the data quantity, if the buffer is too small an error is output.

The file name must always be specified in full together with the folder name and the file extension in the following format: `UserFiles/test.dat`.

Note
  The file extension (here e.g. `dat`) can be freely selected or omitted, it is useful for external processing to indicate the format of the file to the user.
  A file extension in the file name has no influence on the content of the file as well as its formatting, providing the data in an appropriate file format is up to the user.

Warning
  The SMC (SIMATIC Memory Card) is a flash memory with finite writing cycles.
  A high frequent write access therefore also affects the lifetime of the SMC!

## Change log

Version & Date | Change description
--- | ---
| 1.0.0 | Simatic Systems Support
| 19.02.2023 | First released version
| 1.1.0 | Simatic Systems Support
| 29.04.2025 | Insert parameter `clearBufferBefore` to clear buffer befor serializing the data