ESP32 MODBUS SLAVE BLOCKING

erhansari
Posts: 1
Joined: Thu Apr 22, 2021 7:06 am

ESP32 MODBUS SLAVE BLOCKING

Postby erhansari » Thu Apr 22, 2021 7:28 am

hello esp32 modbus slave communication is working as blocked, which stops the program completely if no data comes from the master. i want help me fix this problem.
thanks everyone.



"// The cycle below will be terminated when parameter holdingRegParams.dataChan0
// incremented each access cycle reaches the CHAN_DATA_MAX_VAL value.
for (; holding_reg_params.holding_data0 <MB_CHAN_DATA_MAX_VAL;) (
// Check for read / write events of Modbus master for certain events
mb_event_group_t event = mbc_slave_check_event (MB_READ_WRITE_MASK);
const char * rw_str = (event & MB_READ_MASK)? "READ": "WRITE";
// Filter events and process them accordingly
"

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

Re: ESP32 MODBUS SLAVE BLOCKING

Postby ESP_alisitsyn » Thu Apr 29, 2021 12:58 pm

Hi @erhansari,

Hope I understand you correctly. You want to do something else when your main task with modbus cycle is waiting for master data. Is this correct? In the modbus slave example the whole cycle of getting data is located in main task. So, if the holding_reg_params.holding_data0 reaches the maximum limit we exit the cycle and task is deleted. This is the functionality of original example to show how to init Modbus, get updated data over the Modbus then complete the communication. For your case you could put the code in the main() function into separated function like `slave_operation_func()`, create the task for modbus communication and execute this as a task function:

Code: Select all

static void slave_operation_func(void *arg)
{
	// The modbus code, place here

}

TaskHandle_t hask_handle;

void app_main(void)
{
    // Starts slave operation task
    xTaskCreate(slave_operation_func, "slave_task", 8192, NULL, 20, &task_handle);
    
    // Do here whatever you need
    // ....   application cycle
    
}
In this case the modbus code will start in separated task. When the modbus task reaches the maximum parameter value it will exit from cycle the destroy slave and delete the task but the main task will continue the application cycle.

The code is attached. Let me know if you have any issues.
Attachments
mb_slave.zip
modified modbus slave project
(7.43 KiB) Downloaded 264 times

Who is online

Users browsing this forum: Majestic-12 [Bot] and 108 guests