Tutorial Modbus RTU Slave (Jessie)

In the following example, we provide a Modbus-RTU master with the CPU temperature of the RevPi core. The RevPi Core 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 want to follow this example.

 

Let’s go!

  • Connect your RevPi Core to the Master via the RJ45 connector
  • Connect your RevPi Core to the power supply

Enable ModbusRTU-Slave

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

The login window appears.

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

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

  • Click the “Services” tab.
  • Click “Enabled” in the Enable/Disable Modbus Slave line.
  • Click the “Save All” button.
Services tab screenshot
  • Click the “Apps” tab.
  • Click the start button behind the entry “PiCtory”.
Apps tab screenshot

PiCtory starts.

 

Modbus RTU Slave configuration

Open the folder “Virtual Devices” in the device catalog.

Select Modbus RTU-Slave.

Hold down the left mouse button and drag the Modbus-RTU slave to your RevPi Core.

Your Modbus-RTU slave will now appear on the far right 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 want to process the data later in another program, this input can be very helpful.
In the “Value Editor” we define the settings for the RTU connection. In our example, this is:

  • 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“ to save the file.

Click on “Tools>Reset Driver”. This activates the changes for the adapter.

Query CPU temperature

You can query the CPU temperature of your RevPi Cores with 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 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 dez (=01ec hex) to offset 11.

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

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

[…]

Modbus-RTU Master configuration

In this example, we query the values with the tool “qModbusMaster”. Of course, you can also use other 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 which has unit ID 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 form:
    Format: Decimal
  • The data should be read from Modbus register address 1 onwards:
    Start Address: 1
  • An entire register is to be read:
    Number of Registers: 1

 

Click “Menu”

Click “Options” and select “ModbusRTU”

Set the settings for the serial interface here. For our example, this is what it looks like::

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

Click “Commands>Connect” to establish a connection to the Modbus.

Click “Commands”>Read/Write” to retrieve values via Modbus.

You will now see the value 519. This tells us that the CPU temperature of our RevPi Core is 51.9°C

Modbus functions

The Modbus slave module provides you with 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 register