MODBUS mbc_master_start(); cannot start it

EmrahDuatepe
Posts: 1
Joined: Fri Jun 17, 2022 11:47 am

MODBUS mbc_master_start(); cannot start it

Postby EmrahDuatepe » Fri Jun 17, 2022 12:07 pm

Hello everyone,
I am new at ESP32 and I am trying to setup modbus. When the codes come on error = master_interface_ptr->start(); inside the mbc_master_start(); I cant see the printf("master_init() !\n"); section. All codes below.

1. I created an new project.
2. I got the example from the esp-idf about modbus and applied it. That is it?
I dont know should I perform any extra steps.
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "sdkconfig.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_system.h"
  8. #include "esp_spi_flash.h"
  9. #include "mbcontroller.h"
  10. #include "modbus_params.h"
  11. #include "esp_log.h"
  12.  
  13. void app_main(void)
  14. {
  15.     printf("master_init() !\n");
  16.     ESP_ERROR_CHECK(master_init());
  17. }
  18.  
  19.  
  20.  
  21. static esp_err_t master_init(void)
  22. {
  23.     mb_communication_info_t comm =
  24.     {
  25.         .port = MB_PORT_NUM,        // Serial port number
  26.         .mode = MB_MODE_RTU,        // Modbus mode of communication (MB_MODE_RTU or MB_MODE_ASCII)
  27.         .baudrate = 9600,           // Modbus communication baud rate
  28.         .parity = MB_PARITY_NONE    // parity option for serial port
  29.     };
  30.  
  31.     void* master_handler = NULL;
  32.  
  33.     esp_err_t err = mbc_master_init(MB_PORT_SERIAL_MASTER, &master_handler);
  34.  
  35.     MB_RETURN_ON_FALSE((master_handler != NULL), ESP_ERR_INVALID_STATE, TAG,
  36.                                 "mb controller initialization fail.");
  37.  
  38.     MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
  39.                             "mb controller initialization fail, returns(0x%x).",
  40.                             (uint32_t)err);
  41.  
  42.     err = mbc_master_setup((void*)&comm);
  43.  
  44.     MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
  45.                             "mb controller setup fail, returns(0x%x).",
  46.                             (uint32_t)err);
  47.  
  48.     // Set UART pin numbers
  49.     err = uart_set_pin(MB_PORT_NUM, 1, 3, 22, UART_PIN_NO_CHANGE);
  50.  
  51.     err = mbc_master_start();
  52. //    MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
  53. //                            "mb controller start fail, returns(0x%x).",
  54. //                            (uint32_t)err);
  55. //    MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
  56. //                "mb serial set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)err);
  57. //
  58. //    // Set driver mode to Half Duplex
  59. //       err = uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX);
  60. //       MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
  61. //               "mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)err);
  62. //
  63. //       vTaskDelay(5);
  64. //           err = mbc_master_set_descriptor(&device_parameters, 1);
  65. //           MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
  66. //                                       "mb controller set descriptor fail, returns(0x%x).",
  67. //                                       (uint32_t)err);
  68. //           ESP_LOGI(TAG, "Modbus master stack initialized...");
  69.     return err;
  70. }
  71.  

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

Re: MODBUS mbc_master_start(); cannot start it

Postby ESP_alisitsyn » Tue Jun 28, 2022 8:20 am

Hello EmrahDuatepe,

In your code, you are missing the required parts of Modbus initialization and start. This causes the main task to be deleted after Modbus initialization.

1. Port initialization https://docs.espressif.com/projects/esp ... ialization
2. Configure data access https://docs.espressif.com/projects/esp ... descriptor
3.Start communication https://docs.espressif.com/projects/esp ... munication. Prepare the function or task to send and receive Modbus data: https://github.com/alisitsyn/modbus_sup ... ter.c#L101
4.Modbus drive destroy https://docs.espressif.com/projects/esp ... munication

Also, it seems that you are using the pins GPIO1, and GPIO3 that are used by the console by default this is why you don't see your "master_init() !\n" message in the console. If you still going to use these pins you need to disable the default console or use custom pins for this.

Connect your serial adapter to GPIO as below or other UART supported pins:
#define MB_UART_RXD_PIN 22
#define MB_UART_TXD_PIN 23
#define MB_UART_RTS_PIN 18

I suggest you to setup it as in the simplified Modbus example:
https://github.com/alisitsyn/modbus_sup ... us_master/

Please try the suggestions above and refer to Modbus documentation:
https://docs.espressif.com/projects/esp ... index.html

Let me know if you have some specific questions related to it.

Who is online

Users browsing this forum: No registered users and 123 guests