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 ( e.g. '%+20.5f' or 'Measure X: %+.10d, Y: %+.10d' ).

arg1 bis arg10: arguments to be formatted. 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_SINT, F_INT, F_DINT, F_USINT, F_UINT, F_UDINT, 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.

 

Example in ST:

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 system type

PLC libraries to be linked

TwinCAT v2.9.0 Build >1021

PC or CX (x86)

TcUtilities.Lib

( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically )

TwinCAT v2.10.0 Build >= 1301

CX (ARM)