#26 Find or create a Codesys solution for: FB_FormatString

None
closed
aliazzz
None
2019-07-29
2019-07-27
aliazzz
No

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
    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
    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'

For more details see
https://infosys.beckhoff.com/english.php?content=../content/1033/tcplclib_tc2_utilities/9007199289731211.html&id=4735384626923042142

Discussion

  • aliazzz

    aliazzz - 2019-07-28
    • status: open --> accepted
    • assigned_to: aliazzz
     
  • aliazzz

    aliazzz - 2019-07-29
    • status: accepted --> closed
    • Milestone: 1.0 -->
     
  • aliazzz

    aliazzz - 2019-07-29

    Concat will do the trick...

     

Log in to post a comment.