How to access Unit Identifier from MBAP header in Modbus TCP
Posted: 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!
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!