Hi, I'm working on a small project involving Modbus and I need some help retrieving a specific parameter.
My idea is to use the ESP32 as a gateway. In my setup, there's a Modbus master that communicates using Modbus TCP, and a slave device that uses Modbus RTU. The ESP32 should act as a bridge between them, converting the TCP frame into an RTU frame and vice versa. It would receive the TCP request, parse it to build the RTU frame, send it to the slave, and then do the reverse process with the response.
Using the Modbus API, I’m able to build the RTU frame with the mb_param_request_t structure and send it using mbc_master_send_request. However, I need the slave_addr, which is located in the Unit Identifier field of the MBAP header from the original TCP frame sent by the master.
Is there any way to access this Unit Identifier value? I understand that since this is TCP, it is managed by the TCP stack and Modbus might not have direct access to that header. But is there a way to retrieve it through the TCP API or any workaround?
I hope there's a solution for this.
Thank you very much!
How to access Unit Identifier from MBAP header in Modbus TCP
Re: How to access Unit Identifier from MBAP header in Modbus TCP
I've been reviewing the Modbus documentation and API, and I have some doubts about whether I can get my project to work as intended.
Using the Modbus API directly, is it possible to make the ESP32 act as a gateway? Specifically, I want to implement a setup with three participants: a Modbus TCP master, the ESP32 acting as a gateway, and a Modbus RTU slave sensor. The idea is for the master to send a request to the ESP32, which would then process and adapt the request, forward it to the RTU slave, receive the slave’s response, and finally return that response to the master.
From what I understand about how the Modbus API works, I’m not sure this is possible. It seems that as soon as the ESP32 receives a request from the master, it immediately responds with its own data, without giving itself enough time to forward the request to the RTU slave and return the slave’s actual response.
Am I misunderstanding how the API works? I’d really like to know if this approach is feasible, or if I would need to implement my own Modbus stack over TCP to make it work the way I want.
Using the Modbus API directly, is it possible to make the ESP32 act as a gateway? Specifically, I want to implement a setup with three participants: a Modbus TCP master, the ESP32 acting as a gateway, and a Modbus RTU slave sensor. The idea is for the master to send a request to the ESP32, which would then process and adapt the request, forward it to the RTU slave, receive the slave’s response, and finally return that response to the master.
From what I understand about how the Modbus API works, I’m not sure this is possible. It seems that as soon as the ESP32 receives a request from the master, it immediately responds with its own data, without giving itself enough time to forward the request to the RTU slave and return the slave’s actual response.
Am I misunderstanding how the API works? I’d really like to know if this approach is feasible, or if I would need to implement my own Modbus stack over TCP to make it work the way I want.
-
MicroController
- Posts: 2663
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: How to access Unit Identifier from MBAP header in Modbus TCP
It's definitely possible.
Proof: https://github.com/zivillian/esp32-modbus-gateway, https://github.com/harihanv/esp32-modbus-gateway
Proof: https://github.com/zivillian/esp32-modbus-gateway, https://github.com/harihanv/esp32-modbus-gateway
If you don't want to use Arduino, this may be necessary.I would need to implement my own Modbus stack over TCP to make it work the way I want.
Last edited by MicroController on Fri Apr 11, 2025 8:27 am, edited 1 time in total.
Re: How to access Unit Identifier from MBAP header in Modbus TCP
Thank you very much, I'm going to take a look at them to see how it's implemented.
Re: How to access Unit Identifier from MBAP header in Modbus TCP
In the repo, it doesn't use the Espressif API. My question is whether it's possible to use the Modbus API provided by Espressif directly, or if I need to develop my own application using TCP/IP.
Who is online
Users browsing this forum: No registered users and 5 guests