FB_MemBufferSplit

 FB_MemBufferSplit 1:

This function block splits a memory area (data buffer) into several smaller segments of certain maximum length as required. The function block returns a smaller partial segment, if the length of the last segment is smaller than required.

VAR_INPUT

VAR_INPUT
    eCmd        : E_EnumCmdType := eEnumCmd_First;
    pBuffer     : DWORD;
    cbBuffer    : UDINT;
    cbSize      : UDINT;
END_VAR

eCmd: Control parameter for the enumeration block: eEnumCmd_First returns the first segment, eEnumCmd_Next returns the next segment. No other parameters are used.

pBuffer: Address (pointer) of the data buffer to be divided. The address can be determined with the ADR operator.

cbBuffer: Length of the data buffer to be divided. The length can be determined with the SIZEOF operator.

cbSize: Maximum segment size into which the data buffer is to be divided.

VAR_OUTPUT

VAR_OUTPUT
    bOk       : BOOL;
    pSegment  : DWORD;
    cbSegment : UDINT;
    bEOS      : BOOL;
END_VAR

bOk: TRUE = success, FALSE = error, invalid parameter value or no further segment available.

pSegment: Address (pointer) to the next data segment.

cbSegment: Length (bytes) of the next data segment.

bEOS: End of segment. TRUE = last segment. FALSE = further segments follow.

 

Example in ST:

In the following example the buffer variable is divided into 5-byte segments. The returned segments are converted to a hexadecimal string for test purposes.

PROGRAM MAIN
VAR
    bSplit  : BOOL := TRUE;
    buffer  : ARRAY[1..30] OF BYTE := 16#A,1,2,3,4,5,6,7,8,9,16#B,1,2,3,4,5,6,7,8,9,16#C,1,2,3,4,5,6,7,8,9;
    fbSplit     : FB_MemBufferSplit;
    sHex    : T_MaxString;
END_VAR
IF bSplit THEN
    bSplit := FALSE;
    fbSplit.eCmd := eEnumCmd_First;

    REPEAT
        fbSplit( pBuffer := ADR(buffer), cbBuffer := SIZEOF(buffer), cbSize := 5 );
        IF fbSplit.bOk THEN

            sHex := DATA_TO_HEXSTR( pData := fbSplit.pSegment, cbData := fbSplit.cbSegment, FALSE );

            fbSplit.eCmd := eEnumCmd_Next;
        END_IF
    UNTIL NOT fbSplit.bOk
    END_REPEAT
END_IF

 

Requirements

Development environment

Target platform

PLC libraries to be linked

TwinCAT v2.10.0 Build >= 1331

PC or CX (x86, ARM)

TcUtilities.Lib