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
| Answer | Rung |
|---|---|
| Anything but zero | Power carries on |
| Zero | Power 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 = -1 | on |
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") IsINFandIsNAN, which test aREALfor 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):
| Expression | What happens | Rung |
|---|---|---|
Count AND 1 | 10 AND 01 — no bit set in both | off |
(Count > 0) && (1 > 0) | both comparisons are true | on |
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
ANDto join two comparisons. Use&&. - Putting
CMPat the end of a rung. It is an input, so something has to follow it. - Testing a single bit with
CMP. UseXICorXIO. - Comparing two decimals for exact equality.
REALvalues 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).
Related
- CPT — the same expressions, used to store a number
- Expressions in Ladder — operator order, decimals, and the traps
- Compare instructions —
EQ,GTand friends, one comparison per box
Compare — EQ / NE / GT / GE / LT / LE
Reference for Ladder Logic compare instructions in rungs.dev — EQ, NE, GT, GE, LT, LE — that pass power along a rung when the numeric relationship holds.
IsINF / IsNAN — Test for Infinity or Not a Number
IsINF and IsNAN test one REAL tag for infinity or for "not a number". Covers what produces each value, both Compare-tab boxes, and the same two tests written inside a CMP expression.