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. The formatting takes place in the same PLC cycle. This means that the output string is available immediately after calling the FB.

FB_FormatString 2: Inputs

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

Name

Type

Description

sFormat

T_MaxString

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

arg1 to arg10

T_Arg

Arguments to be formatted. The following helper functions can be used to convert PLC variables of different types 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.

FB_FormatString 3: Outputs

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

Name

Type

Description

bError

BOOL

TRUE if an error occurred during the formatting.

nErrId

UDINT

Returns the Format error code if the bError output is set.

sOut

T_MaxString

If successful, this output returns the formatted output string

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)