FB_CSVMemBufferWriter 1:

This function block can be used to generate data sets in an external buffer in CSV format from individual data fields. The content of the buffer can then be written into a file, e.g. with the aid of the function blocks for file access. The new data field can be transferred to the block either via the putValue variable (string) or via the optional pValue and cbValue variables, depending on whether data fields without control characters (string), data fields with control character or binary data are to be written into the data set. The function block can generate several data sets in the buffer until the maximum available buffer size is reached. The end of the data set (last data field in the current data set) is automatically appended to the data field if the bCRLF variable was set to TRUE during writing of the data field. The block automatically adds the data field separators. The default data field separator is a semicolon. The separator can be configured from semicolon to comma via the global PLC variable DEFAULT_CSV_FIELD_SEP.


    eCmd     : E_EnumCmdType := eEnumCmd_First;
    putValue : T_MaxString := '';
    pValue   : POINTER TO BYTE := 0;
    cbValue  : UDINT := 0;
    bCRLF    : BOOL := FALSE;
    pBuffer  : POINTER TO BYTE;
    cbBuffer : UDINT;

eCmd: Control parameter for the buffer component (type: E_EnumCmdType). eEnumCmd_First adds the first data field to the buffer, eEnumCmd_Next adds the next data field. No other parameter values are used.

putValue: A new data field as a string (type: T_MaxString). This input must be an empty string if the optional parameters pValue and cbValue are used instead of this input.

pValue: Optional: Address of an external byte buffer containing the new data field. Together with the cbValue parameter this input can then be used to write a data field with control characters or binary data into the data set, for example. Control characters or binary data in the data field could truncate the putValue string at an undesired location and are therefore transferred as a byte buffer. This input must be zero if it is not used.

cbValue: Optional: Length of the data field data in the external byte buffer. This input must be zero if it is not used.

bCRLF: If this input set the new data field is terminated with a CRLF data set separator. Subsequent data fields belong to the new data set.

pBuffer: Address (pointer) for the target buffer variable. The address can be determined with the ADR operator. In this buffer the function block generates the data sets in CSV format.

cbBuffer: Maximum available size (in bytes) of the target buffer variables. The size can be determined with the SIZEOF operator.


    bOk      : BOOL;
    cbSize   : UDINT;
    cbFree   : UDINT;
    nFields  : UDINT;
    nRecords : UDINT;
    cbWrite  : UDINT;

bOk: TRUE = success, FALSE = buffer overflow or faulty input parameters.

cbSize: Current buffer fill status (number of data bytes created in the buffer).

cbFree: Number of free data bytes in the buffer.

nFields: Number of written data fields.

nRecords: Number of written data sets.

cbWrite: Number of last written data bytes (length of the last data field + any data set or data field separators).


See: Example: Writing/reading of a CSV file.


Development environment

Target platform

PLC libraries to be integrated (category group)

TwinCAT v3.1.0

PC or CX (x86, x64, ARM)

Tc2_Utilities (System)