Skip to content

Modbus TCP

nXPlc speaks Modbus TCP on its Ethernet port with the standard CODESYS devices, the same as on any other PLC: no library of ours and no code to write. Everything is configured in the device tree and downloaded with the application.

The board can be a server, which other devices read and write, a client, which reads and writes other devices, or both at once.

Both roles hang under an Ethernet node:

  1. Right-click on Device → Add Device… → Fieldbuses → Ethernet Adapter → Ethernet → Add Device.
  2. Open the Ethernet node. In the General tab, with the PLC online, click … next to Interface and select eth0: IP address, subnet mask and default gateway are filled in with the board’s current values.
  3. Leave Adjust operating system settings off: the board’s address is set by the parameters of the IO node, see Ethernet.
  1. Under Ethernet: Add Device… → Modbus → Modbus TCP Server Device.
  2. In the General tab choose how many registers to expose and the port (502 by default).
  3. In I/O Mapping map your variables onto the registers.
  4. Build, download.

The two register groups go in opposite directions:

In the PLCWritten byRead by
Holding Registers%IW (inputs)the Modbus clientthe PLC
Input Registers%QW (outputs)the PLCthe Modbus client

To publish a PLC variable to a client, map it onto an Input Register. A variable mapped onto a Holding Register is overwritten every cycle by what the client sent.

Registers are WORD (0…65535): mapping an INT works, but negative values reach the client as large numbers (−1 becomes 65535).

  1. Under Ethernet: Add Device… → Modbus → Modbus TCP Client.
  2. Under the client: Modbus TCP Server, the remote device. Set its IP address and port.
  3. In the channels tab add what to read and write: Read Input Registers, Read Holding Registers, Write Multiple Registers, …
  4. In I/O Mapping map the channels onto your variables, then build and download.

Two Python 3 scripts with no dependencies, for Linux and Windows:

  • modbus_client.py — reads and writes the registers of the nXPlc server:
    Terminal window
    python3 modbus_client.py 192.168.1.50 leggi-input 0 10 --ogni 1
    python3 modbus_client.py 192.168.1.50 scrivi 0 123 456
  • modbus_server.py — a server for the nXPlc client to poll. Input Register 0 counts up, Input Register 1 holds the seconds, and every write from the PLC is printed:
    Terminal window
    python3 modbus_server.py --porta 502
IEC data memory84 KB, shared with the rest of the application. Past it, the build stops with C0106 Out of global data memory.
Network connections8 sockets in total. The CODESYS connection takes up to three, the server one to listen, and every connected client or remote server one more.

With a few hundred registers and two or three clients neither limit is anywhere close.