While / End While
Purpose: repeat a block of steps while a condition holds, checking the condition again before every pass.

The condition is set up as in If (register value or expression). Max loops is a safety limit, so a condition that never stops holding cannot loop forever.
JSON
json
{ "while": { "cond": { "expr": "${n} < 5" }, "max_loops": 100 } },
{ "set": { "var": "n", "op": "+=", "value": 1 } },
{ "endwhile": true }Register condition:
json
{ "while": { "address": "0x0500", "compare_op": "!=", "expected": 3 } }| Field | Meaning |
|---|---|
| Condition | As in If: cond.expr, or read and compare fields |
max_loops | The most passes to run. Default 1000. 0 means no limit. At most 10000000 |
| End While | {"endwhile": true} |
Result
| Result | When |
|---|---|
| PASS | The condition stops holding and the loop ends normally |
| FAIL | The condition still holds after max_loops passes (the While step shows stopped after N loops (max)). The run then carries on after EndWhile |
A device that does not respond to a register condition is handled as in If.
Example
| Case | What it does | Result |
|---|---|---|
| Expression condition | n counts up from 0 and stops at 5 | PASS |
| Register condition | While 0x0500 is not 3, writes the next number; stops after writing 3, 3 passes in all | PASS |
| Hits max_loops | The condition always holds, so max_loops: 3 stops it after 3 passes | FAIL |

Notes
max_loops: 0means no limit, so make sure the condition eventually stops holding.- The loop needs a step that changes the condition (
Set n += 1or the Write in the example); otherwise it only runs until the limit.
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.
