ESP-32 Modbus RTU master and slave istances simultaneously

mattiavdm
Posts: 3
Joined: Thu Aug 31, 2023 2:23 pm

ESP-32 Modbus RTU master and slave istances simultaneously

Postby mattiavdm » Thu Aug 31, 2023 2:29 pm

Hello,
I'm working in a project where there is the need for a device to communicate over Modbus as master and slave RTU simultaneously.
I have not access to ESP32 hardware yet, and i tried to find implementations or posts about this use case without any luck.
Supposing i have two serial ports, one for the Modbus master communication and one for the slave, is the Espressif Modbus library capable of running two istances of Modbus RTU (one as master, one as slave)?
Thanks

rsimpsonbusa
Posts: 126
Joined: Tue May 17, 2016 8:12 pm

Re: ESP-32 Modbus RTU master and slave istances simultaneously

Postby rsimpsonbusa » Thu Aug 31, 2023 3:58 pm

Hi.

I actually did not try what I’m about to explain but I have done a lot of programming in espidf/Esp32xxx and from the espidf examples (google it) in protocols/modbus/serial you have a Master and Slave example.

You will see that the Master and Slave have a configuration section in where the real world requirement is a UART port for the TX/RX and RTS pins.

I presume you want to “connect” both “devices” so:

I would create a task for the Master with UART0 and 3 pins for TX/RX/RTS(say 18,19,21) as per the example. Then create another Task for the Slave with UART1 and 3 pins (different than the Master say 13,14,15). The RTS signal should be Low or High I don’t know, so get a 1K resistor and Ground it or VCC (3.3Volts).

Now hardware wise you must cross the signals in the ESP32 pins from Master to Slave (TX->RX, RX->TX, 18->14, 19->13) and in theory it should work. The actual convertion to RS485 differential signals is irrelevant to the software and you :D .

I don’t think you can use the same UART for both (surely an error will be returned) but you can try it and just configure the Slave TX pin to the Master RX pin and Slave TX to Master RX.

You can printf stuff coming and going in both Tasks so you can see whats going on. The Master could do other things like send Mqtt messages etc basically becoming a RS485 bridge.

An ESP32 costs less than $5 so can surely experiment at a low cost.

Good luck.

mattiavdm
Posts: 3
Joined: Thu Aug 31, 2023 2:23 pm

Re: ESP-32 Modbus RTU master and slave istances simultaneously

Postby mattiavdm » Fri Sep 01, 2023 7:37 am

Thanks for the detailed reply!
I will try to do it and post an update.

ESP_alisitsyn
Posts: 211
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: ESP-32 Modbus RTU master and slave istances simultaneously

Postby ESP_alisitsyn » Thu Oct 26, 2023 7:20 am

Hi,

The old example based on old version of stack is here: https://github.com/alisitsyn/modbus_sup ... 3012_esp32.
It can be modified a little bit to support latest version of esp-modbus. It is possible to support one master and ove slave of each type or wise versa (MASTER RTU - SLAVE RTU) in one application.

Who is online

Users browsing this forum: olli100 and 64 guests