Modbus TCP RTU Master in CODESYS

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

In this case, the application should read the holdig registers of the Modbus slaves 0-9 and copy them to the holding registers 10-19.

The following figure shows the CODESYS device tree overview of the example. Both Modbus masters are running on the RevPi Connect and each has a slave connected. The application logic is implemented in the POU_Modbus, which is responsible for reading the holding registers and writing back to the holding registers for both Modbus TCP and Modbus RTU slaves.

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

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.

For RevPi Compact:

If using the default RS485 port on the device, the serial port needs to be assigned in Codesys.

  • Add the following line in /etc/CODESYSControl.cfg
    [SysCom]
    Linux.Devicefile =/dev/ttyAMA
  • If using a USB-485 Converter, add the following line in /etc/CODESYSControl.cfg
    [SysCom]
    Linux.Devicefile =/dev/ttyUSB

Modbus RTU Slave Address

Configure the Modbus RTU Slave channels, one for Read Holding Registers and one for Writing Multiple Registers.

The Modbus RTU Slave default value is set as 12 for the first 10 holding registers.

Map the two previously configured channels for Read Holding Registers and Write Multiple Registers to the application POU named “POU_Modbus” in order to make the data available to the application.

Modbus TCP Slave

Configure the appropriate Modbus TCP Slave address.

For Modbus TCP Master, the configuration is similar to the one done for Modbus RTU.

Simulating Modbus TCP and RTU Slave

For the simulation of the Modbus slaves we use the application ModRSsim2.

You can download the application here:
https://sourceforge.net/projects/modrssim2/

Modbus TCP/RTU Slave Holding Registers 40001-40010 are copied by Modbus TCP/RTU Master running on RevPi and written back to Slave Holding Registers 40010-40020 .