Page 1 of 1

Serial Modbus Master - restart stack

Posted: Thu Oct 24, 2019 11:11 am
by sabbay
In my application modbus controller seems to start malfunctioning after some time (reads return error 0xffff), so I`m trying to restart the modbus stack, but the code below doesnt seem to work. Also I came across use after free bug while doing this https://github.com/espressif/esp-idf/issues/4238

How on earth should I restart this? Or is it unnecessary?

Code: Select all

E (2968) MB_PORT_COMMON: xMBMasterPortTimersInit(121): timer set value failure, timer_isr_register() returned (0x105).
E (2968) MB_CONTROLLER_MASTER: mbc_serial_master_start(109): mb stack initialization failure, eMBInit() returns (0x3).
E (2978) MB_CONTROLLER_MASTER: mbc_master_start(211): SERIAL master start failure error=(0x103).
E (2988) MODBUS_CONTROLLER: modbus_init(114): mb controller start fail, returns(0x103).
E (2998) MODBUS_CONTROLLER: modbus_controller_loop(30): mb controller failed to restart
E (3008) FreeRTOS: FreeRTOS Task "modbus_gateway" should not return, Aborting now!

Code: Select all

esp_err_t modbus_restart(void){
  SENSE_MB_CHECK((mbc_master_destroy() == ESP_OK), ESP_ERR_INVALID_STATE, "Failed to destroy mb handler structure.");
  return modbus_init();
}

esp_err_t modbus_init(void){
    mb_communication_info_t comm = {
            .port = MB_PORTNUM,
            .mode = MB_MODE_RTU,
            .baudrate = MB_BAUDRATE,
            .parity = MB_PARITY
    };
    void* master_handler = NULL;

    esp_err_t err = mbc_master_init(MB_PORT_SERIAL_MASTER, &master_handler);
    SENSE_MB_CHECK((master_handler != NULL), ESP_ERR_INVALID_STATE,
                                "mb controller initialization fail.");
    SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
                            "mb controller initialization fail, returns(0x%x).",
                            (uint32_t)err);
    err = mbc_master_setup((void*)&comm);
    SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
                            "mb controller setup fail, returns(0x%x).",
                            (uint32_t)err);
    err = mbc_master_start();
    SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
                            "mb controller start fail, returns(0x%x).",
                            (uint32_t)err);
    // Set UART pin numbers
    err = uart_set_pin(MB_PORTNUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
                                    CONFIG_MB_UART_RTS, UART_PIN_NO_CHANGE);
    SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
            "mb serial set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)err); 
    // Set driver mode to Half Duplex
    err = uart_set_mode(MB_PORTNUM, UART_MODE_RS485_HALF_DUPLEX);
    SENSE_MB_CHECK((err == ESP_OK), ESP_ERR_INVALID_STATE,
            "mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)err);
    return err;
}


Environment

Code: Select all

esp-idf: v4.1-dev-437-gd2ad0f077
xtensa-esp32-elf-gcc (crosstool-NG esp32-2019r1) 8.2.0
Linux
esp-wroom-32 devkitv1

Re: Serial Modbus Master - restart stack

Posted: Fri Oct 25, 2019 3:34 am
by mronchi
I'm facing exactly the same issue, after a while where it works ok, start to received 0xFFFF error, and if I try to re-initialize Modbus after use mbc_master_destroy(), I'm get the Heap error and reboot.

EDIT:
After check alisitsyn answers on https://github.com/espressif/esp-idf/issues/4238 seems to be fixed.

Best regards.

Re: Serial Modbus Master - restart stack

Posted: Mon Oct 19, 2020 6:51 am
by juanchu98
Hello everyone,
I'm facing this same issue in a recent release of ESP-IDF (v4.3-dev-907-g6c17e3a64).
Could you please tell me how did you fix the issue?
I have checked all the issues on github but seems i cannot reinit the Modbus Stack. I still get the errors similar to @mronchi:

Code: Select all

E (2968) MB_PORT_COMMON: xMBMasterPortTimersInit(121): timer set value failure, timer_isr_register() returned (0x105).
E (2968) MB_CONTROLLER_MASTER: mbc_serial_master_start(109): mb stack initialization failure, eMBInit() returns (0x3).
Any update on this?

Thanks!!

Re: Serial Modbus Master - restart stack

Posted: Tue Oct 20, 2020 4:09 pm
by ESP_alisitsyn
Hello @juanchu98,

0x105 = ESP_ERR_NOT_FOUND.

The Modbus stack uses the timer group to measure timeouts. THis looks like the default timer group is already used in your application that causes this error. Please try to configure the timer group in freemodbus kconfig menu (CONFIG_FMB_TIMER_GROUP, CONFIG_FMB_TIMER_INDEX).