Temperature Measurements with RevPi as Modbus TCP Slave

This tutorial is valid from the Image Jessie. Unfortunately, it doesn’t apply to Wheezy.

In the following example, we provide a Modbus TCP master with the CPU temperature of the RevPi. The RevPi 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 base module to the Master via the RJ45 connector.
  • Connect your RevPi base module to the power supply.

Enable Modbus-TCP Slave function

  • Start RevPi Status.
  • Click tab SERVICES.
  • Enable Modbus Slave.
  • Click button Save All.

Modbus TCP Slave Configuration

  • Start PiCtory.
  • Open the folder Virtual Devices in the device catalog.
  • Select Modbus TCP-Slave.
  • Hold down the left mouse button and drag the Modbus TCP slave to your RevPi.

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 TCP connection. In our example, this is:

TCP_port: 502

The default value of the TCP port is “502”according to Modbus specification. You can change this value. That makes sense, if you want to implement 2 different controllers in the same network, which should not interfere with each other..

max._modbus_TCP_connections: 10

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 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-TCP 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 TCP connection:
    Modbus Mode: TCP
  • We want to communicate with the slave which has unit ID 1:
    Unit ID: 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”

Clicke “Options” and choose “ModbusTCP”

Set the IP address of your RevPi in the “Slave IP” field.

Set the TCP port. For ModbusTCP connections, this is usually “502”.

Click on “Commands”
Click on “Connect”. This will connect you to the Modbus.


Click “Commands”

Click “Read/Write”. This will retrieve the values via the Modbus.

You will now see the value 508 (CPU temperature of the RevPi is 50.8°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