Description
You can use the "Equal" instruction to query whether the value at input IN1 is equal to the value at input IN2.
If the condition of the comparison is fulfilled, the instruction returns the result of logic operation (RLO) "1". If the comparison condition is not fulfilled, the instruction returns RLO "0".
If IEC check is enabled, the operands to be compared must be of the same data type. If IEC check is not enabled, the width of the operands must be the same.
Comparison of floating-point numbers
If you want to compare the data types REAL or LREAL, then instead of the instruction "CMP ==: Equal" we recommend using the instruction "IN_RANGE: Value within range".
If floating-point numbers are being compared, the operands to be compared must be of the same data type regardless of the IEC check setting.
The special bit patterns of invalid floating-point numbers (NaN) that are the outcome of undefined results (e.g. root of -1) are not comparable. This means that if one of the operands has the value NaN, the instruction "CMP ==: Equal" has the result FALSE.
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.
The following table shows examples of string comparisons:
|
IN1 |
IN2 |
RLO of the instruction |
|---|---|---|
|
'AA' |
'AA' |
1 |
|
'Hello World' |
'HelloWorld' |
0 |
|
'AA' |
'aa' |
0 |
|
'aa' |
'aaa' |
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 example, compares the second character of the "MyString" string.
|
Note Status still displayed despite "inactive" instruction Under the following requirements:
Result: For the instruction "CMP ==: Equal" (data type STRING, WSTRING or VARIANT), 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 ==: Equal" (data type STRING, WSTRING or VARIANT) is displayed. The instruction "CMP ==: Equal" is shown as grayed out in the network in the inactive status. |
Comparison of timers, date and time
Bit patterns of invalid timers, date and times, e.g. DT#2015-13-33-25:62:99.999_999_999, cannot be compared. This means that if one of the operands has an invalid value, the instruction "CMP ==: Equal" has the result FALSE.
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.
Comparison of hardware data types
To be able to compare the operands of the PORT data type, you need to select the WORD data type from the drop-down list in the instruction box.
If you want to compare the two data types, you must first create a tag of data type HW_ANY in the "Temp" section of the block interface and then copy the LADDR (from data type HW_DEVICE) to the tag. It is then possible to compare HW_ANY and HW_IO.
Comparison of structures
|
Note Availability of comparison of structures The option to compare structures is available for a CPU of the S7-1200 series as of firmware version >= 4.2, and for a CPU of the S7-1500 series as of firmware version >= 2.0. |
You can compare the values of two structured operands when both tags are of the same structure data type. When structures are compared, the operands to be compared must have the same data type regardless of the setting for the IEC Check. An exception is comparisons in which one of the two operands is a VARIANT or an ANY. If the data type is not yet known when the program is created, you can use the VARIANT data type. In this case you can also compare the operand with a structured tag of any data type. You can also compare two tags of the data type VARIANT or ANY with each other.
You can select the data type VARIANT for the comparison of structures from the drop-down list in the instruction box. Tags from the following data types are possible:
-
PLC data type (UDT)
-
STRUCT (either the structure of the STRUCT data type must be contained in a PLC data type or the two structures to be compared must be two elements of an ARRAY of STRUCT. Instance data blocks and tags of anonymous structures are not permitted.)
-
Data blocks derived from a PLC data type.
-
Tag to which ANY is pointing
-
Tag to which VARIANT is pointing
The following requirements must be met to compare two tags of the data type ARRAY with VARIANT selected:
-
The elements must each have the same data type.
-
The two ARRAYs must have the same dimension.
-
All dimensions must have the same number of elements. The exact ARRAY limits do not have to match.
|
Note ARRAY of BOOL When you compare two operands of the data type ARRAY of BOOL with each other, you have selected the data type VARIANT from the drop-down list, and the number of elements cannot be divided by 8, the fill bits are compared as well. This may affect the comparison result. |
If an element within the structures that you want to compare is an invalid STRING/WSTRING, an invalid time or date or an invalid floating-point number, the comparison result in the RLO (result of logic operation) will return the signal state "0".
The table below shows an example of a comparison of structures:
|
<Operand1> |
<Operand2> |
RLO of the instruction |
||||
|---|---|---|---|---|---|---|
|
Tag of data type A <PLC data type> |
Tag value |
Tag of data type A <PLC data type> |
Tag value |
1 |
||
|
BOOL |
FALSE |
BOOL |
FALSE |
|||
|
INT |
2 |
INT |
2 |
|||
|
<Operand1> |
<Operand2> |
RLO of the instruction |
||||
|---|---|---|---|---|---|---|
|
Tag of data type A <PLC data type> |
Tag value |
Tag of data type B <PLC data type> |
Tag value |
0 |
||
|
BOOL |
FALSE |
BOOL |
TRUE |
|||
|
INT |
2 |
INT |
3 |
|||
|
<Operand1> |
<Operand2> |
RLO of the instruction |
||||
|---|---|---|---|---|---|---|
|
Tag of data type A <PLC data type> |
Tag value |
VARIANT (supplied with tag of data type A) |
Tag value |
1 |
||
|
BOOL |
FALSE |
BOOL |
FALSE |
|||
|
INT |
2 |
INT |
2 |
|||
Parameters
The following table shows the parameters of the instruction "Equal":
|
Parameters |
Declaration |
Data type |
Memory area |
Description |
|---|---|---|---|---|
|
IN1 |
Input |
Bit strings, integers, floating-point numbers, character strings, timers, date and time, ARRAY of <data type> with fixed and variable ARRAY limits, STRUCT, VARIANT, ANY, PLC data type |
I, Q, M, D, L, P or constant |
First comparison value |
|
IN2 |
Input |
Bit strings, integers, floating-point numbers, character strings, timers, date and time, ARRAY of <data type> with fixed and variable ARRAY limits, STRUCT, VARIANT, ANY, PLC data type |
I, Q, M, D, L, P or constant |
Second value to compare |
|
As detailed above, the data types ARRAY, STRUCT (in a PLC data type), VARIANT, ANY and PLC data type (UDT) are only available as of firmware version 2.0 or 4.2. |
||||
You can select the data type of the instruction from the "???" drop-down list of the instruction box.
Example
The following example shows how the instruction works:
The "TagOut" output is set when the following conditions are fulfilled:
-
The operand "TagIn_1" has the signal state "1".
-
The condition of the comparison instruction is fulfilled if "Tag_Value1" = "Tag_Value2".