Close
0%
0%

DIY MODBUS TCP/RTU Gateway with WiFi

This is a simple and cheap DIY MODBUS-gateway with WiFi

Public Chat
Similar projects worth following
This gateway allows you to connect any industrial devices with the MODBUS RTU protocol

Introduction

MODBUS is an industrial protocol developed by MODICON in 1979. The protocol has become very popular due to its simplicity and reliability. Even more than 50 years after the publication of the specification, it remains one of the most popular industrial protocols in the world.

Today, there are a huge number of devices on the market that support this protocol, most of which are for industrial applications. These are sensors, actuators, frequency drives, touch and key operator panels, climate systems, charging controllers for solar battery systems, PLCs, gateways, modems, etc. They are manufactured thoroughly and with high quality, in accordance with industry standards.

Therefore, the ability to connect MODBUS devices to computers and microcontrollers opens up wide opportunities for building advanced and reliable automated systems. From data collection in a smart home system to a whole factory management system.

In this project, I developed a MODBUS gateway that allows you to connect a network segment with MODBUS devices (up to 32 devices) to the control system via a WiFi connection. The number of such gateways is unlimited.

The gateway does not require any MODBUS protocol register settings, because it transparently converts MODBUS RTU packets to MODBUS TCP and vice versa.

To be honest, I developed this project more than 5 years ago for use in my smart home system. Using a MODBUS gateway, I connected an electric meter to the Majordomo smart home system. The gateway has been working fine since then, so I decided to share this project with the community.

MODBUS protocol variations

The first versions of the protocol worked on top of asynchronous serial interfaces (RS 232, and later RS485 and RS422). There are 2 variations of the MODBUS protocol for these interfaces - MODBUS RTU and MODBUS ASCII. The difference between them is that RTU is the binary version of the protocol, and ASCII is the text version. The obvious advantage of MODBUS ASCII is the ease of debugging. But MODBUS RTU uses the bandwidth of the channel 2 times more efficiently. To date, MODBUS ASCII is not used in new devices, it has been completely replaced by MODBUS RTU, so MODBUS ASCII is not supported in the gateway.

With the rapid spread of the TCP/IP protocol stack, there is a need in the industry for a TCP version of this popular industrial protocol. There are 2 protocols for solving this problem: MODBUS RTU and MODBUS RTU over TCP.

MODBUS TCP is the official implementation, which is described in the specification. The difference between MODBUS TCP and MODBUS RTU is that the MBAP Header is added to the beginning of the packet, and the checksum field is not used (because the integrity of the packets is controlled by the TCP protocol).

MODBUS RTU over TCP is a simple transfer of all RTU frames over a TCP connection without making any changes to the frame.

As you can see from the illustration above, both protocols (MODBUS TCP and MODBUS RTU over TCP) can be transparently converted to and from MODBUS RTU. Transaction ID field is used to determine whether the response belongs to the request. Protocol ID field is always 0.

What does the PDU field consist of?

In the MODBUS protocol, there is always 1 master and 1 or more slaves. MODBUS communication always initiates the master. It sends a request to one of the slaves, after which the slave sends its response back to the master.

The MODBUS protocol supports only 2 data types - 1-bit (discrete) and 16-bit (analog). For industrial automation, this is more than enough. This limitation is at the same time a significant simplification that contributed to the widespread use of the protocol.

There are up to 4 register groups inside each Slave device. They are shown in the illustration below:

Discrete Inputs and Coils are 1-bit registers, Inputs and Holding are 16-bit registers. The MODBUS protocol functions that are used to read and write registers are listed on the right.

... Read more »

  • 1 × ESP-07 module ESP8266 based module with wifi
  • 1 × Hi-Link HLK-PM03 AC-DC
  • 1 × RS485 PHY (3.3V) Any RS485 transceiver with a 3.3V power supply can be used

View project log

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates