Tidying steps into subroutines
Need: three outputs (0x0400, 0x0401, 0x0402) all get the same check: switch on, wait 100 ms, read back, switch off, read back.
Before
The same 5 steps repeat 3 times, 15 steps in all. Changing the check (the wait time, for example) means editing it in three places.

After
The 5 steps move into the subroutine check_output, with the address as the parameter ${addr}; the case is left with 3 Calls:
"subroutines": [
{
"name": "check_output",
"params": [
{ "name": "addr", "desc": "output register" },
{ "name": "on", "default": 1, "desc": "value that switches the output on" }
],
"steps": [
{ "write": { "function": 6, "address": "${addr}", "value": "${on}" } },
{ "wait": { "ms": 100 } },
{ "read": { "address": "${addr}", "expected": "${on}" } },
{ "write": { "function": 6, "address": "${addr}", "value": 0 } },
{ "read": { "address": "${addr}", "expected": 0 } }
]
}
]

The parameter on defaults to 1, so a Call that leaves it out uses 1. For an output switched on by some other value, add a parameter such as "on": 2 to that Call.

The result is the same as before (PASS). Click the subroutine on the left to see the result of each step inside it.
Notes
- If any step in the subroutine FAILs, the Call that ran it is FAIL.
- Variables set in a subroutine only exist inside it. To bring a result back to the case, return it with Return.
- A Wait's milliseconds must be a fixed number, not a parameter or variable.
Download the sample scripts
Sample scripts
Before running them, start a Slave window on 127.0.0.1:502 and click Listen, then open a script with Open JSON in Script mode and click Run. See How to use the examples.
