Skip to content

Script tutorial: your first script ​

This chapter starts from an empty script and builds your first automated test on screen, one step at a time: write a register → wait → read it back and compare → log the result. At the end you have a script that you can run again and again, that decides PASS or FAIL by itself, and an Excel report.

Every screen and result here was checked on the v1.3.4 (Windows) Release build. The sample script and report used in this chapter can be downloaded from the links in the text.

0. Before you start: a device to talk to ​

Follow step 1 of Getting started › First connection: open another window, switch it to Slave and click Listen, so it acts as a device on 127.0.0.1:502. The script in this chapter connects to that simulated device.

If you have a real device, point channel A at its IP or serial port in section 2 instead.

1. Open Script mode ​

Click Script on the left. A new script already has one empty case, named Test 1.

Script mode (new script)

The main areas:

AreaWhat it is
Toolbar (top)New, Open JSON, Save JSON, undo, redo; Run F5, Pause F6, Step F10, Stop Esc; Loop (1×/N×/∞); Pause on FAIL; Export
CONNECTIONS (left)Connection cards for channels A and B. The switch turns a channel on or off, and the ⇄ icon opens its settings
CASES / SUBROUTINES (left)The list of test cases and subroutines. + adds one
REPORT & ERRORS (bottom left)Firmware: the firmware version text written into the report. On comm error: what to do when communication fails (see Concepts)
Step table (centre)The steps of the current case. After a run, it shows each step's Result, Check (expected value) and Actual (actual value)
Output (bottom)The Log and Variables tabs

2. Set up channel A ​

Click the ⇄ icon at the top right of the channel A card (or Connection → Set Channel A...) to open its connection settings.

Modbus TCP (used in this tutorial; keep the defaults):

Channel A settings (TCP)

FieldWhat it setsThis tutorial
RoleMaster: read and write the device. Slave: make this channel act as a deviceMaster
ModeModbus TCP or Modbus RTUModbus TCP
Slave IDThe device's unit ID1
TCP IPThe device's IP127.0.0.1
TCP PortThe device's port502
TimeoutHow long to wait for a reply1000 ms
RetriesHow many times to retry when there is no reply3

Modbus RTU: switch Mode to Modbus RTU and the IP and port give way to serial settings:

Channel A settings (RTU)

FieldWhat it sets
COM PortThe serial port, for example COM3 (/dev/ttyUSB0 on Linux)
BaudThe baud rate, for example 9600, 19200 or 115200
Data bits / Parity / Stop bitsData bits, parity (None/Even/Odd) and stop bits. They must match the device

Click OK. The channel A card shows a summary of the connection, for example 127.0.0.1:502 · Slave ID 1. This tutorial uses only channel A, so leave channel B off (Off).

3. Add steps ​

Click Add Step at the top right of the step table (or + Add step at the end below it) to open the add-step window:

Add step window

  • The left side lists every step type, in five groups: I/O, FLOW, VARIABLES, TIMING & RESULT and INSPECT.
  • The right side holds the settings of the selected step.
  • PREVIEW at the top summarises the step as you edit. The top right shows Ready when the settings are complete, or Needs a fix when a field still needs attention, with the reason shown below.
  • Add adds the step and closes the window. Add & next adds it and opens a new add-step window straight away, which is quicker when adding several steps.

We will add four steps in order.

Step 1: Write ​

  1. Click Write on the left.
  2. Keep Function at 06 - Write Single Register, Channel at A and Start address at 0x0000.
  3. Enter 0x1234 in Write value.
  4. PREVIEW shows Write FC06 A 0x0000 = 0x1234. Click Add & next.

Add a Write step

Addresses and values can be decimal (4660) or hex (0x1234). Hex always needs the 0x.

Step 2: Wait ​

  1. Click Wait on the left.
  2. Keep the default 100 ms.
  3. Click Add & next.

The wait gives the device time to process the write. If a real device needs longer, raise it.

Step 3: Read back and compare ​

  1. Click Read on the left.
  2. Keep Function at 03 - Read Holding Registers. Start address and End address are both 0x0000 (one register).
  3. Set Compare to = equals and enter 0x1234 in Expected value.
  4. Enter v in Variable to store the value read in variable v, which the next step uses.
  5. PREVIEW shows Read FC03 A 0x0000 == 0x1234 → v. Click Add & next.

Add a Read step

The four Compare choices:

ChoicePASS when
No checkThe device replies, whatever the value
= equalsThe value read equals the expected value
≠ not equalThe value read differs from the expected value
In rangeThe value read is between the lower and upper limits

Step 4: Log a line ​

  1. Click Log on the left.
  2. Enter Read back 0x0000 = ${v} in Message. At run time, ${v} is replaced with the value of variable v.
  3. Click Add.

Add a Log step

The step table now has four steps:

Four steps added

To edit a step, double-click it (or select it and press Enter). Del deletes it. Drag the ⋮⋮ handle or press Ctrl+↑/↓ to reorder. Ctrl+/ disables or enables the step. Right-click for more.

4. Run ​

Click Run F5. A confirmation window first lists the channels this run will connect, how communication failures are handled, and the number of rounds:

Run confirmation

If it looks right, click Run. When the run finishes:

Result: all PASS

  • The top shows Release PASS and PASS 1 / FAIL 0 / SKIP 0 / DISCONNECT 0.
  • Every step's Result is PASS, and the Actual column shows the value read and the response time, for example 0x1234 · 0.3 ms [v = 4660].
  • The case card on the left is ticked and shows 4 step(s) · PASS.
  • The Log tab records the whole run, and its last line is the path of the log file. Click Open log folder at the top right to open the folder.

The Log step prints Read back 0x0000 = 4660: ${v} is shown in decimal, and 4660 is 0x1234.

Switch to the Slave window: 0x0000 now holds 4660, and the Writes counter has gone up:

The Slave received the write

The Variables tab lists the variables this run created, with their decimal and hex values and the step that set them:

Variables tab

Other ways to run ​

Button / shortcutWhat it does
Run F5Run every case from the start
Script → Run from Selected Step (Ctrl+F5)Run one round starting at the selected step
Step F10Run one step, then pause. Handy for debugging
Pause F6 / Resume F6Pause / resume
Stop EscStop the run
Pause on FAILWhen ticked, the run pauses on a FAIL so you can inspect the device on the spot

5. Save the script ​

Click Save JSON in the toolbar (or File → Save Script As...) and choose a location and name. A script is a JSON text file. Saved, the script you just built looks like this (first-script.json):

json
{
    "cases": [
        {
            "steps": [
                {
                    "write": {
                        "address": "0x0000",
                        "channel": "A",
                        "function": 6,
                        "value": "0x1234"
                    }
                },
                {
                    "wait": {
                        "ms": 100
                    }
                },
                {
                    "read": {
                        "address": "0x0000",
                        "channel": "A",
                        "compare_op": "==",
                        "expected": "0x1234",
                        "function": 3,
                        "store": "v"
                    }
                },
                {
                    "log": {
                        "message": "Read back 0x0000 = ${v}"
                    }
                }
            ],
            "test": "Test 1"
        }
    ],
    "channel_a": {
        "enabled": true,
        "ip": "127.0.0.1",
        "port": 502,
        "retry": 3,
        "role": "master",
        "slave_id": 1,
        "timeout_ms": 1000,
        "transport": "tcp"
    },
    "channel_b": {
        "enabled": false,
        "ip": "127.0.0.1",
        "port": 502,
        "retry": 3,
        "role": "master",
        "slave_id": 1,
        "timeout_ms": 1000,
        "transport": "tcp"
    },
    "firmware": "unknown",
    "loop": 1,
    "loop_infinite": false,
    "on_error": "skip",
    "same_line": false
}

The file holds the steps, the connection settings of both channels, the number of rounds and how communication failures are handled, so opening it on another PC runs it with the same settings. Each field is explained in Script concepts.

The status next to the file name shows Unsaved (never saved), Modified (unsaved changes) or Saved.

6. Export a report ​

After a run, click Export (or Script → Export Excel) and choose where to save. The default name is "script name_report.xlsx", for example first-script_report.xlsx (download the sample report). The report has three sheets:

SheetContents
SummaryFirmware, case count, PASS/FAIL/SKIP/DISCONNECT counts and the Release verdict
CasesEach case's run time, name and verdict
StepsEach step's time, case, step number, action, channel, function code, start and end address, actual value, expected value, verdict, note and response time (ms)

Export is available once the script has run, and exports the most recent run.

7. Open existing scripts and samples ​

  • Click Open JSON in the toolbar (or File → Open Script...) to open a script file.

  • The scripts folder in the app folder holds sample scripts, for example:

    SampleWhat it does
    A single.jsonWrite one register on channel A and read it back
    A consecutive.json, A multi-segment.jsonA consecutive block, several blocks
    A to B single.json and othersWrite on channel A, read back on channel B (gateway tests)
    Power cycle.jsonPower-cycle test
    Showcase - all features.jsonShows every feature
  • Script → Import Template... adds a case from a template: one channel stands for one device, two channels for "write on one side, read on the other".

8. Next steps ​

  • Script concepts: results, communication failures, variables, multiple rounds and value formats

Roach Modbus Tool by Mr.Roach