41333members
184398posts

Operator precedence in PE programming

Operator precedence in PE programming

Issue

Care must be taken about the precedence of operators in Plain English programming to achieve expected results.

The following PE program was created with the intent of monitoring how long a point has been in alarm and unacknowledged

The user intent was to have the program increment  ‘TimeInALM’ for every minute the point has been in alarm without an ACK

WaitOneMin:
IfTS > 59 then Goto CheckALM
CheckALM:
  TimeInALM = TimeInALM + PSS\ACX2.48\Num1 Alarm1 = On and PSS\ACX2.48\Num1 AckTransToOffNorm <> 255
  IfTimeInALM > 4 thenGotoAlertGuard ElseGotoWaitOneMin
AlertGuard:
  TimeInALM = 0
  GotoWaitOneMin

Instead of incrementing as was the intent, ‘TimeInALM’ kept toggling between 0 and 1.

Product Line

Andover Continuum

Environment

  • Continuum Cyberstation
  • Continuum (All CXs)
  • Continuum Plain English

Cause

The rules of precedence dictate that the + is done first then the AND. So this is what happens:

TimeInALM = TimeInALM + PSS\ACX2.48\Num1 Alarm1 = On and PSS\ACX2.48\Num1 AckTransToOffNorm <> 255

  1. We start with TimeInALM = 0
  2. PSS\ACX2.48\Num1 Alarm1 evaluates to 1 and so does PSS\ACX2.48\Num1 AckTransToOffNorm
  3. So we have
    0+1 = 1 ANDed with 1 = 1
  4. Now TimeInALM = 1, so second pass is
    1+1=2 ANDed with 1 = 0
  5. So TimeInALM is now zero and the cycle repeats

Resolution

Use parentheses to enforce the order of operations.  This resolves potential ambiguities.

TimeInALM = TimeInALM + (PSS\ACX2.48\Num1 Alarm1 = On and PSS\ACX2.48\Num1 AckTransToOffNorm <> 255)

Here is the complete PE Operator Order of Precedence for reference.

PE_OperatorsPrecendenceTable.PNG

Tags (4)
Labels (1)
No ratings