FB_FormatString2

FB_FormatString2 1:

This function block can be used for converting up to 10 arguments (similar to fprintf) into a string and formatting them according to the format specification (e.g. '%+20.5f' or 'Measure X: %+.10d, Y: %+.10d'). The formatting takes place in the same PLC cycle. This means that the output string is available immediately after calling the function block.

As opposed to the function block FB_FormatString, the size of the format string and the output string is not limited to 255 characters. However, each argument is limited to a representation of 250 characters at the most. The function block outputs an error if the number of characters is exceeded or if the output string is too small for the formatted character string.

VAR_INPUT

VAR_INPUT
    pFormatString : POINTER TO STRING;
    arg1          : T_Arg;
    arg2          : T_Arg;
    arg3          : T_Arg;
    arg4          : T_Arg;
    arg5          : T_Arg;
    arg6          : T_Arg;
    arg7          : T_Arg;
    arg8          : T_Arg;
    arg9          : T_Arg;
    arg10         : T_Arg;
    pDstString    : POINTER TO STRING;
    nDstSize      : UDINT;
END_VAR

pFormatString: Pointer to the format specification as a string. The address must be assigned each time the function block is called. The operator ADR() can be used for the assignment.

arg1 to arg10: Arguments to be formatted (type: T_Arg). The following helper functions can be used for converting different types of PLC variables into the required data type T_Arg: F_BYTE, F_WORD, F_DWORD, F_LWORD, F_SINT, F_INT, F_DINT, F_LINT, F_USINT, F_UINT, F_UDINT, F_ULINT, F_STRING, F_REAL, F_LREAL.

pDstString: Pointer to the resulting STRING variable. If successful, the formatted character string will be written here. The address must be assigned each time the function block is called. The operator ADR() can be used for the assignment.

nDstSize: Size of the resulting STRING variable in bytes. The operator SIZEOF() can be used for the assignment.

VAR_OUTPUT

VAR_OUTPUT
    bError : BOOL;
    nErrId : UDINT;
END_VAR

bError: TRUE if an error occurred during the formatting.

nErrId: Returns the format error code if the bError output is set.

Sample

PROGRAM MAIN
VAR
    fbFormat   : FB_FormatString2;
    sFormat    : STRING := 'Measure X: %+.10d, Y: %+.10d';
    iY         : DINT;
    iX         : DINT;
    bError     : BOOL;
    nErrID     : UDINT;
    sOut       : STRING(600);
END_VAR
iX := iX + 1;
iY := iY + 1;
fbFormat( pFormatString := ADR(sFormat), arg1 := F_DINT( iX ), arg2 := F_DINT( iY ), pDstString := ADR(sOut), nDstSize := SIZEOF(sOut), bError => bError, nErrID => nErrID );

Result:

sOut = 'Measure X: +0000000130, Y: +0000000130'

Requirements

Development environment

Target platform

PLC libraries to be integrated (category group)

TwinCAT v3.1.4022

PC or CX (x86, x64, ARM)

Tc2_Utilities (System) >=3.3.35.0