How to Use RevPi as Modbus RTU Master

You can use your RevPi Flat as a Modbus RTU master. You can e.g. connect slaves such as temperature sensors, pushbuttons, motion detectors and many more via Modbus RTU and process the data from these slaves in your RevPi Flat .

You require:

  • RevPi Flat
  • Slave
  • Suitable cables with D-Sub 9 connectors (depending on the project)
  • Adapter (e.g. shop article 200018 or 200019)
  • Internet connection

Requirements:

  • You have installed a web browser on your PC (e.g. Chrome or Firefox).
  • RevPi Flat is connected to your network.

Let”s go!

    1. Connect a USB socket to the adapter.
    2. Connect the adapter to the slaves via the D-Sub 9 socket.
    3. Specify the address of the slaves and make a note of it. You may find information on this in the manufacturer”s operating instructions.
    4. Determine the device path of your RevPi Flat and make a note of it. Here you can find out how.
HINWEIS

If you use multiple slaves, make sure that each slave has its own address.

If two or more slaves have the same address, communication errors occur.

  1. Open your web browser.
  2. 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 Master.
  • 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.

  • 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 master.
  • Hold down the left mouse button and drag the Modbus RTU master onto your RevPi Flat.

The Modbus RTU Master now appears on the right side of the configuration board.

 

HINWEIS

Your Modbus RTU Master is always placed on the far right. If you are using a RevPi gate, which has to be placed on the far right/left outside as well, it will still work.

The RevPi Gate is a device physically present which shall be mounted on the very outside of the DIN rail. However, virtual devices such as the Modbus RTU master are not physically present. Therefore you don”t have to create a separate slot for these devices, and PiCtory automatically arranges them outside the devices that are physically present.

  • Click on the Modbus RTU master.
  • Define the basic settings for your adapters in the “Device Data” area. 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”. You must specify the following values:

Parameter

Description

Input

Your inputs can be configured here. A total of 32 values can be used for each master. A value has a width of 16 bits.

Output

Your outputs can be configured here. A total of 32 values can be used for each master. A value has a width of 16 bits.

device_path

File path to the Linux device file, default: /dev/ttyUSB0

You have noted this path during the preparations. If not, here you can find out how to find it.

Important: If you are using multiple devices of the same type, the paths to the Linux device files may change when you reboot. This can be prevented by setting udev rules that have e. g. assignments of Linux device file paths to device serial numbers.

baud rate

The baud rate determines the speed of the serial connection, default: 19200

parity

Configuration of the parity bit

None: no parity bit

Even: even parity (default)

Odd: odd parity

data_bits

Number of data bits, default: 8

Stop_bits

Number of stop bits, default: 1

  • Right-click on the Modbus RTU master on the configuration board.
  • A context menu opens.
  • Select the entry “Extended Data”.

An input mask opens.

  • You can set the Modbus commands here.

Parameter

Description

Slave Addr.

Slave address

You set this address directly at your slave in use. You will certainly find information on how to do this in the manufacturer”s operating instructions.

Important: If you use multiple slaves, make sure that each slave has its own address. If two or more slaves have the same address, communication errors occur.

The address “0” must not be used as slave address. With Modbus, this is reserved for broadcast.

Function Code

Modbus has function codes which allow to access data in a certain way. We use the following function codes:

READ_COILS
Read single bits

READ_DISCRETE_INPUTS
Read single input bit

READ_HOLDING_REGISTERS
Read whole input/output register (16 bit)

READ_INPUT_REGISTERS
Read whole input register (16 bit)

WRITE_SINGLE_COIL
Write single bit

WRITE_SINGLE_REGISTER
Write whole register

WRITE_MULTIPLE_COILS
Write several consecutive bits

WRITE_MULTIPLE_REGISTERS
Write several consecutive registers

Register Address

The Modbus register address or bit address of the data you want to access. In the manual of the slave you will find the information how the registers of the slave are assigned.

The register addresses are always valid in conjunction with the respective function code. This means e. g. that a “read holding register no. 17” accesses a different value than a “read input register no. 17”.

Quantity of Registers

Number of registers (or bits) that are read/written

Action Interval

The interval at which the command is sent (in milliseconds)

Note that very short intervals cannot be maintained if the system load is too high. The commands are then sent as quickly as conditions permit.

Device Value

The variable name in the RevPi process image in which the first word or bit of the Modbus command is read or written.

Make sure that the individual commands do not overlap.

Example: 4 bytes from “Output_1” also occupy “Output_2”, “Output_3” and “Output_4”. To avoid overlapping, you have to set the next command to “Output_5” in this example.

  • Click on “Add Row” to add another command.

Tip: You can delete commands by placing the check mark at the beginning of the command and clicking on “Remove Selected Rows”.

  • Click on “File>Save”. This saves your file.
  • Click on “Tools>Reset Driver”. This activates the changes for the adapter.
  • Do you want to continue using the settings in logiCAD3 or C? Then click on “File>Export”.

A window opens. Here you can specify the format of the file and enter the file name.

You can choose from two formats:

  • Export 01 creates a file that is suitable for using it in logiCAD3.
  • Export 02 creates an offset list that you can use as basic information for your own C program.
  • Select a format you want to continue working with.
  • Specify a file name.
  • Click on “OK”.

Tip: 32 values are too few for your project? No problem. Just take another master and access the same slave with it. Now you have 64 values.

Help! Error!

The Modbus master has predefined Modbus registers for status messages.

If a communication error occurs, the error code is written to the Modbus register “Modbus_Master_Status”.

The error remains in this register until the value “1” is entered manually in the Modbus register “Master_Status_Reset”.

Error Code

Function

0x10

The configured device was not found.

Check if the wiring is correct.

0x11

The configured device does not respond or you are using a register address that is not permitted for the slave (see note above!).

Check if the configuration is correct.

The Modbus master can take over up to 32 tasks. For each task there is one register each for status and status reset. If an error occurs in a task, the error code is written to the Modbus register “Modbus_Action_Status_[1 … 32]”.

The error remains in this register until the value “1” is manually entered in the Modbus register “Action_Status_Reset_[1 … 32]”.

The error codes correspond to the Modbus exception codes as specified in the Modbus specification.

The following table shows the most important error messages.

Error Code

Name

Function

0x01

ILLEGAL FUNCTION

The function code used is not allowed. Check if you are using the correct function code.

0x02

ILLEGAL DATA ADDRESS

The Modbus register address used is not valid. The register is either read-only or invalid. Check the register address.

0x03

ILLEGAL DATA VALUE

At least some of the data values used are invalid. You may have specified too high number of registers, for example. Check your values.

0x11

Resource temporarily unavailable

0x12

INVALID CRC

A faulty packet was received from the slave. This can occur e. g. after the connection has been interrupted. Check your wiring.

0x13

INVALID DATA

An incomplete packet was received from the slave. This can occur e. g. after the connection has been interrupted. Check your wiring.

0x104

Connection reset by peer

0x110

CONNECTION TIMED OUT

The slave did not respond quickly enough or at all. Check your configuration and wiring.

More about this topic can be found in the Modbus specification.