Modbus TCP master over a TCP client
You will find the project Qenex_TcpClient_ModbusMaster_HoldingRegisters.qproj
in the Examples folder next to QInsight.exe.
What the example shows
QInsight acts here as a Modbus TCP master: the TCP Client driver
holds the connection to the slave device and the Modbus Master
protocol in tcp mode periodically reads three holding-register items
over it — two scalars (EngTorque, Torque) and one 3×2 matrix
(Drivability). Variables with direction readWrite can be written back
to the slave from the controls.
The second topic is connection resilience: the driver has TCP keepalive and endless reconnection enabled, so a pulled cable or a slave restart is survived without any operator action.
What you need
- A Modbus TCP slave with unit id 2 listening on port 502 — e.g. pyModSlave in TCP mode with the unit address set to 2. It can run on the same PC as QInsight or on a second PC in the network.
- The driver in the project is set to
ip=192.168.2.208. Change it to your slave's address (127.0.0.1when it runs on the same PC) — otherwise the driver stays Faulted and keeps trying to connect forever. - Firewall: the connection is initiated by QInsight (the client), so the rule belongs on the PC running the slave — it must allow inbound connections on port 502. No inbound rule is needed on the QInsight PC.
The project
In the configuration the project is named Modbus TCP Master. Its
description mentions a slave at 127.0.0.1:502 and a 500 ms period —
the actual values in the settings are 192.168.2.208 and 1000 ms (see
below).
Drivers and protocols
The TCP Client Driver (TcpClientDriver):
| Parameter | Value | Meaning |
|---|---|---|
ip | 192.168.2.208 | slave address — change it to match your network |
port | 502 | the standard Modbus TCP port |
connectionTimeoutMs | 5000 | how long to wait for the connection to be established |
reconnectDelayMs | 1000 | pause between two reconnect attempts |
reconnectAttempts | -1 | -1 = retry forever, the driver never stops on its own |
keepAliveMs | 5000 | after 5 s of silence the OS probes the connection — a dead cable is detected within seconds |
Modbus TCP sends nothing between polls, so a quiet line by itself is not
a fault; it is keepAliveMs that makes a pulled cable or a vanished slave
show up within seconds instead of after the tens of seconds of the plain
TCP timeout. Details in Reconnection.
Under the driver runs the Modbus Master (RTU/TCP) protocol
(ModbusMasterProtocol):
| Parameter | Value | Meaning |
|---|---|---|
mode | tcp | MBAP framing — matches the TCP Client driver |
unitId | 2 | slave address the master addresses |
requestTimeoutMs | 1000 | response timeout per request |
requestRetries | 2 | two retries on top of the first attempt (up to 3 transmissions) |
The project also contains two drivers unrelated to Modbus: File data
logger (FileDataLoggerDriver, enabled — on every run it records
EngTorque and Torque into the DataLogs folder, append=true,
reorderBufferMs=500) and File data replay (FileDataReplayDriver,
disabled — it points to DataLogs\values.qilog). Neither is needed for
the Modbus demonstration itself; disable the logger if you do not want
the recordings.
Events
A single periodic event Pool500ms with a period of 1000 ms. Despite
its name the master therefore polls once per second; all three variables
reference it in eventRef. Every polled variable is read by its own
request, so three FC03 requests go out per cycle.
Variables
All three variables live in the /Modbus namespace, type ushort
(1 register per value), wordOrder="big", event Pool500ms:
| Variable | Type | Holding register | Direction | Event | Presentation / unit |
|---|---|---|---|---|---|
EngTorque (Engine torque) | ushort | 0 | readWrite | Pool500ms | ModbusSImple, ×0.01, Nm |
Torque (Torque) | ushort | 1 | read | Pool500ms | ModbusSImple, ×0.01, Nm |
Drivability (Drivability) | matrix ushort, big endian | 2–12 (11 registers) | readWrite | Pool500ms | ModbusSImple on axes and data |
The Drivability matrix has an X axis Speed [RPM] (3 points), a Y axis
Temp. [K] (2 points) and 6 data cells; in memory the block is laid out
X axis → Y axis → data (22 B), which the master transfers as a
contiguous range of 11 registers starting at address 2 — one FC03 read,
and on write only the changed cells are sent. wordOrder does not apply
to a matrix; the byte order follows its Endianness (be). See
Matrix variables.
The single presentation ModbusSImple (range 0–65535, format
{0:F3}, unit Nm) uses a linear conversion named Pass-through which,
however, has multiplier=0.01 — the engineering value is raw/100.
Register value 1234 is shown as 12.340 Nm; on write the entered
engineering value is multiplied by one hundred and 1234 goes into the
register. Both matrix axes share the same presentation (including the
unit Nm).
Workspace
The single workspace contains:
- A Graph at the top —
EngTorque(red, left axis) andTorque(blue, right axis), 10 s time window, both axes auto-scaled. - Two Single-Signal controls for
EngTorqueandTorque. - A Matrix control on
Drivability— Write on Enter on, write mode off. - A Watch Table with the rows
TorqueandEngTorque, columns Name, Value, Unit, Time, Write and Read (no row is in write mode).
All controls refresh their display every 250 ms.
What you will see and what to try
- Values every cycle. Once the runtime starts, both scalars and the matrix update once per second; the graph draws two traces following whatever the slave holds in registers 0 and 1. In pyModSlave just overwrite the register values and the change arrives with the next poll.
- Writing to the readWrite registers. In the Watch Table tick
Write on the
EngTorquerow, enter an engineering value (e.g.12.34) and send it with the row's Write button (or with Enter when Write on Enter is on in the table's context menu) — 1234 is written to register 0. The table shows the written value with the next read of the register. The box is not active forTorque(direction="read"). In the Matrix control switch on Write Mode in the context menu and edit the data cells and the axis breakpoints; with Write on Enter each cell is sent right after Enter as a write of just the affected register in the range 2–12. - Pull the network cable (or stop the slave). Within seconds the
keepalive detects the dead connection, the driver goes Faulted and the
log shows
… failed (attempt N): … Reconnecting in 1000 ms.Thanks toreconnectAttempts=-1it retries forever (after three attempts it logs only once per minute). The master pauses polling meanwhile. Plug the cable back in and the connection is re-established and polling resumes on its own — no runtime restart. - If the driver does not connect at all, check
ip, unit id 2 in the slave and the firewall on the slave PC — see The device does not connect.
Related chapters
- TCP Client and Reconnection — driver parameters, keepalive and outage behaviour.
- Modbus Master (RTU/TCP) — protocol settings and per-variable Comm params.
- Matrix variable — block layout and assignment to the Modbus Master.
- Writing values to the device, Watch Table and Matrix control.
- Logging data — what the bundled File data logger does.

