Modbus TCP RTU Slave in CODESYS

In this example, we want to run a Modbus TCP slave and a Modbus RTU slave on the RevPi Connect+ CODESYS with CODESYS Control for Raspberry Pi MC SL.

In this case, the application should copy the first 10 Holding registers to the 10 input registers of both slaves.

The following figure shows the CODESYS device tree overview of the example.

Adapting example for other devices
You can adapt this example for RevPi Compact or RevPi Core.

  • Right-click on the RevPi_Connect in the Codesys device tree.
  • Click on update device.
  • Select a RevPi Core or Compact instead of the RevPi Connect and name the device accordingly.

Configuring MODBUS RTU Serial Port

For RevPi Connect or Core:

  • Add the following line in /etc/CODESYSControl.cfg
    [SysCom]
    Linux.Devicefile=/dev/ttyUSB

Modbus RTU Slave Configuration

  • Map Holding Registers and Input Registers to the application POU named “POU_Modbus” in order to make the data available to the application.
  • For Modbus TCP Slave, the configuration is similar to the one done for Modbus RTU slave.

Simulating Modbus TCP and RTU Master

Write Multiple Registers writes to Holding Registers of the slave and Read Holding Registers gives back input register data of the Modbus TCP/RTU Slave running on RevPi.

Modbus TCP Unit ID: It has the same functionality as the slave ID. However, there is still the 255. In practice, it depends on the connected device which value has to be set. Then either a Modbus TCP Slave will be clearly identified in the network by means of its IP address. But when this slave is a gateway of ModbusTCP on Modbus RTU, it can address several Modbus RTU Slaves. The Unit ID 255 is recommended to address the gateway itself. QModMaster uses unit ID 0 instead of 255 for this purpose.