Objective: Configure a RevPi as a Modbus Client using CODESYS in order to reliably communicate with Modbus-TCP- and Modbus-RTU-based industrial devices simultaneously. The application reads the holding registers (0 … 9) of the connected Modbus Server and writes their values back to the holding registers (10 … 19).

This tutorial applies to the following base modules:

A preconfigured example project using RevPi Connect/+/S as the base module is available in a GitLab repository. It can be adapted to the base modules listed above.

Prerequisites #

Devices #

RevPi base module with RS485 interface

✓ Modbus TCP Server

✓ Modbus RTU Server

Note

Make sure the server are configured with unique IDs and consistent communication settings.

Hardware Installation #

✓ The RevPi base module is connected to the network.

✓ The RevPi base module is connected to the power supply.

Software Installation #

✓ CODESYS Development System (CODESYS) is installed on your PC.

✓ CODESYS runtime is installed in the CODESYS Development System.

✓ RevPi I/O driver is installed in the CODESYS Development System.

✓ CODESYS is connected to the RevPi.

✓ CODESYS runtime is installed on the RevPi.

See the documentation on Setting up the System.

1. Creating a Project in CODESYS #

▷ Open CODESYS on your PC.

▷ Open the example project ModbusTCP-RTU-Master from the GitLab repository.

▷ If necessary, adjust the RevPi base module in the device tree to match your actual device.

The device tree in CODESYS is configured as follows:

  • The Modbus TCP Client reads from and writes to a Modbus TCP Server device.

  • The Modbus RTU Client communicates with a serial Modbus RTU Server device.

  • POU_Modbus processes the application logic for reading and writing data.

Project Setting

2. Configuring the Serial Modbus RTU Interface #

For RevPi devices with an RS485 interface

▷ Open the configuration file CODESYSControl_User.cfg using:

sudo nano /etc/codesyscontrol/CODESYSControl_User.cfg

▷ Add the following line to define the naming scheme for the serial ports:

[SysCom]
Linux.Devicefile=/dev/ttyRS485-

This allows CODESYS to identify the RS485 interfaces in the Linux system and communicate via Modbus RTU.

▷ In CODESYS, open the MODBUS_COM device.

▷ On the General tab, assign the COM ports according to the number of RS485 interfaces as follows:

  • /dev/ttyRS485-0 → COM Port 1

  • /dev/ttyRS485-1 → COM Port 2

  • /dev/ttyRS485-x → COM Port x+1

Up to RevPi Bullseye (04/2024)

Older images only provide the directory /dev/ttyRS485.

▷ Create a symbolic link to access the same physical RS485 port and communicate with the Modbus Server:

ls -la /dev/ttyRS485-0

▷ If the error message No such file or directory is displayed, create the symbolic link with:

sudo ln -s /dev/ttyRS485 /dev/ttyRS485-0

▷ Reboot the system.

Using a USB-to-RS485 Converter

If an additional USB-to-RS485 converter is connected, the correct COM port must be identified.

▷ Add the following line to the file /etc/codesyscontrol/CODESYSControl_User.cfg:

[SysCom]
Linux.Devicefile=/dev/ttyRS485-

The USB device number is automatically assigned by the system when the USB-to-RS485 converter is connected.

▷ Determine the device number with:

ls /dev/ttyUSB*

❯ This displays devices such as /dev/ttyUSB0, /dev/ttyUSB1.

▷ Create a symbolic link for the USB-to-RS485 converter to the next available /dev/ttyRS485-x device file:

sudo ln -s /dev/ttyUSBx /dev/ttyRS485-y

x is the USB device number assigned by the system.

y is the next higher RS485 port number that is not used by an integrated port.

Example:

If your device has an integrated port /dev/ttyRS485-0 and your USB converter is /dev/ttyUSB0, use:

sudo ln -s /dev/ttyUSB0 /dev/ttyRS485-1

3. Configuring the Modbus RTU Server #

▷ Configure the Modbus RTU Server as follows:

  • Channel 1: Read Holding Registers

  • Channel 2: Write Multiple Registers

Project Setting

▷ Set the value 12 as the Default Value for the first ten holding registers.

Project Setting

▷ Map these channels in the POU Modbus logic for access at the application level.

Project Setting

4. Configuring the Modbus TCP Server #

▷ Specify for Modbus TCP the Slave IP address in the CODESYS configuration.

Project Setting

▷ Use the same mapping logic as for Modbus RTU for holding registers 0 … 9 and 10 … 19.

5. Simulating Modbus Server #

To simulate Modbus TCP and RTU Server, use a Modbus simulator, e.g. ModRSsim2:

▷ Install the application ModRSsim2.

▷ Configure Modbus TCP/RTU Server with holding registers:

  • 40001 … 40010 for source data

  • 40011 … 40020 for target data

Modbus RTU Server in ModRSsim2 with configured source and target register:

Project Setting

Modbus TCP Server in ModRSsim2 with configured source and target register:

Project Setting

6. Running Application #

▷ Select Generate Code (F11) in the menu bar to translate the project.

▷ Open the root device.

▷ Select Scan Network in the Communication Settings , select the RevPi and establish the connection to the device using OK.

▷ Select Login (AltF8) in the menu bar to log in to the RevPi.

▷ When you log in for the first time, create a user by following the wizard and entering your login details.

▷ In the Device User Logon window, log in to the RevPi.

▷ The first time, confirm the download of the application to the RevPi by Yes .

▷ Select ⯈ (F5) in the menu bar to start the application.

7. Verifying Application #

▷ Verify in CODESYS and in the Modbus Server that:

  • data is correctly read from holding registers 0 … 9.

  • data is written back to holding registers 10 … 19.

▷ Use the CODESYS monitoring tools to check the real-time data exchange between the client and the server.

Further Resources #