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.1 when 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):

ParameterValueMeaning
ip192.168.2.208slave address — change it to match your network
port502the standard Modbus TCP port
connectionTimeoutMs5000how long to wait for the connection to be established
reconnectDelayMs1000pause between two reconnect attempts
reconnectAttempts-1-1 = retry forever, the driver never stops on its own
keepAliveMs5000after 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):

ParameterValueMeaning
modetcpMBAP framing — matches the TCP Client driver
unitId2slave address the master addresses
requestTimeoutMs1000response timeout per request
requestRetries2two 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:

VariableTypeHolding registerDirectionEventPresentation / unit
EngTorque (Engine torque)ushort0readWritePool500msModbusSImple, ×0.01, Nm
Torque (Torque)ushort1readPool500msModbusSImple, ×0.01, Nm
Drivability (Drivability)matrix ushort, big endian2–12 (11 registers)readWritePool500msModbusSImple 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) and Torque (blue, right axis), 10 s time window, both axes auto-scaled.
  • Two Single-Signal controls for EngTorque and Torque.
  • A Matrix control on Drivability — Write on Enter on, write mode off.
  • A Watch Table with the rows Torque and EngTorque, 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 EngTorque row, 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 for Torque (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 to reconnectAttempts=-1 it 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.