How to Implement Data Communication Using Modbus RTU? (part 1)

Mar 19

Not so long ago, I was engaged in the MC programming (in the framework of the work on the nanosatellite), in particular, I implemented a communication between devices by different protocols. So, where do we start?

trollfaceWhen it is necessary to solve this kind of problem, first of all, you should choose (if there are “hardware” restrictions, it means that the choice is partly made for us):

  • data communication type (synchronous, asynchronous);
  • interface (RS-232, RS-422, RS-485, USB, Bluetooth, etc.);
  • protocol (WAKE, Modbus, etc.).

To make the right choice, you need to take into account the scope, speed and signal transmission distance limits and other characteristics.

One of my tasks was to implement a data communication between a computer and the STM32F105 MC on the existing board.


On the interface board, there was the RS-485 for asynchronous serial data communication, which allows implementing connection of up to 32 devices at the same time, has a speed of 30 Mbit/s and a transmission distance of 1 200 m (see Table 1).



Data communication type


Maximum number of receivers


Maximum cable length

1200 m

Maximum transmission rate

30 MBit/sec

Output voltage

-7…+12 V

Receiver sensitivity

±200 mV

Table 1. RS-485 interface parameters

In principle, it is possible to implement a data communication without a protocol, but in this way, nobody guarantee data integrity and timely delivery, so a common practice is to use specialized protocols.

Data communication protocol is a set of agreements of a logic level interface that define a data communication between different programs. These agreements define a uniform method for transmitting messages and error handling in the interaction of software of spaced apart devices, connected by one or another interface. A standardized data communication protocol also enables to develop interfaces (now at the physical level), which are not tied to a particular hardware platform and manufacturer (e.g., USB, Bluetooth).

Communication protocols generally operate on the “master-slave” principle. A master initiates a communication and sends a request to slave devices (slaves), and slave devices respond (or do not respond) to requests according to the address in a package. There are many protocols, but one of the most common is Modbus. The Modbus protocol was established many years ago but is still very popular.

Modbus is an open communication protocol based on the “client-server” architecture. It is widely used in industry for communication between electronic devices. It can be used to communicate data via the RS-485, RS-422, RS-232 communication lines and TCP/IP network (Modbus TCP).


“Master-slave” operational scheme

Package structure:

Address of a slave device Function code Data Error detection unit


There are three versions of the Modbus protocol:

  • ASCII (transmission of ASCII-characters, separated by “:”, a sign of the end of the package is a newline);
  • RTU (transmission of frames, a sign of the end of the package is a silence in a channel);
  • TCP/IP (used over TCP/IP).

My choice fell on the Modbus RTU, as the Modbus TCP/IP did not technically suite (TCP/IP was not used), and in contrast to the Modbus ASCII, it was easier to communicate data not in the form of lines, but in the form of frames, in order not to waste resources for parsing. In the following article, I want to show examples of the implementation of the Modbus RTU and use of special libraries to simplify development and life 🙂