Processing of strings (Data type STRING)

Một phần của tài liệu Hans berger automating with SIMATIC s7 1200 configuring programming (Trang 399 - 405)

A string can be processed using the following functions:

b LEN Outputs the length of a string b CONCAT Combines two strings together b LEFT Outputs the left part of a string b RIGHT Outputs the right part of a string b MID Outputs the middle part of a string b DELETE Deletes part of a string

b INSERT Inserts characters into a string b REPLACE Replaces characters in a string

b FIND Outputs the position of a searched character

Fig. 11.30 Minimum and maximum selection, representation and function

EN EN

OUT ENO

OUT ENO

Minimum selection MIN, Maximum selection MAX

LAD FBD

IN1 IN2

IN1 IN2

Function:

MIN MAX

Minimum selection Maximum selection

Data type:

USINT, UINT, UDINT, SINT, INT, DINT, REAL, LREAL Declaration

Name EN ENO IN1 IN2 OUT

– – INPUT INPUT OUTPUT

BOOL BOOL Data type Data type Data type

Description Data type

SCL OUT := Function(IN1 := ... , IN2 := ... );

* *

Enabling input Enabling output Input tag 1 Input tag 2 Result Function

Data type

Function Data type

The lowest input value is copied to the OUT output.

The highest input value is copied to the OUT output

All functions for string processing expect a valid string with plausible values in the length bytes at the parameters with data type STRING (maximum length  254, actual length  maximum length). Strings which you do not assign with default val- ues during their declaration are automatically assigned as an empty string (actual length = 0) of maximum length (= 254).

Note that you cannot assign default values to strings which you declare in the tem- porary local data. In this case you must assign a defined value (this can also be an empty string) to a STRING tag in the program before you use the STRING tag togeth- er with a function or block.

11.9.1 Output length of a string LEN

The LEN function outputs the current length of a string present at the IN parameter at the OUT parameter. For an “empty” string, the current length is zero. The maxi- mum length of a string is 254 characters. LEN returns an error on incorrect param- eter assignment (Fig. 11.32).

Fig. 11.31 Limiter LIMIT, representation and function

Fig. 11.32 Output length of a string LEN, function and representation

EN EN

OUT ENO

OUT ENO Limiter LIMIT

LAD FBD

MN IN MX

MN IN MX

Data type:

USINT, UINT, UDINT, SINT, INT, DINT, REAL, LREAL

Function:

Declaration Name

EN ENO MN IN MX OUT

– – INPUT INPUT INPUT OUTPUT

BOOL BOOL Data type Data type Data type Data type

Description Data type

LIMIT Data type

LIMIT Data type

SCL OUT := LIMIT(MN := ... , IN := ... , MX := ... );

Enabling input Enabling output Lower limit Input tag Upper limit Result

The value of the tag at the IN parameter is limited to the MIN and MAX limits, and output at the OUT parameter.

EN EN

OUT

ENO OUT

ENO Output length of a string LEN

LAD FBD

IN IN

Deklaration Name

IN OUT

INPUT OUTPUT

STRING INT

String Actual length Description Data type

LEN String

LEN String

Function:

LENoutputs the length of the string present at the IN parameter at the OUT parameter.

SCL OUT := LEN(IN);

11.9.2 Combine strings CONCAT

The CONCAT function combines the STRING tags at parameters IN1 and IN2 into a single tag and outputs it at parameter OUT. The string of IN2 is appended to the string of IN1. If the length of both source strings exceeds the maximum length of the target string, they are truncated to the maximum length and ENO is set to “0”

(Fig. 11.33).

11.9.3 Output left part of string LEFT

The function LEFT returns the first characters of the string, whose number is spec- ified at parameter L, at the IN parameter and writes it as a STRING tag to the OUT parameter. If L is greater than the current length of the input tags, the input value is output. With an empty string as the input value, an empty string is output. If L is equal to zero or negative, an empty string is output and ENO is set to “0” (Fig. 11.34).

Fig. 11.33 Combining two strings CONCAT, function and representation

Fig. 11.34 Output left or right part of a string LEFT and RIGHT

EN EN

OUT ENO

OUT ENO Combination of two strings CONCAT

LAD FBD

IN1 IN2

IN1 IN2

CONCAT CONCAT

Function:

Declaration Name

IN1 IN2 OUT

INPUT INPUT OUTPUT

STRING STRING STRING

Description Data type

SCL OUT := CONCAT(IN1 := ... , IN2 := ... );

String 1 String 2 Result

String String

CONCAT adds the string at the IN2 parameter to the string at the IN1 parameter, and outputs the result at the OUT parameter.

Declaration Name

IN L OUT

INPUT INPUT OUTPUT

STRING INT STRING

Description Data type

EN EN

OUT ENO

OUT ENO

Output left or right part of a string LEFT, RIGHTen

LAD FBD

IN L

IN L

Function:

SCL OUT := Function(IN := ... , L := ... );

String

Number of charact.

Result

Function Function

String String

LEFT extracts the number of characters specified in the L parameter from the string at the IN1 parameter starting at position 1, and outputs the result at the OUT parameter.

RIGHT takes from the string at parameter IN1 the number of characters specified by parameter L from the last position and outputs the result at parameter OUT.

11.9.4 Output right part of string RIGHT

The RIGHT function delivers the last characters (whose number is specified at the L parameter) from the string applied to the IN parameter, and writes them as a STRING tag to the OUT parameter. If L is greater than the current length of the input tags, the input value is output. An empty string is output if the input value is an empty string. If L is equal to zero or negative, an empty string is output and ENO is set to “0” (Fig. 11.34).

11.9.5 Output middle part of string MID

The MID function extracts a middle section of the string present at the IN parame- ter, and outputs it at the OUT parameter. The middle section starts at the position specified at the P parameter, and has as many characters as defined by the L param- eter (Fig. 11.35).

If the sum of P and L exceeds the current length of the input tags, a string beginning at position P and reaching to the end is output. If P is outside the current length of IN, a blank string is output and ENO is set to “0”. If P or L is zero or negative, a blank string is output and ENO is set to “0”.

11.9.6 Delete part of a string DELETE

The DELETE function removes part of the string at the IN parameter and outputs the

“collapsed” remainder at the OUT parameter. The removed part begins at the char- acter position specified by parameter P and has as many characters as specified in parameter L (Fig. 11.36).

Fig. 11.35 Output the middle part of a string MID, function and representation Declaration

Name IN L P OUT

INPUT INPUT INPUT OUTPUT

STRING INT INT STRING

Description Data type

EN EN

OUT ENO

OUT ENO Output middle section of a string MID

LAD FBD

IN L P

IN L P

MID MID

Function:

MID extracts the number of characters specified in the L parameter from the string at the IN1 parameter starting at position P, and outputs the result at the OUT parameter.

SCL OUT := MID(IN := ... , L := ... , P := ... );

Input tag Number of charact.

Character position Output tag

String String

If L is equal to zero, the input string is output. If P is greater than the current length of the input tag, this tag is output and ENO is set to “0”. If the sum of P and L is great- er than the current length of the input tag, the string is deleted up to the end. If L is negative or if P is zero or negative, an empty string is output and ENO is set to “0”.

11.9.7 Insert string INSERT

The INSERT function inserts the string at the IN2 parameter into the string at the IN1 parameter and outputs the result at the OUT parameter. Parameter P specifies the position from which the insertion is to take place (Fig. 11.37).

If P is equal to zero or negative, an empty string is output and ENO is set to “0”. If P is greater than the current length of IN1, IN2 is appended to IN1, and ENO is set Fig. 11.36 Delete part of string DELETE, function and representation

Fig. 11.37 Insert string INSERT, function and representation Declaration Name

IN L P OUT

INPUT INPUT INPUT OUTPUT

STRING INT INT STRING

Description Data type

EN EN

OUT ENO

OUT ENO Delete part of a string DELETE

LAD FBD

IN L P

IN L P DELETE

String

DELETE String

Function:

DELETE deletes the number of characters specified in the L parameter from the string at the IN parameter starting at position P, and outputs the result at the OUT parameter.

SCL OUT := DELETE(IN := ... , L := ... , P := ... );

Input tag Number of charact.

Character position Output tag

Declaration Name

IN1 IN2 P OUT

INPUT INPUT INPUT OUTPUT

STRING STRING INT STRING

Description Data type

EN EN

OUT ENO

OUT ENO Insert string into another string INSERT

LAD FBD

IN1 IN2 P

IN1 IN2 P INSERT

String

INSERT String

Function:

INSERT inserts the string IN2 into the string at the IN1 parameter starting at position P, and outputs the result at the OUT parameter.

SCL OUT := INSERT(IN1 := ... , IN2 := ... , P := ... );

Input tag String to be inserted Character position Output tag

to “0”. If the new string is longer than the maximum length permitted by the out- put string, the characters are entered up to the permissible length, and ENO is set to “0”.

11.9.8 Replace part of string REPLACE

REPLACE replaces characters present in the string at the IN1 parameter by the string at the IN2 parameter and outputs the result at the OUT parameter. Beginning with the position specified by parameter P, the characters are replaced for a length given at parameter L (Fig. 11.38).

If L is equal to zero, the IN2 string is inserted into the IN1 string from position P without deleting characters in IN1. If P is equal to 1, the first L characters of IN1 are replaced by IN2. If P is greater than the current length of IN1, IN2 is appended to IN1, and ENO is set to “0”. If L is negative or if P is zero or negative, an empty string is output and ENO is set to “0”. If the new string is longer than the maximum length of the output string, only the maximum length is output, and ENO is set to “0”.

11.9.9 Find part of string FIND

the FIND function determines the position of the string at parameter IN2 in the string at parameter IN1 and outputs it at the OUT parameter. The position of the first character is output if a match has been found. If IN2 is not contained in IN1, zero is returned (Fig. 11.39).

Fig. 11.38 Replace part of string REPLACE, function and representation Declaration

Name IN1 IN2 L P OUT

INPUT INPUT INPUT INPUT OUTPUT

STRING STRING INT INT STRING

Description Data type

EN EN

OUT ENO

OUT ENO

Replace string in a different string REPLACE

LAD FBD

IN1 L IN2

P

IN1 L IN2

P REPLACE

String

REPLACE String

Function:

SCL OUT := REPLACE(IN1 := ... , IN2 := ... , L := ... , P := ... );

Input tag String to be inserted Number of charact.

Character position Output tag

REPLACE replaces the number of characters specified in the L parameter in the string at the IN1 parameter with the string IN2 starting at position P, and outputs the result at the OUT parameter.

Một phần của tài liệu Hans berger automating with SIMATIC s7 1200 configuring programming (Trang 399 - 405)

Tải bản đầy đủ (PDF)

(577 trang)