Skip to content

Scanning addresses with loops ​

Need: write patterned data to a block of addresses and check each one, or find the first address that meets a condition. Building the address from a variable saves writing one step per address.

CaseWhat it does
Write a patterni starts at 0; Repeat 16 times: write 0x0100 + ${i} = ${i} * 0x0101, then i += 1
Check the patternReads the 16 addresses back with another Repeat, also expecting ${i} * 0x0101
Find the first zeroaddr starts at 0x0101; While the value at ${addr} is != 0, addr += 1, at most 64 times. Then Log the address
json
{ "set": { "var": "addr", "value": "0x0101" } },
{ "while": { "address": "${addr}", "compare_op": "!=", "expected": 0, "max_loops": 64 } },
{ "set": { "var": "addr", "op": "+=", "value": 1 } },
{ "endwhile": true },
{ "log": "First register that reads 0: ${addr}" }

Address scan result

After 16 passes, the While reads 0 at 0x0110 (Actual column [@0x0110]), and the Log prints First register that reads 0: 272.

Notes

  • Values in the Log and the Variables tab are shown in decimal: 272 is 0x0110. The Hex column of the Variables tab shows hex.
  • Always set max_loops on a While, so a condition that always holds cannot run forever. Going past the limit makes that While step FAIL.
  • A Read inside a Repeat compares on every pass; if any pass fails, that step is FAIL.

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