misunderstanding CMP

MartyMaucher

Member
Join Date
May 2014
Location
Pensacola
Posts
91
In Logix Designer, I'm just not understanding how to compare a bit to 0:

CMP: bHoldAnalogsDuringCal = 0 returns Invalid expression.

This is a simple comparison of a bit to zero. o_O

CMP.PNG
 
The CMP instruction is meant to be used for complex expressions, so that you don't have to use a bunch of intermediate registers.

The CMP instruction doesn't operate on BOOL tags; I had to check the User Manual to be sure that its operands need to be SINT, INT, DINT, REAL, or String.

That instruction is probably overkill for an "If Boolean Tag is False" evaluation. The ordinary XIO instruction will run faster and hopefully be clear to a troubleshooter.
 
Hi

For Sint dint int real you could use an equ instruction
But as ken said for a bool it does not make sense

Donnchadh
 
Arrrg ... As a C programmer new at PLCs, my first instinct was to use a CMP, but now seems trivial... thanks guys.

This: (bNotMeasureModeAIn = 0) OR (bHoldAnalogsDuringCal = 0)

Is this:

CMP2.PNG
 
misunderstanding CMP...

MartyMaucher said:
...This is a simple comparison of a bit to zero.

Yes, you were overthinking it and obviously very new to the instruction set available to you. The Examine-If-Open (XIO) and Examine-If-Closed (XIC) instructions are indeed the best comparison instructions to use on a single BOOL tag value for simple 0 or 1 evaluations. The Equal (EQU) and Limit (LIM) instructions also being of use in that regard.

But, for the Compare (CMP) instruction, as well as not supporting the use of the BOOL data type for tags you add to the expression, it is also important to note that you also cannot use boolean expressions, even when using one of the correct data types, such as the mentioned SINT, INT, DINT, REAL, or String.

An example of a boolean expressions is your "bHoldAnalogsDuringCal = 0". Even if that tag is of the DINT data type you cannot use that expression. Whether entered on their own, as you have done, or even if entered using any of the supported data types, the boolean "= 1" or "= 0" expressions are invalid. This can also be said for the Compute (CPT) instruction.

So an expression such as...

DINT tag:

TagToCompare1 = 0

...is invalid.

As is...

DINT tags:

TagToCompare1 = 0 OR TagToCompare2 = 0

They both contain boolean expressions, which are not supported.

The Compare (CMP) instruction is used with the bitwise operators OR, AND, XOR, NOT. These operators are used to create an expression which evaluates the value of say two tags.

So an expression such as...

DINT tags:

TagToCompare1 OR TagToCompare2

...would be valid.

For the above expression, if both DINT tag values = 0 then the instruction evaluates as FALSE.
If either tag value is a non-zero value then the instruction evaluates as TRUE.

For kicks...

If you wanted to "fool" the instruction and still evaluate a boolean value then you could assign a DINT tag as a BOOL holder and just use the first BOOL of the DINT, say "TagToCompare1.0", as your intended BOOL tag, leaving the rest of the DINT unused. This way, setting and resetting the BOOL within the DINT will only change the DINT value between "0" and "1". Now you have a valid data type, which acts as a boolean.

In the CMP instruction you can enter the expression...

DINT tag:

TagToCompare1

...and it will be valid.

Here, if the DINT tag value is 0, the instruction evaluates as FALSE.
If the tag value is a non-zero value then the instruction evaluates as TRUE.

You could also enter the expression...

DINT tag:

TagToCompare1 OR TagToCompare1

...and it would also be valid, even though the same tag is used twice. This is of no real use but it just demonstrates what is supported and what is not.

To take it a little further to something "useful" you could compare two BOOL values using two DINT tags setup as before, using only the first BOOL element of the DINT.

You could then enter the expression...

DINT tags:

TagToCompare1 OR TagToCompare2

...and again it would be valid.

Here, the result is similar to the earlier example using two actual DINT tags, except you are indirectly comparing two BOOL tag values...

If both BOOL tag values (within the DINTs) = 0 then the instruction evaluates as FALSE.
If either BOOL tag value is a non-zero value then the instruction evaluates as TRUE.

Again, this is just for kicks and only intended to demonstrate not only the aforementioned and unsupported BOOL data type, but also the unsupported boolean expression for these instructions. It would be convoluted to implement this sort of comparison at the boolean level.

Regards,
George
 
Last edited:

Similar Topics

This is a very fundamental scan question. I am not clear as to what the PLC does in the following situation: I have a Home routine that moves 11...
Replies
6
Views
2,802
Situation: Single [EQU] instruction with single [OTE] on a rung, nothing else/simple. [EQU] operand(A) is a real number data type...
Replies
16
Views
2,573
Hi All, I was wondering what your thoughts are on why Rockwell has this disabled by Default on Series B (Series A this is always enabled I...
Replies
4
Views
2,173
I've recently picked up AB again after spending 5 years on Siemens and noticed a frustration immediately with monitoring of CMP (Compare)...
Replies
6
Views
2,763
RSLogix 5000 I am getting a 'CMP, Operand 0: Invalid expression.' for this. It's been a long week, I can't see the problem. (a = (b + 1)) OR...
Replies
7
Views
2,539
Back
Top Bottom