Skip to content

Training course ​

Every exercise and answer in this course was run and checked in the v1.3.4 (Windows) Release build.

This is a one-day Roach Modbus Tool training course, from Modbus basics all the way to production-line automation. Each unit has key points, hands-on exercises and answers, followed by a final quiz and trainer notes. Every exercise runs on a single PC, with Slave simulating the device, so no real hardware is needed.

Contents ​

SectionTopicTime
1Course overview—
2Before the courseBefore class
3Unit 1: Modbus basics60 min
4Unit 2: Poll hands-on60 min
5Unit 3: Slave hands-on45 min
6Unit 4: Script basics60 min
7Unit 5: Advanced Script60 min
8Unit 6: Production-line automation45 min
9Final quiz20 min
10Trainer notes—

1. Course overview ​

Audience ​

AudienceFocus units
Production test staff1, 2, 4, 6: read values, run ready-made scripts, understand reports and exit codes
Firmware engineersAll: simulate devices with Slave, write verification tests with Script
FAEs (field application engineers)1, 2, 3: connect and debug on site, read frames, reproduce problems with Slave

Course goals ​

After the course, trainees can:

  1. Turn the addresses and data types in a device manual into function codes, addresses and formats in the tool.
  2. Connect to a device with Poll, read and write registers, and set up engineering conversion.
  3. Build a simulated device with Slave and test master programs without real hardware.
  4. Write Script tests that judge PASS / FAIL automatically and export reports.
  5. Run scripts from the command line and use the exit code to integrate with fixtures or schedulers.

Timetable (one day) ​

TimeTopic
09:00–10:00Unit 1: Modbus basics
10:10–11:10Unit 2: Poll hands-on
11:20–12:05Unit 3: Slave hands-on
13:10–14:10Unit 4: Script basics
14:20–15:20Unit 5: Advanced Script
15:30–16:15Unit 6: Production-line automation
16:15–16:35Final quiz
16:35–17:00Review and Q&A

Short on time? Classes for production test staff can skip units 3 and 5.


2. Before the course ​

Each trainee ​

  1. A Windows 10 / 11 PC (or Linux x86_64) that can run two or three program windows.
  2. Roach Modbus Tool installed; see Getting started 2. Install. The first start gives a 30-day trial, so no license is needed for the course.
  3. The course's practice files, downloaded into one folder:
FilePurpose
09-trainer.mbslaveTraining device (Slave project), shared by every unit
u2-answer.mbprojUnit 2 answer (Poll project)
u3-answer.mbslave, u3-check.mbprojUnit 3 answer (Slave project) and the Poll project used to check it
u4-answer.jsonUnit 4 answer (script)
u5-answer.jsonUnit 5 answer (script)
u6-run.batUnit 6 answer (batch file)

Trainers should hand out the answer files only after each exercise is finished.

The training device ​

Every unit connects to the same simulated device: in one window switch to Slave, open 09-trainer.mbslave with File → Open Project..., and click Listen. It listens on 127.0.0.1:502, Slave ID 1, and holds the following (this table is the trainees' "device manual"):

Function areaAddressNameDescriptionInitial value
Holding Registers0x0000Status0 = stopped, 1 = running1
0x0001SetpointSet temperature, in 0.1 °C250
0x0002TemperatureCurrent temperature, in 0.1 °C, signed-55
0x0003Mode0 = Off, 1 = Heat, 2 = Cool1
0x0010–0x0011PressurePressure (bar), 32-bit float, CD AB order1.25
0x0020–0x0029Data 0–9Test data area0
Input Registers0x0000FirmwareFirmware version; 0x0103 means 1.30x0103
Coils0x0000HeaterHeater, 1 = on1
0x0001AlarmAlarm, 1 = alarm active0

If the values get scrambled during the exercises, close the Slave window and open 09-trainer.mbslave again.

Trainer ​

  • Prepare a projector or large screen, and show the Slave and Poll windows side by side when demonstrating.
  • Make sure every PC can listen on 127.0.0.1:502: no other Modbus program on the same PC may hold port 502. If it is taken, Slave shows that Listen failed; see Troubleshooting.
  • The whole course runs on 127.0.0.1 (this PC), so no firewall changes and no network are needed.

3. Unit 1: Modbus basics ​

Key points ​

Roles: the master sends requests and the slave (the device) answers. Poll and Script act as the master; Slave mode acts as the device.

The four data areas and their function codes

Data areaContentsReadWriteCommon manual notation
CoilsCoils (0 / 1), read-writeFC01FC05 (single), FC15 (multiple)0xxxx, for example 00001
Discrete InputsDiscrete inputs (0 / 1), read-onlyFC02—1xxxx, for example 10001
Input Registers16-bit registers, read-onlyFC04—3xxxx, for example 30001
Holding Registers16-bit registers, read-writeFC03FC06 (single), FC16 (multiple)4xxxx, for example 40001

Addresses: addresses in the frame start at 0. When a manual says 40001, it means the first holding register, frame address 0x0000. The tool takes frame addresses and does not subtract 1 for you. A few manuals give frame addresses directly ("register 0", for example); go by what the manual says.

Data types: a register is 16 bits, read as unsigned (0 to 65535) or signed (-32768 to 32767). A 32-bit integer or float takes two registers, and which comes first is up to the device (AB CD, CD AB and so on); see Script core concepts: Value formats.

TCP and RTU: TCP connects by IP and port (502 by default) and puts a 6-byte MBAP header in front of each frame; RTU uses a serial port, has no header, and ends with a 2-byte CRC. Both have a Slave ID.

Exception responses: when a device cannot handle a request, it replies with the function code plus 0x80, followed by an exception code: 01 illegal function, 02 illegal data address, 03 illegal data value, 04 device failure.

Exercise 1-1: Converting addresses ​

Fill in the function code and frame address for each manual entry:

Manual notationTaskFunction codeFrame address
40001Read
40011Write one value
30001Read
00002Read
10002Read

Exercise 1-2: Reading frames ​

Poll reads 4 holding registers from 0x0000 on the training device, and the Communication Log records these two lines (a real capture):

text
TX  00 49 00 00 00 06 01 03 00 00 00 04
RX  00 49 00 00 00 0B 01 03 08 00 01 00 FA FF C9 00 01
  1. What are the TX's transaction ID, Slave ID, function code, start address and quantity?
  2. How many data bytes does the RX return, and what are the four register values?
  3. What is FF C9 as unsigned and as signed? According to the device manual, which one applies, and what is the actual temperature?

Exercise 1-3: Floats and byte order ​

Pressure's two registers, 0x0010 and 0x0011, hold the raw values 0x0000 and 0x3FA0. The manual says the order is CD AB.

  1. Put into AB CD order (high word first), what is the 32-bit value?
  2. 0x3FA00000 is the float 1.25. If you set the format to Float AB CD in the tool, what do you see, and why?

Exercise 1-4: Exception responses ​

Reading a device returns 01 83 02 (RTU, CRC left out). What does it mean, and what should you check first?

Answers ​

1-1

Manual notationFunction codeFrame address
40001 readFC030x0000
40011 write one valueFC060x000A (decimal 10)
30001 readFC040x0000
00002 readFC010x0001
10002 readFC020x0001

1-2

  1. TX: transaction ID 0x0049, protocol 0x0000, length 6, Slave ID 1, function code 03, start address 0x0000, quantity 4.
  2. RX: length 0x0B (11 bytes: Slave ID, function code, byte count, plus 8 data bytes); the 8 data bytes are 0x0001 = 1, 0x00FA = 250, 0xFFC9, 0x0001 = 1.
  3. 0xFFC9 is 65481 unsigned and -55 signed. The manual says Temperature is signed, so it is -55; times 0.1 gives -5.5 °C.

1-3

  1. CD AB puts the low word first; back in AB CD order it is 0x3FA0 0000, which is 1.25.
  2. Float AB CD treats 0x0000 3FA0 as the float, giving a value very close to 0 (about 2.3×10⁻⁴¹), not 1.25. A wrong byte order raises no error; you just read a value that makes no sense, so always check against the manual or a known value.

1-4: 01 is Slave ID 1 and 83 is function code 03 plus 0x80, so it is an exception response; exception code 02 means "illegal data address". First check that the address was converted as the manual says (one too many, or 40001 typed in as 40001), and that the device really has that address.


4. Unit 2: Poll hands-on ​

Reference: Poll guide.

Exercise 2-1: Building a Poll project ​

Open a second window (Poll mode), choose File → New Project, and build a project from the training device's manual:

  1. Rename the device to Trainer, connection 127.0.0.1:502, Slave ID 1.
  2. In 03 Holding Registers, delete the 10 default rows and add 0x0000–0x0003 and 0x0010.
  3. Add 04 Input Registers (0x0000) and 01 Coils (0x0000–0x0001).
  4. Set names, formats and engineering values as below:
AddressNameRaw formatAccessConversion
HR 0x0000StatusUnsignedROValue map: 0 Stopped, 1 Running
HR 0x0001SetpointUnsignedRWScale × 0.1, °C
HR 0x0002TemperatureSignedROScale × 0.1, °C
HR 0x0003ModeUnsignedRWValue map: 0 Off, 1 Heat, 2 Cool
HR 0x0010PressureFloat CD ABROScale × 1, bar
IR 0x0000FirmwareHex—No conversion
Coil 0x0000HeaterUnsignedRWValue map: 0 Off, 1 On
Coil 0x0001AlarmUnsignedROValue map: 0 OK, 1 Alarm
  1. Click Connect and note each row's Eng. Value.
  2. Save it as u2.mbproj with File → Save Project As....

Exercise 2-2: Writing ​

  1. Change the set temperature to 30.0 °C.
  2. Change Mode to Cool.
  3. In the Slave window, check the values of 0x0001 and 0x0003, and the Last access column.

Answers ​

Answer project: u2-answer.mbproj

Unit 2 answer: Holding Registers

NameEng. Value
StatusRunning
Setpoint25 °C
Temperature-5.5 °C
ModeHeat
Pressure1.25 bar
Firmware0x0103
HeaterOn
AlarmOK

2-2:

  • Type 300 (30.0 ÷ 0.1) in Setpoint's Raw column and click Write F6. Engineering values are for display only; writes take the raw value.
  • Type 2 in Mode's Raw column and write it; Eng. Value shows Cool.
  • In the Slave window, 0x0001 becomes 300 and 0x0003 becomes 2, Last access briefly shows an orange Write, and Writes in the status bar goes up.

Common mistakes:

  • Pressure set to Float AB CD, reading a value close to 0.
  • Temperature set to Unsigned, showing 6548.1 °C.
  • Status and Temperature not set to RO, so trainees change them by accident.

5. Unit 3: Slave hands-on ​

Reference: Slave guide.

Exercise 3-1: Building a simulated device ​

A customer's pump station has a Modbus TCP gateway with two pumps behind it. Simulate it with Slave (open a third window, and leave the training device alone):

ItemSetting
Listener127.0.0.1:1502
Pump ASlave ID 1: HR 0x0000 Speed = 1450 (rpm), HR 0x0001 Running = 1, Discrete Input 0x0000 Fault = 0
Pump BSlave ID 2: HR 0x0000 Speed = 0, HR 0x0001 Running = 0, Discrete Input 0x0000 Fault = 0

Hint: first change the device's Listen IP and port to 127.0.0.1:1502, then click + to add the second device. It goes on the same listener, and its Slave ID is set to 2 by itself.

Exercise 3-2: Checking with Poll ​

  1. In Poll, add a device connected to 127.0.0.1:1502 and read ID 1 and ID 2 with Slave ID tabs (or just open u3-check.mbproj).
  2. In Slave, change Pump B's Speed to 1200 and Running to 1, and check that Poll reads them on the next round.
  3. In Slave, click Stop and then Listen, and check that Pump B's values are still there.
  4. In Poll, add a Slave ID 3 tab with one register to the device, and watch Quality.

Answers ​

Answer project: u3-answer.mbslave; for checking: u3-check.mbproj

Unit 3 answer: Slave

Unit 3: Poll reading Pump B (ID 2)

3-2:

  • A changed Value is served to the master at once, so Poll reads 1200 rpm and Running on the next round.
  • Values are kept across Stop and Listen; see Slave guide 7.
  • There is no Slave ID 3 behind the gateway, so Slave does not answer: Poll's Quality shows Timeout and the device turns into an orange N% ok. This is what "one device is not answering" looks like on site.

6. Unit 4: Script basics ​

Reference: Script tutorial, Script core concepts.

Exercise 4-1: A first verification script ​

Open another window and switch to Script (keep the training device listening). Set channel A to TCP 127.0.0.1:502, Slave ID 1, and build two cases:

Status

  1. Read 0x0000 (FC03), expecting 1.
  2. Read 0x0002, format Signed, range -100 to 100 (the temperature must be sensible).
  3. Read input register 0x0000 (FC04), expecting 0x0103 (the firmware version).

Setpoint

  1. Write 0x0001 = 280 (FC06).
  2. Wait 100 ms.
  3. Read 0x0001, expecting 280.
  4. Write 0x0001 = 250 to put the setting back.
  5. Read 0x0001, expecting 250.

After the run, click Export to export an Excel report, and save the script as u4.json.

Exercise 4-2: Making it fail ​

  1. In Slave, change Firmware to 0x0104 and run again. Which step FAILs, and what is the Release result?
  2. In Slave, click Stop and run again. What is the result?

Answers ​

Answer script: u4-answer.json

Actual output from a command-line run (excerpt):

text
Status: PASS
  A 0x0000 = 0x0001 (expect 0x0001)
  A 0x0002 = -55 (expect -100 - 100)
  A 0x0000 = 0x0103 (expect 0x0103)
Setpoint: PASS
  A 0x0001 = 0x0118 (expect 0x0118)
  A 0x0001 = 0x00FA (expect 0x00FA)
PASS 2 - FAIL 0 - SKIP 0 - DISCONNECT 0 - Release PASS

4-2:

  1. Step 3 of Status FAILs (it reads 0x0104, expecting 0x0103), and the Release result is BLOCKED. If any case is not PASS, the release is blocked.
  2. The device cannot be reached, so both cases are DISCONNECT and the Release result is BLOCKED. DISCONNECT means communication failed, not that the device's values were wrong; see Script core concepts.

7. Unit 5: Advanced Script ​

Reference: Script command reference, Script examples.

Exercise 5-1: Subroutines and loops ​

Build a subroutine write_check with parameters addr and value: it writes value to addr, then reads it back and compares.

Then build a case Data block: count a variable i from 0 to 9, calling write_check to write i × 10 into 0x0020 + i. Finish with a Log message saying how many registers were checked.

Exercise 5-2: Conditions and forced verdicts ​

Build a case Mode and temperature:

  1. Read 0x0002 (Signed) without comparing, store it in variable t, and compute temp = t / 10.0.
  2. If Mode (0x0003) is 1 (Heat):
    • Log the current temperature.
    • If t < 0: Log a warning, and read Coil 0x0000 (Heater) expecting 1.
  3. Otherwise: Log "Mode is not Heat" and force the verdict to FAIL.

Answers ​

Answer script: u5-answer.json

Unit 5 answer: the run result

Key points:

  • 0x0020 + ${i} and ${i} * 10 are both expressions: addresses and values can both be built from variables.
  • Subroutine parameters are passed in the call, for example {"call": {"sub": "write_check", "args": {"addr": "0x0020 + ${i}", "value": "${i} * 10"}}}.
  • If can be nested. Here the outer If uses a read condition (read Mode and compare), the inner one an expression condition (${t} < 0).
  • Going further: in Slave, change Mode to 2 (Cool) and run again. The second case takes the Else branch and FAILs, and the command-line exit code is 1.

8. Unit 6: Production-line automation ​

Reference: Command-line runs.

Key points ​

text
RoachModbusTool --run-script <script.json> [--report <report.xlsx or .csv>] [--loops N] [--case <case name>] [--log <log.txt>]
Exit codeMeaning
0Release PASS
1Some FAIL, SKIP or DISCONNECT
2Could not run (file, format, serial port or option error)
3No license

On Windows you need start /wait, or PowerShell's Start-Process -Wait, to get the exit code.

Exercise 6-1: Running from the command line ​

  1. Run u4.json from the command line for three rounds, writing the report report.xlsx and the log log.txt.
  2. Get the exit code.
  3. Click Stop in Slave and run again. What is the exit code now?

Exercise 6-2: A batch file ​

Write a batch file that sits next to RoachModbusTool.exe: it runs the Unit 4 script for three rounds, names the report and log after the date and time, shows PASS, FAIL or ERROR according to the exit code, and passes the exit code back to whatever called it (fixture software or a scheduler).

Answers ​

6-1:

bat
start /wait "" RoachModbusTool.exe --run-script u4.json --loops 3 --report report.xlsx --log log.txt
echo %ERRORLEVEL%

With Slave running, the exit code is 0. With Slave stopped, the device cannot be reached, the result is DISCONNECT, and the exit code is 1.

6-2: u6-run.bat

bat
@echo off
rem Run the Unit 4 script three rounds and keep a report named after the date and time.
rem Put this file next to RoachModbusTool.exe and u4-answer.json.
setlocal
cd /d "%~dp0"
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set STAMP=%%i
start /wait "" RoachModbusTool.exe --run-script u4-answer.json --loops 3 --report report_%STAMP%.xlsx --log log_%STAMP%.txt
set RC=%ERRORLEVEL%
if %RC%==0 (
  echo PASS
) else if %RC%==1 (
  echo FAIL: see report_%STAMP%.xlsx
) else (
  echo ERROR %RC%: the script could not run
)
exit /b %RC%

Tested: with Slave listening it shows PASS with exit code 0; with Slave stopped it shows FAIL: see report_20261126_070515.xlsx with exit code 1. Every run produces report_<date>_<time>.xlsx and log_<date>_<time>.txt.

Notes:

  • cd /d "%~dp0" makes the batch file run in its own folder, wherever it is called from.
  • The timestamp comes from PowerShell, so it does not depend on Windows regional settings (the format of %date% changes with them).
  • exit /b %RC% hands the exit code back to the caller, so fixture software can check it directly.

9. Final quiz ​

Multiple choice, 5 points each, 15 questions. The answers are at the end of this section.

  1. A device manual says "40005: setpoint". Which function code and address read it in the tool? (A) FC03, 0x0005 (B) FC03, 0x0004 (C) FC04, 0x0004 (D) FC04, 0x0005
  2. Which data area can a master write? (A) Discrete Inputs (B) Input Registers (C) Holding Registers (D) All of the above
  3. A register reads 0xFF9C, and the manual says it is a signed temperature in 0.1 °C. What is the temperature? (A) 6545.2 °C (B) -10.0 °C (C) -0.1 °C (D) 10.0 °C
  4. What does the response 01 86 02 mean? (A) Write succeeded (B) FC06 exception, illegal data address (C) FC06 exception, illegal data value (D) CRC error
  5. Which is true about Poll's Raw column and Eng. Value? (A) To write 25.0 °C (× 0.1), type 250 in Raw (B) Typing 25.0 in Eng. Value is enough (C) They are always the same (D) Eng. Value is stored in the device
  6. In Poll, how do you auto poll only certain rows? (A) Delete the other rows (B) Mark only those rows with ★ (C) Check those rows and click Read F5 (D) Make the poll interval longer
  7. One Poll device needs to read Slave IDs 1 and 2 behind the same gateway. What do you do? (A) It cannot be done (B) Add ID 2 as a Slave ID tab (C) Increase Timeout (D) Run two copies of the program
  8. When Slave receives a request for a Slave ID it does not have, it: (A) Replies with exception 02 (B) Replies with 0 (C) Does not answer, so the master times out (D) Adds that ID by itself
  9. After Stop and Listen in Slave, values the master wrote earlier are: (A) Cleared to 0 (B) Restored from the project file (C) Kept (D) Random
  10. In a Script case, a read value does not match what is expected. The result is: (A) DISCONNECT (B) FAIL (C) SKIP (D) PASS
  11. While a script runs, the device cannot be reached. The case's result is: (A) FAIL (B) DISCONNECT (C) PASS (D) The program crashes
  12. If any case is not PASS, the Release result is: (A) PASS (B) BLOCKED (C) FAIL (D) It depends on the number of cases
  13. Which one lets a Read's address change with a loop? (A) "address": "0x0020 + ${i}" (B) "address": "${0x0020+i}" (C) Addresses must be fixed (D) "address": "i"
  14. A command-line run has a FAIL. The exit code is: (A) 0 (B) 1 (C) 2 (D) 3
  15. To get the exit code of RoachModbusTool --run-script in the Windows Command Prompt, you must: (A) Just run it (B) Use start /wait (C) Add --wait (D) Switch to RTU

Quiz answers ​

QAnswerWhy
1B4xxxx is a holding register; 40005 is the 5th, frame address 0x0004, read with FC03
2CDiscrete Inputs and Input Registers are read-only to the master
3B0xFF9C signed is -100; × 0.1 = -10.0 °C
4B86 = 06 + 0x80; exception 02 is illegal data address
5AEngineering values are for display only; writes take the raw value in Raw
6BAuto poll only reads ★ rows
7BSeveral Slave IDs on the same IP and port go in Slave ID tabs
8CWithout that Slave ID it does not answer
9CAfter Stop and Listen, every address is back to its value before Stop
10BA mismatch is FAIL
11BA communication failure is DISCONNECT
12BRelease PASS needs every case to PASS
13AAddress fields take expressions, with variables written as ${name}
14B0 PASS, 1 some FAIL / SKIP / DISCONNECT, 2 could not run, 3 no license
15BThe program is a Windows GUI program, so you have to wait for it to finish explicitly

80 points (12 questions) or more is a pass.


10. Trainer notes ​

Before the course ​

  • Run every exercise once on the trainer's PC, and make sure ports 502 and 1502 are not taken by other programs.
  • Other Modbus software on trainee PCs (a PLC development environment's simulator, for example) may hold port 502; close it beforehand.
  • The trial lasts 30 days from the first start. On PCs installed early for testing, check the trial will still be running on the day.

Reminders by unit ​

UnitCommon trainee problemWhat to do
1Using 40001 as the addressStress that frame addresses start at 0, and compare with the hint Monitors wire address N at the bottom of Poll's Add register window
1Not seeing why signed and unsigned matterShow FF C9 from exercise 1-2: the same bits read two very different ways
2Pressure reads close to 0Wrong byte order; change it to Float CD AB
2Changing Eng. Value does nothingEngineering values are for display only; write by changing Raw
2A written value goes back to the old oneWrite F6 was never clicked, only the value on screen changed (orange Changed)
3The second device became another listenerDevices added with + go on the first device's listener; if the first device's port was changed first, make sure both use the same port
3Poll does not change after a Slave editPoll is not polling (state Connected, not Polling), or that row has no ★
4Every step of the script is DISCONNECTThe training device is not listening, or channel A's port or Slave ID is wrong
4Script and Slave in the same windowSwitching to Script mode stops Slave's listener; always use separate windows
5Expressions have no effectVariables are written ${i}, not i
6No exit code (always 0)start /wait was not used; see Troubleshooting 7

Resetting the practice environment ​

  • The training device's values are scrambled: close the Slave window and open 09-trainer.mbslave again. The project file keeps the initial values, and is not overwritten unless someone clicks Save Project.
  • A trainee saved over 09-trainer.mbslave: download a fresh copy.

Extra exercises for advanced classes ​

  • Turn the Unit 4 script into a two-channel test: channel A writes to the training device and channel B reads the Unit 3 pump station, to practise gateway forwarding; see Script examples 3.
  • Use Ask (text input) to have the operator enter a serial number and write it into the report; see Script examples 7.
  • Simulate a power cycle with Slave's Stop / Listen and Wait Until; see Script examples 4.

Roach Modbus Tool by Mr.Roach