CMP <>: Not equal (S7-1200, S7-1500) - STEP 7

FBD (S7-1200, S7-1500)

ft:publication_title
FBD (S7-1200, S7-1500)
Product
STEP 7
Version
V20
Publication date
11/2024
Language
en-US
CMP <>: Not equal

Description

You can use the "Not equal" instruction to query whether the value at input IN1 is not 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 <>: Not equal" we recommend using the instruction "OUT_RANGE: Value outside 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 <>: Not 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'

1

'AA'

'AA'

0

'aa'

'aaa'

1

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:

  • Prior to the instruction "CMP <>: Not 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 <>: Not equal" will become inactive.

Result:

For the instruction "CMP <>: Not 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 <>: Not equal" (data type STRING, WSTRING or VARIANT) is displayed. The instruction "CMP <>: Not 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 <>: Not equal" has the result TRUE.

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 with the "Not equal" instruction, you need to select the WORD data type from the drop-down list of the instructions box.

If you want to compare the two hardware data types HW_IO and HW_DEVICE, you must first create a tag of data type HW_ANY in the section "Temp" of the block interface and then copy the LADDR (of 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. The following data types are possible:

  • PLC data type

  • STRUCT (the structure of the data type STRUCT must be contained in a PLC data type (UDT) or the two structures to be compared are two elements of an ARRAY of STRUCT. Anonymous structures are not permitted.)

  • 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

0

 

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

1

 

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

0

 

BOOL

FALSE

 

BOOL

FALSE

INT

2

INT

2

Parameters

The following table shows the parameters of the "Not equal" instruction:

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".