Skip to content

While / End While ​

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

While settings

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 } }
FieldMeaning
ConditionAs in If: cond.expr, or read and compare fields
max_loopsThe most passes to run. Default 1000. 0 means no limit. At most 10000000
End While{"endwhile": true}

Result ​

ResultWhen
PASSThe condition stops holding and the loop ends normally
FAILThe 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 ​

CaseWhat it doesResult
Expression conditionn counts up from 0 and stops at 5PASS
Register conditionWhile 0x0500 is not 3, writes the next number; stops after writing 3, 3 passes in allPASS
Hits max_loopsThe condition always holds, so max_loops: 3 stops it after 3 passesFAIL

While example result

Notes ​

  • max_loops: 0 means no limit, so make sure the condition eventually stops holding.
  • The loop needs a step that changes the condition (Set n += 1 or 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.

Roach Modbus Tool by Mr.Roach