Skip to content

If / Else / End If ​

Purpose: run one block of steps when a condition holds and another when it does not (Else is optional).

If settings

Based on picks where the condition comes from:

  • Register value: read a register and compare it, with the same fields as Read.
  • Variables / expression: decide with an expression over variables, for example ${n} > 5 && ${n} % 2 == 1.

With Add an Else branch ticked, Else and EndIf are added for you; unticked, only EndIf is added. Put the steps for each branch between If and Else (or EndIf).

JSON ​

json
{ "if": { "address": "0x0300", "expected": 1 } },
{ "log": "0x0300 is 1: If branch" },
{ "else": true },
{ "log": "0x0300 is not 1: Else branch" },
{ "endif": true }

Expression condition:

json
{ "if": { "cond": { "expr": "${n} > 5 && ${n} % 2 == 1" } } }
StepForm
If (register value){"if": {read and compare fields}}. Same fields as Read; it always compares
If (expression){"if": {"cond": {"expr": "expression"}}}. The condition holds when the expression is not 0
Else{"else": true} (optional)
End If{"endif": true}

Result ​

  • The If itself is PASS when the read or calculation succeeds. A condition that does not hold is not a FAIL; the other branch runs instead.
  • Steps in the branch that did not run show SKIP (if skipped (compare), else skipped).
  • If the device does not respond to a register condition: with on_error: skip, the rest of the case is skipped; with continue, the whole If block is skipped and the run carries on after EndIf.

Example ​

CaseWhat it does
Branch on a registerWrites 1, then reads 0x0300 == 1: the If branch runs and the Else branch is SKIP
Branch on an expressionn = 7: "n > 5 and odd" holds; n < 5 does not, so the Log inside it does not run

If example result

Notes ​

  • If, Else and EndIf must pair up, and can be nested. Adding an If on screen inserts the rest for you; in hand-written JSON that does not pair up, the run stops and points at the spot.
  • Disabling an If skips the whole If block.

Download the sample script ​

Sample script

Before running it, start a Slave window on 127.0.0.1:502 and click Listen, then open the script with Open JSON in Script mode and click Run. See How to use the examples.

Roach Modbus Tool by Mr.Roach