How to Use RevPi as Modbus RTU Slave

In the following example we provide the CPU temperature of the RevPi Flat to a Modbus TCP master. The RevPi Flat takes on the role of a slave. You have a similar project? Great, then just follow our example! Of course, you have to adjust some data.

Info: In this example, we use the software “qModMaster” on a Windows PC to simulate a Modbus master. You can download this software at “https://sourceforge.net/projects/qmodmaster” if you just want to follow this example.

Let’s go!

  • Connect your RevPi Flat to the master via the RJ45 connector.
  • Connect your RevPi Flat to the power supply.

Enabling Modbus RTU Slave Function

  • Open your web browser.
  • Enter the IP address of your RevPi Flat in the address bar of your browser.

The login window opens.

  • Log in with the username “admin”.
  • Enter your password. You can find it on the sticker on the side of your RevPi Flat.
  • Click on “Login”.

You can now see the current device status of your RevPi Flat.

  • Click on the “Services” tab.
  • Click on “Enabled” in the line Enable/Disable Modbus Slave.
  • Click on the “Save All” button.

Tip: You can also activate the master function in the command line. Enter the following command: „sudo revpi-config enable pimodbus-master“

  • Click on the “Apps” tab.
  • Click on the start button behind the entry “PiCtory”.

PiCtory opens.

Configuring Modbus RTU Slave

  • Select your RevPi Flatin the “Device Catalog”.
  • Hold down the left mouse button and drag the RevPi Flat onto the configuration board.
  • Open the folder “Virtual Devices” in the “Device Catalog”.
  • Select the Modbus RTU Slave.
  • Hold down the left mouse button and drag the Modbus RTU Slave onto your RevPi Flat.
  • The Modbus RTU Slave now appears on the far right side of the configuration board.

Set the basic settings for your adapters in the “Device Data” window. This entry is optional. If you use a lot of devices and wish to process the data in another program later, then this entry can be very helpful. Set the basic settings for your RTU connection in the “Value Editor”. In our example, these settings are:

  • device_path: /dev/ttyUSB0
  • baud_rate: 19200
  • parity: none
  • data_bits: 8
  • stop_bits: 1
  • modbus_address: 1

Saving PiCtory Configuration

  • Click on “File>Save”. This saves your file.
  • Click on “Tools>Reset Driver”. This activates the changes for the adapter.

 

Querying CPU Temperature

You can query the CPU temperature of the RevPi Flat using the following command in the command line:

/usr/bin/vcgencmd measure_temp

You get the current output of the temperature:

/usr/bin/vcgencmd measure_temp

temp=48.7″C

In order to be able to process the values on the Modbus, we have to convert this output. The temperature must be output as x10 °C:

/usr/bin/vcgencmd measure_temp | awk ” { print substr($0,6,2) substr($0,9,1) } “

492

Now we write the data with piTest into a Modbus register:

piTest -w Output_1, $(/usr/bin/vcgencmd measure_temp | awk ” { print substr($0,6,2) substr($0,9,1) } “)

To ensure that the temperature is not only written to the register once, but continuously, the whole process must run in a loop:

while true; do piTest -w Output_1,$(/usr/bin/vcgencmd measure_temp | awk ” { print substr($0,6,2) substr($0,9,1) } “); sleep 1; done &

Write value 492 dec (=01ec hex) to offset 11.

Write value 498 dec (=01f2 hex) to offset 11.

Write value 492 dec (=01ec hex) to offset 11.

[…]

Configuring Modbus TCP Master

In this example we query the values using the tool “qModbusMaster”. Of course, you can use another suitable software.

For our example, we make the following settings:

  • We want to communicate via a Modbus RTU connection:
    Modbus Mode: RTU
  • We want to communicate with the slave with address 1:
    Slave Addr: 1
  • The data should be retrieved in a cycle of 1000 ms:
    Scan Rate (ms): 1000
  • The data should be read as a whole Modbus register:
    Function Code: Read Input Registers (0x04)
  • The data should be output in decimal format:
    format: Decimal
  • The data should be read from Modbus register address 1:
    Start Address: 1
  • An entire register should be read:
    Number of Registers: 1
  • Click on “Menu”.
  • Click on “Options” and select “ModbusRTU”.
  • Define the settings for the serial interface here. For our example, it looks like this:

Info: If you are using qModbus Master and use a different port, you may need to enter it like this: „.COM10“

  • Click on “Commands” and click on “Connect”. This establishs a connection to the Modbus.
  • Click on “Commands”.
  • Click on Read/Write. With this you retrieve the values via the Modbus.

You now see the value 519. This indicates that the CPU temperature of our RevPi Flat is 51.9°C right now.

Modbus Functions

The Modbus slave module provides the following Modbus functions:

  • Read Holding Registers (0x03)
    Read inputs
  • Read Input Registers (0x04)
    Read outputs
  • Write Single Register (0x06)
    Write into an input register
  • Write Multiple Registers (0x10)
    Write into multiple input registers