FB_FormatString

FB_FormatString 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. Formatting occurs in the same PLC cycle, i.e. the output string is available immediately after the FB was called.

VAR_INPUT

VAR_INPUT
    sFormat   : T_MaxString;
    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;
END_VAR

sFormat: Format specification as string (type: T_MaxString)( e.g. '%+20.5f' or 'Measure X: %+..10d, Y: %+..10d' ).

arg1 to arg10: Arguments to be formatted (type: T_Arg). The following auxiliary 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.

VAR_OUTPUT

VAR_OUTPUT
    bError : BOOL;
    nErrId : UDINT;
    sOut   : T_MaxString;
END_VAR

bError: This output is set if an error occurs during formatting.

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

sOut: If successful, this output returns the formatted output string (type: T_MaxString)

Example:

PROGRAM MAIN
VAR
    fbFormat   : FB_FormatString;
    iY         : DINT;
    iX         : DINT;
    bError     : BOOL;
    nErrID     : UDINT;
    sOut       : T_MaxString;
END_VAR
iX := iX + 1;
iY := iY + 1;
fbFormat( sFormat := 'Measure X: %+.10d, Y: %+.10d', arg1 := F_DINT( iX ), arg2 := F_DINT( iY ), sOut => sOut, bError => bError, nErrID => nErrID );

The result:

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

Requirements

Development environment

Target platform

PLC libraries to be integrated (category group)

TwinCAT v3.1.0

PC or CX (x86, x64, ARM)

Tc2_Utilities (System)