How to access Unit Identifier from MBAP header in Modbus TCP

MrNando
Posts: 8
Joined: Thu Jan 23, 2025 10:37 am

How to access Unit Identifier from MBAP header in Modbus TCP

Postby MrNando » Wed Apr 09, 2025 10:58 am

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!

MrNando
Posts: 8
Joined: Thu Jan 23, 2025 10:37 am

Re: How to access Unit Identifier from MBAP header in Modbus TCP

Postby MrNando » Thu Apr 10, 2025 6:46 am

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.

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

Postby MicroController » Thu Apr 10, 2025 6:59 pm

It's definitely possible.
Proof: https://github.com/zivillian/esp32-modbus-gateway, https://github.com/harihanv/esp32-modbus-gateway
I would need to implement my own Modbus stack over TCP to make it work the way I want.
If you don't want to use Arduino, this may be necessary.
Last edited by MicroController on Fri Apr 11, 2025 8:27 am, edited 1 time in total.

MrNando
Posts: 8
Joined: Thu Jan 23, 2025 10:37 am

Re: How to access Unit Identifier from MBAP header in Modbus TCP

Postby MrNando » Fri Apr 11, 2025 7:12 am

Thank you very much, I'm going to take a look at them to see how it's implemented.

MrNando
Posts: 8
Joined: Thu Jan 23, 2025 10:37 am

Re: How to access Unit Identifier from MBAP header in Modbus TCP

Postby MrNando » Fri Apr 11, 2025 7:18 am

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: Bing [Bot], Google [Bot], Qwantbot, Semrush [Bot], YisouSpider and 4 guests