Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CMP <=: Less or equal (S7-1200, S7-1500)

CMP <=: Less or equal

Description
You can use the "Less or equal" instruction to determine if a first comparison value (<Oper-
and1>) is less than or equal to a second comparison value (<Operand2>). Both values to
be compared must be of the same data type.
If the condition of the comparison is fulfilled, the instruction returns the result of logic opera-
tion (RLO) "1". If the comparison condition is not fulfilled, the instruction returns RLO "0".
The RLO of the instruction is logically combined with the RLO of the entire rung as follows:
• By AND, when the comparison instruction is connected in series.
• By OR, when the comparison instruction is connected in parallel.
Specify the first comparison value (<Operand1>) at the operand placeholder above the in-
struction. Specify the second comparison value (<Operand2>) at the operand placeholder
below the instruction.

Comparison of character strings


The individual characters are compared by means of their code (for example, 'a' is greater
than 'A') during the comparison of the strings. The comparison is performed from left to
right. The first character to be different decides the result of the comparison. If the left part
of the longer string is identical to the shorter string, the shorter string is considered smaller.
The following table shows examples of string comparisons:

<Operand1> <Operand2> RLO of the instruction

'AA' 'aa' 1
'AAA' 'a' 1
'Hello World' 'Hello World' 1
'HelloWorld' 'Hello World' 0
'BB' 'AA' 0
'AAA' 'AA' 0
You can also compare individual characters of a string. The number of the character to be
compared is specified in square brackets next to the operand name. "MyString[2]", for ex-
ample, compares the second character of the "MyString" string.

Note
Status still displayed despite "inactive" instruction
Under the following requirements:
• Prior to the instruction "CMP <=: Less or equal" (with data type STRING, WSTRING or
VARIANT), a condition (e.g. of a NO contact) is queried in the network.
• "Monitoring on" is switched on.
• A new result of the condition resets the network to FALSE. The instruction "CMP <=:
Less or equal" will become inactive.
Result:

-1-
CMP <=: Less or equal (S7-1200, S7-1500)

For the instruction "CMP <=: Less or equal" (data type STRING, WSTRING or VAR-
IANT), the previous status is still shown in the network.
When you switch "Monitoring on" off and on again or scroll to a different network, the
correct status of the instruction "CMP <=: Less or equal" (data type STRING, WSTRING
or VARIANT) is displayed. The instruction "CMP <=: Less or equal" is shown as grayed
out in the network in the inactive status.

Comparison of timers, date and time


When time values are compared, the RLO of the instruction is "1" if the point of time at
<Operand1> is less (less recent) than or equal to the point of time at <Operand2>.
Not all times can be compared directly with each other, such as S5TIME. In this case they
are implicitly converted into another time so that they can be compared, for example to
TIME.
If you want to compare dates and times of different data types, the value of the smaller
date or time data type is implicitly converted into the larger date or time data type. This
means the two date and time data types DATE and DTL, for example, are compared on the
basis of DTL.
When the implicit conversions fail, the comparison result is FALSE.

Comparing tags of WORD data type to tags of the S5TIME data type
Both tags are converted to the TIME data type when comparing a tag of WORD data type
to a tag of S5TIME data type. The WORD tag is interpreted as an S5TIME value. If one of
the two tags cannot be converted, the comparison is not performed and the result is
FALSE. If the conversion is successful the comparison is performed based on the selected
comparison instruction.

Parameters
The following table shows the parameters of the "Less or equal" instruction:

Parameters Declaration Data type Memory area Description

Bit strings, inte-


gers, floating-
<Oper- point numbers, I, Q, M, D, L, P,
Input First comparison value
and1> character or constant
strings, timers,
date and time
Bit strings, inte-
gers, floating-
<Oper- point numbers, I, Q, M, D, L, P,
Input Second value to compare
and2> character or constant
strings, timers,
date and time
You can select the data type of the instruction from the "???" drop-down list of the instruc-
tion box.

Example
The following example shows how the instruction works:

-2-
CMP <=: Less or equal (S7-1200, S7-1500)

The "TagOut" output is set when the following conditions are fulfilled:
• The operands "TagIn_1" and "TagIn_2" have signal state "1".
• The condition of the comparison instruction is fulfilled if "Tag_Value1" <= "Tag_Value2".
For more information and the program code to the above named example, refer to: Sam-
ple Library for Instructions

See also
Overview of the valid data types
Memory areas (S7-1500)
Basic information on LAD
Memory areas (S7-1200)

-3-

You might also like