Page 1 of 3

ESP32 RS485driver hardwareserial ModbusMaster

Posted: Wed Jan 10, 2018 12:31 pm
Hi, I'm trying to establish RS485 Modbus communication between ESP32 - RS485 Arduino driver board - Modbus device by using harwareserial(2) and the MODBUSMASTER library.

I know the MAXRS485 is TTL and ESP is CMOS, but I tried the code on an Arduino MEGA, and powered the RS485 driver board via 3.3V and it works.

When connected via ESP, I see the pulse train going out of the TX (hardwareserial2 pins 4,5) and on the differential AB lines of the driver, but the Modbus device doesn't seem to respond. I don't see it on scope (differential AB lines).

So I'm assuming a problem with the message sent to the Modbus device. Any idea's? Maybe a problem of compatibility of the modbusmaster library on ESP32?

Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Thu Jan 11, 2018 9:53 am
From time to time I get a response from the slave device. Still don't know what the cause could be, the pulse train looks very clean so I don't expect a hardware/noise issue.

There is also no softwareserial library for the ESP32?

Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Sat Jan 13, 2018 6:22 pm
by tka4ev_s
hello i want to compiling code modbus for esp 32 but i have error
:\Users\serg\Documents\Arduino\libraries\ModbusMaster-master\src\ModbusMaster.cpp: In member function 'uint8_t ModbusMaster::requestFrom(uint16_t, uint16_t)':

C:\Users\serg\Documents\Arduino\libraries\ModbusMaster-master\src\ModbusMaster.cpp:94:33: error: 'read' is used uninitialized in this function [-Werror=uninitialized]

_u8ResponseBufferLength = read;
cc1plus.exe: some warnings being treated as errors

i can test modbus in analyzer

Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Tue Jan 30, 2018 8:23 pm
by bertvaneyken
I'm using the MAX485 with ESP8266 and Atmega328 without any issue for a long time so I thought I could keep the circuitry and just exchange the mcu.
(I use the SimpleModbusMaster library in Arduino)

The first packet with the ESP32 is processed correctly but the Siglent reveals that all packets after are the first reply nonsense.
I assume Serial.flush() causes the issue.

I've found some usefull threads but I have not distilled a working solution with them:

Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Thu Feb 01, 2018 1:07 pm
by antonyc
Hey, I also have this exact problem and I'm really interested to know the solution. :-)

What do we know so far?


Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Thu Mar 08, 2018 7:40 pm
by casmeiron
Hi Guys,

I'm having the exact same issue!
@TLSESP32 did you find a solution for your problem?

We're using MAX485 with ESP32 and ModBusMaster library The transmitter its working but we never get any response back. The same code works with Arduino UNO.

I would love if anybody could give me any hints on that :|

Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Thu Mar 15, 2018 12:37 am
by antonyc
Hi everyone,

I need this to work, so I decided to try to diagnose and fix it myself.

The problem is that the ESP32 sends gibberish to the Modbus device. The device then doesn't respond because it doesn't understand what the hell the ESP32 is saying!

If you look at the pulse train, you can see that the ESP32 does try to send the pulses correctly, but the timing is out.

It looks like the ESP32 drops the stop bits too early. What it should so is hold the stop bit high for 1.5 times the length of a bit in order to denote a gap in the message. For some reason, the DE/RE timing is out too, they are also released early.


Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Sat Mar 17, 2018 3:20 pm
by antonyc
Hi everyone,

An update for those who are interested. You can get Modbus to work with the ESP32.

The libraries for Arduino do not work. For some reason, they cause the transmission to the Modbus device to be messed up. The timing is not right and the RE/DE control doesn't work properly. This is because there is *something* in the libraries which doesn't suit the ESP32.

To make ESP32 work with Modbus, you have to manually transmit a Modbus frame to the device with raw bytes, according to the Modbus specification. Also, you need to manually control the DE/RE pin directly too.

Having written a sketch to transmit the data in a raw byte form, I can confirm that the ESP32 can be made to work with Modbus.

Basically, transmit a frame by transmitting individual bytes via serial with a 13us delay in between each byte.

The contents of your transmission depend on which command you're trying to transmit.

For more info, look here:


Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Sat Mar 17, 2018 10:00 pm
by casmeiron
Hi Antony,
Could you share your sketch?
Didn't you try to run the same example using ESP-IDF without Arduino libraries like Wire?


Re: ESP32 RS485driver hardwareserial ModbusMaster

Posted: Sat Mar 24, 2018 6:58 pm
by gregger2k
antonyc wrote:
I've seen this sort of problem before with the ESP32. A few weeks ago, I fixed Modbus, TIA485. It was because the timing of the ESP32/libraries was just messed up. :-(

It looks like I'm going to have to look into the wire library. :-(
Are your fixed Modbus files or examples available anywhere?
I have been using Modbus with the ESP8266 but I am ready to move to the ESP32 and would like the upgrade to go as smooth as possible. Any Information on your fixes would be greatly appreciated.

Thanks, Greg