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.
The Ethernet node
Section titled “The Ethernet node”Both roles hang under an Ethernet node:
- Right-click on Device → Add Device… → Fieldbuses → Ethernet Adapter → Ethernet → Add Device.
- 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. - Leave Adjust operating system settings off: the board’s address is set by the parameters of the IO node, see Ethernet.
Server
Section titled “Server”- Under Ethernet: Add Device… → Modbus → Modbus TCP Server Device.
- In the General tab choose how many registers to expose and the port (
502by default). - In I/O Mapping map your variables onto the registers.
- Build, download.
The two register groups go in opposite directions:
| In the PLC | Written by | Read by | |
|---|---|---|---|
| Holding Registers | %IW (inputs) | the Modbus client | the PLC |
| Input Registers | %QW (outputs) | the PLC | the 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).
Client
Section titled “Client”- Under Ethernet: Add Device… → Modbus → Modbus TCP Client.
- Under the client: Modbus TCP Server, the remote device. Set its IP address and port.
- In the channels tab add what to read and write: Read Input Registers, Read Holding Registers, Write Multiple Registers, …
- In I/O Mapping map the channels onto your variables, then build and download.
Trying it from a PC
Section titled “Trying it from a PC”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 1python3 modbus_client.py 192.168.1.50 scrivi 0 123 456modbus_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
Limits
Section titled “Limits”| IEC data memory | 84 KB, shared with the rest of the application. Past it, the build stops with C0106 Out of global data memory. |
| Network connections | 8 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.