Skip to content

Script concepts ​

This chapter explains how a script runs, how results are decided, and how variables, expressions and value formats work. Each concept comes with a sample script you can run and a screenshot of the actual run.

Every screen and result here was checked by running it on the v1.3.4 (Windows) Release build. Each sample script can be downloaded from the links in the text. Except in section 4, the samples connect to 127.0.0.1:502, so first start a Slave window as in Getting started › First connection (default registers 0x0000–0x0009).

1. Script structure ​

A script file (.json) has three parts:

text
Script
├─ Global settings: channels A/B, rounds, comm failure handling, Firmware
├─ cases (test cases)
│   ├─ case 1: steps …
│   ├─ case 2: steps …
│   └─ …
└─ subroutines (optional)

Global fields:

FieldOn screenMeaning
channel_a, channel_bCONNECTIONS, leftConnection settings of the two channels (see the next table)
loopLoop in the toolbar: 1×/N× and the numberHow many rounds to run
loop_infiniteLoop in the toolbar: ∞true runs until you stop it
on_errorOn comm error, bottom leftWhat to do when communication fails: skip or continue (see section 4)
firmwareFirmware, bottom leftThe firmware version text written into the report
same_lineConnection → Same line: B uses AChannel B shares channel A's connection (see section 9)
casesCASES, leftThe list of test cases

Channel fields:

FieldMeaning
enabledWhether the channel is on
rolemaster (reads and writes the device) or slave (acts as a device)
transporttcp or rtu
ip, portTCP IP and port
slave_idThe unit ID
timeout_ms, retryReply timeout and number of retries

Each case has a name, test, and a list of steps. Each step is an object with a single key, and the key is the step type, for example {"write": {...}} or {"wait": {"ms": 100}}. Every step is described in full in the Script command reference.

2. How a run works: rounds, cases, steps ​

  • A round (loop): every case runs once, top to bottom.
  • Several rounds: set Loop to N× to repeat N rounds, or ∞ to repeat until you click Stop.
  • Variables: carry over between cases in the same round, and are all cleared when a round starts (see section 6).
  • End Case: skips the rest of the current case and moves on to the next case.
  • End Round: ends the round, so the remaining cases do not run this round. If there is another round, it starts again from the first case.

Example: End Case and End Round ​

04-end-case-round.json, run for 2 rounds:

json
"cases": [
    {"test": "01 End Case", "steps": [
        {"log": {"message": "case 1: before End Case"}},
        {"end_case": true},
        {"log": {"message": "case 1: this line does not run"}}]},
    {"test": "02 End Round", "steps": [
        {"log": {"message": "case 2: before End Round"}},
        {"end": true}]},
    {"test": "03 Does not run", "steps": [
        {"log": {"message": "case 3: this case does not run this round"}}]}
],
"loop": 2

End Case and End Round

What happened:

  • Step 3 of case 1 shows SKIP, but case 1 itself is PASS (a skipped step does not make the case SKIP).
  • Case 2 reaches End Round and the round ends. Case 3 never runs and gets no verdict on the left.
  • Both rounds behave the same, for a total of PASS 4: case 1 and case 2 once per round. Case 3 never ran, so it is not counted at all.

3. Results ​

Step results ​

ResultMeaning
PASSThe step ran; if it compares, the value matched
FAILThe value did not match, an expression failed, the response was too slow, or a While went past its loop limit
DISCONNECTCommunication failed: could not connect, timed out, or no reply
SKIPThe step did not run (skipped by End Case, skipped after a comm failure under skip, disabled, and so on)

Case results ​

Decided in this order:

  1. Any step FAIL → the case is FAIL
  2. Otherwise, any step DISCONNECT → the case is DISCONNECT
  3. No step ran at all (all disabled, for example) → the case is SKIP
  4. Anything else → the case is PASS

If the case ran a Verdict step, the PASS or FAIL it sets wins.

Overall result: Release PASS / Release BLOCKED ​

The PASS/FAIL/SKIP/DISCONNECT counters at the top count cases, not steps.

  • Every case that ran is PASS → Release PASS
  • Any case is FAIL, SKIP or DISCONNECT → Release BLOCKED

Example: PASS and FAIL ​

01-results.json:

json
"cases": [
    {"test": "01 Write then read back", "steps": [
        {"write": {"channel": "A", "function": 6, "address": "0x0000", "value": "0x1234"}},
        {"read": {"channel": "A", "function": 3, "address": "0x0000", "compare_op": "==", "expected": "0x1234"}}]},
    {"test": "02 Unexpected value", "steps": [
        {"write": {"channel": "A", "function": 6, "address": "0x0001", "value": "0x0000"}},
        {"read": {"channel": "A", "function": 3, "address": "0x0001", "compare_op": "==", "expected": "0x00FF"}}]},
    {"test": "03 Read without check", "steps": [
        {"read": {"channel": "A", "function": 3, "address": "0x0002", "compare": false}}]}
]

PASS 2, FAIL 1, Release BLOCKED

What happened: PASS 2 / FAIL 1, and overall Release BLOCKED. The FAIL line in the Log shows both the actual and the expected value:

text
02 Unexpected value: FAIL
  A 0x0001 = 0x0000 (expect 0x00FF)  FAIL

Case 3's Read uses "compare": false (No check on screen), so it passes as long as the device replies, and the Check column shows any.

4. Handling communication failures (on_error) ​

On comm error at the bottom left decides whether the rest of the same case runs after a step fails to communicate (DISCONNECT):

On screenJSONBehaviour
Skip rest of case"on_error": "skip"Skip the rest of this case (shown as SKIP) and go to the next case
Continue"on_error": "continue"Keep running the rest of this case

With either setting, the next case runs as usual, and the case that failed is judged DISCONNECT.

Example: the device is unreachable ​

02-on-error-skip.json and 02-on-error-continue.json are identical apart from on_error. Channel A deliberately points at port 1503, where nothing is listening:

json
"cases": [
    {"test": "Comm failure demo", "steps": [
        {"log": {"message": "Step 1: start"}},
        {"read": {"channel": "A", "function": 3, "address": "0x0000", "compare": false}},
        {"log": {"message": "Step 3: after the read"}}]},
    {"test": "Next case", "steps": [
        {"log": {"message": "Next case runs as usual"}}]}
]

skip: step 3 shows SKIP, and the Actual column notes on_error skip.

on_error = skip

continue: step 3 runs, and its result is PASS.

on_error = continue

Also note:

  • The channel A card shows Not answering yet. A device that does not answer does not stop the run: every later step that communicates tries to connect again.
  • The reason for the failure is in the Actual column and the Log, for example comm fail: connect failed: connection refused.
  • Under skip, step 3 is SKIP but the SKIP counter at the top stays at 0, because the counters count cases.

5. Writing addresses and values ​

FormExampleNotes
Decimal100
Hex0x0064Always needs the 0x
Binary0b1010
Decimal fraction230.5, 2e3Only meaningful with float formats
Expression${base}+${i}*2See section 7

A Read or Write covers address (start) to end_address (end, inclusive). With only address, it covers one register. On screen, Start address and End address show the register count next to them (for example 1 register).

Addresses are protocol addresses (PDU addresses) starting at 0x0000, not numbers like 40001.

6. Variables ​

Setting and using them ​

  • A Set variable step sets a variable: {"set": {"var": "n", "value": "3"}}
  • It can also update one with an operator: =, +=, -=, *=, /=, %=, for example {"set": {"var": "n", "op": "+=", "value": "1"}}. For anything other than =, the variable must already be set, or the step fails (undefined variable n (set it with = first)).
  • A Read's Variable (store in JSON) stores the value read in a variable.
  • Use a variable in any field as ${name}. The ${ } is required: a bare n is an error, which catches typos.
  • Variable names start with a letter or underscore, contain letters, digits and underscores, and are at most 31 characters long.

Storing several values from one read (store_mode) ​

On screenJSON store_modeWhat is stored
First valuefirst (default)v = the first value
Each valueeachv_0, v_1, v_2… one variable per value, and v equals v_0
32-bit, high firstu32_hi_firstReads 2 registers, v = (r0 << 16) | r1
32-bit, low firstu32_lo_firstReads 2 registers, v = (r1 << 16) | r0

Scope ​

  • Within a round, variables carry into later cases.
  • At the start of each round, every variable is cleared.
  • The built-in variable ${now} is the number of milliseconds since this round started (an integer), and restarts from 0 each round. It is not the current date or time.

Example: variables, expressions and two rounds ​

03-variables.json, run for 2 rounds:

json
"cases": [
    {"test": "01 Set and calculate", "steps": [
        {"log": {"message": "Round started, ${now} ms elapsed"}},
        {"set": {"var": "base", "value": "0x0000"}},
        {"set": {"var": "n", "value": "3"}},
        {"write": {"channel": "A", "function": 16, "address": "${base}", "end_address": "${base}+${n}-1", "value": "${n}*100"}},
        {"read": {"channel": "A", "function": 3, "address": "${base}", "end_address": "${base}+${n}-1",
                  "compare_op": "==", "expected": "300", "store": "r", "store_mode": "each"}},
        {"log": {"message": "r_0=${r_0} r_1=${r_1} r_2=${r_2}"}},
        {"set": {"var": "n", "op": "+=", "value": "1"}}]},
    {"test": "02 Variables carry to the next case", "steps": [
        {"log": {"message": "n = ${n}, ${now} ms elapsed"}}]}
],
"loop": 2

Variables tab

What happened (Log excerpt):

text
--- Loop 1/2 ---
[01 Set and calculate #1, loop 1]  Round started, 0 ms elapsed
[01 Set and calculate #6, loop 1]  r_0=300 r_1=300 r_2=300
[02 Variables carry to the next case #1, loop 1]  n = 4, 0 ms elapsed
--- Loop 2/2 ---
[01 Set and calculate #1, loop 2]  Round started, 0 ms elapsed
[01 Set and calculate #6, loop 2]  r_0=300 r_1=300 r_2=300
[02 Variables carry to the next case #1, loop 2]  n = 4, 0 ms elapsed
  • The write range comes from variables: ${base} to ${base}+${n}-1, which is 0x0000–0x0002, three registers, each written with ${n}*100 = 300.
  • Case 1 ends by adding 1 to n, and case 2 sees n = 4: variables carry across cases.
  • In round 2, case 2 still sees n = 4, not 5: variables start over every round.
  • The Variables tab lists each variable's decimal and hex value, and which step of which case set it.
  • ${variable} in a message is always shown in decimal.

7. Expressions ​

Expressions work in addresses, values, expected values, Set, and If/While conditions.

KindOperatorsExample
Arithmetic+ - * / %${base}+${i}*2
Comparison== != < <= > >=${t} > 21.5
Logic&& || !${i} < 10 && ${ok}
Bitwise& | ^ ~ << >>${s} & 0x08
Parentheses( )(${a}+${b})/2

Rules:

  • Integer with integer gives an integer: 7 / 2 = 3. If either side has a fraction, the result does too: 7 / 2.0 = 3.5.
  • Comparisons and logic give 1 (true) or 0 (false).
  • Bitwise operators bind tighter than comparisons: ${s} & 0x08 == 0x08 means (${s} & 0x08) == 0x08.
  • && and || only evaluate the right side when needed, so ${n} != 0 && ${x} / ${n} > 2 never divides by zero.
  • % and the bitwise operators only work on integers.

8. Value formats and byte order ​

Modbus registers are 16 bits. 32-bit integers and floats take 2 registers, and doubles take 4. A Read or Write's Format (format in JSON) decides how they are read:

FormatJSONRegistersRange
Unsignedunsigned (default)10–65535
Signedsigned1-32768–32767
Long (32-bit integer)long_abcd, long_cdab, long_badc, long_dcba2Signed 32-bit
Float (single precision)float_abcd, float_cdab, float_badc, float_dcba2IEEE 754
Double (double precision)double_abcdefgh, double_ghefcdab, double_badcfehg, double_hgfedcba4IEEE 754

The letters give the byte order: ABCD is high word first (big-endian), and CDAB swaps the two words. The device manual usually states the order. With the wrong order, you read a completely different number.

Limits:

  • The address range must be a whole multiple of the register count, for example 0x0004–0x0005 (two registers) for a float.
  • Formats other than Unsigned only work with register function codes: FC03, FC04, FC16, and FC06 for single-register formats. Coils and Discrete Inputs are always Unsigned.

Example: floats and byte order ​

05-formats.json:

json
{"write": {"channel": "A", "function": 16, "address": "0x0004", "end_address": "0x0005", "format": "float_abcd", "value": "230.5"}},
{"read":  {"channel": "A", "function": 3, "address": "0x0004", "end_address": "0x0005", "format": "float_abcd",
           "compare_op": "==", "expected": "230.5", "store": "f"}},
{"read":  {"channel": "A", "function": 3, "address": "0x0004", "end_address": "0x0005", "store": "raw", "store_mode": "each"}},
{"read":  {"channel": "A", "function": 3, "address": "0x0004", "end_address": "0x0005", "format": "float_cdab", "store": "wrong"}},
{"log":   {"message": "ABCD=${f}  raw registers=${raw_0},${raw_1}  read as CDAB=${wrong}"}}

Floats and byte order

What happened:

text
A 0x0004-0x0005 = 230.5 (expect 230.5)
A 0x0004-0x0005 = 0x4366, 0x8000
A 0x0004-0x0005 = -2.4178e-41
  • Writing 230.5 as float_abcd puts 0x4366 and 0x8000 in the registers.
  • Reading back with the same float_abcd gives 230.5, and the compare passes.
  • Reading the same two registers as float_cdab gives -2.4178e-41. When you read a strangely tiny or huge number, check the byte order first.

9. Two channels: A and B ​

Channels A and B are two independent Modbus connections (Help → Script Channels also explains them):

UseSetup
Test one deviceTurn on A only
Test a gateway or converter, or compare two devicesTurn on A and B: write on one channel and read back on the other to confirm the value got through
Two unit IDs on the same lineConnection → Same line: B uses A: B shares A's connection but uses its own Slave ID
Make the tool act as a deviceSet the channel's Role to Slave: it listens on its Listen IP (default 127.0.0.1, reachable only from this PC) and serves a register table, and steps on that channel read and write that table

Each I/O step picks its channel with channel ("A" or "B"). The A to B … and B to A … samples in the scripts folder show how gateway tests are written; the Script examples cover them in detail.

10. Disabled steps and response time limits ​

  • Disabled: select a step and press Ctrl+/, or add "disabled": true in JSON. A disabled step is skipped at run time. Disabling an If, Repeat or While skips the whole block.
  • Response time limit (Max response, max_ms in JSON): if the device takes longer than this many milliseconds to reply, the step fails. The default is No limit. Use it to check a device's response time spec.

11. Next steps ​

Roach Modbus Tool by Mr.Roach