SET_LEN

Set string length

This instruction sets the string length of a string at the input STR to a new length defined at the input LEN. If the new length exceeds the maximum length of the string declaration, an error occurs and bError is set to TRUE. If you define a new string length that is shorter than the current string length, the string will be truncated to the new string length.

Parameters

Input

LEN (INT)

New string length

Input/output

STR (STRING)

String set to the new length

Output

bError (BOOL)

Set to TRUE, if the new string length exceeds the maximum string length of string declaration

Remarks

  • If this instruction is used with UTF-8 strings, please refer to the notes concerning UTF-8 strings under the data type STRING.

Error flags

sys_bIsOperationErrorHold (turns to TRUE and remains TRUE)

if a string applied at the input or output is an invalid string

sys_bIsOperationErrorNonHold (turns to TRUE for one scan)

if a string applied at the input or output is an invalid string

Example

POU header

All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.

	VAR
		sTestString: STRING[128]:='A better life. A better world';
		iStringLength: INT:=0;
		bError: BOOL:=FALSE;
		bStart: BOOL:=FALSE;
	END_VAR

POU body

When the variable bStart is set to TRUE, the function is carried out.

LD body

BODY
    WORKSPACE
        NETWORK_LIST_TYPE := NWTYPELD ;
        ACTIVE_NETWORK := 0 ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 5 ;
        NETWORK_BODY
B(B_F,E_SET_LEN!,,24,0,31,5,,?DEN?DLEN?DSTR?ASTR?AENO?CbError);
B(B_CONTACT,,bStart,5,1,7,3,);
B(B_VARIN,,sTestString,22,3,24,5,);
B(B_VAROUT,,bError,31,2,33,4,);
B(B_VARIN,,32,22,2,24,4,);
L(1,2,5,2);
L(7,2,24,2);
L(1,0,1,5);
        END_NETWORK_BODY
    END_NET_WORK
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 5 ;
        NETWORK_BODY
B(B_VAROUT,,iStringLength,29,2,31,4,);
B(B_VARIN,,sTestString,21,2,23,4,);
B(B_F,LEN!,,24,1,29,4,,?DIN?C);
L(23,3,24,3);
L(1,0,1,5);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

ST body

If (bStart) then
    bError := SET_LEN(32, sTestSTring);
    iStringLength := LEN(sTestString);
End_if;

Modified on: 2022-03-02Feedback on this pagePanasonic hotline