CODESYS Modbus TCP RTU Slave

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.

If you have a license for Single Core, you can change the target like this:

  • Right-click on the “Root Device” CODESYS Control for Raspberry Pi MC SL in the CODESYS device tree.
  • Click on update device.
  • Select CODESYS Control for Raspberry Pi SL.

The CODESYS Control device target for the example will then be changed from multi core to single core.

Configuring MODBUS RTU Serial Port

For RevPi Connect or Core:

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

For RevPi Compact:

  • If you use the standard RS485 interface of the device, the serial interface must be assigned in CODESYS.
    Add the following line to /etc/CODESYSControl.cfg:
    [SysCom]
    Linux.device-file=/dev/ttyAMA
    If you use a USB-485 converter, add the following line to /etc/CODESYSControl.cfg:
    [SysCom]
    Linux.device-file=/dev/ttyUSB

Under General/Serial Port Configuration tab of the MODBUS_COM device in the CODESYS tree the COM Port will be selected as follows:
/dev/ttyUSB0 -> COM Port 1
/dev/ttyUSB1 -> COM Port 2 …

If an extra USB-485 converter is attached, the correct COM port must be identified.

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.