CMP — Compare

CMP (Compare) works out an expression and uses the answer as the rung condition. Any value other than zero passes power. Covers AND versus &&, and why a bare bit needs XIC.

CMP

Expression

expression

Works out an expression and uses the answer as the rung condition. Any value other than zero passes power.

CMP is an input instruction, so it sits on the left of the rung like a contact:

CMP(Level > HighSetpoint) OTE(Alarm)

Where GT compares exactly two values, CMP takes a whole expression, so you can ask a question that would otherwise need several boxes and a scratch tag.

Operands

CMP has one operand: the expression itself — tags and typed-in values of type SINT, INT, DINT or REAL, joined by operators. What you can write in it is below.

How It Works

AnswerRung
Anything but zeroPower carries on
ZeroPower stops

Any value other than zero is true, and the sign does not matter:

CMP(…)Rung
CMP(5)on
CMP(0)off
CMP(-1)on
CMP(Count) with Count = -1on

So CMP(Count) asks "is Count anything other than zero?". A comparison like Count > 5 answers 1 or 0, which fits the same rule.

What you can write in the expression

Everything CPT takes, plus:

  • the comparisons < <= > >= = <>
  • the logical operators && ("both"), || ("either"), ^^ ("exactly one"), ! ("not")
  • IsINF and IsNAN, which test a REAL for infinity or for "not a number"

AND is bitwise, && means "both"

This is the one that catches everybody. AND OR XOR NOT work on the bits inside a number, not on conditions.

With Count = 2 (binary 10):

ExpressionWhat happensRung
Count AND 110 AND 01 — no bit set in bothoff
(Count > 0) && (1 > 0)both comparisons are trueon

Writing Count > 1 AND Limit < 5 is an error, not a slow way of getting the right answer. To join two comparisons use &&:

CMP((Count > 1) && (Limit < 5))

If you already write Structured Text this will feel wrong — there, AND between two comparisons does mean "both". Inside a ladder box, keep AND for bits and && for conditions.

Testing a single bit

CMP(Running) on its own is not allowed. To test one bit, use XIC:

XIC(Running) OTE(Lamp)

A bit is fine inside a larger test, though — CMP(!Running) and CMP(Running && Ready) both work.

Example — Tank Level Alarm

Raise an alarm when the tank is above the high setpoint and the pump is not already running.

0

CMP

Expression

(In_Level > Cfg_HighSetpoint) && !Sts_PumpRunning

Out_Alarm

The level is 85 and the setpoint is 80, so the first comparison is true; the pump is stopped, so !Sts_PumpRunning is true as well. Both true, so the alarm comes on.

Common Mistakes

  • Using AND to join two comparisons. Use &&.
  • Putting CMP at the end of a rung. It is an input, so something has to follow it.
  • Testing a single bit with CMP. Use XIC or XIO.
  • Comparing two decimals for exact equality. REAL values keep about seven digits, so a calculated value rarely lands on the number you expect. Compare against a range instead: CMP(ABS(Measured - Target) < 0.01).

On this page