About the use of mDNS service in modbus TCP slave

asdaaa
Posts: 2
Joined: Fri Jul 07, 2023 9:04 am

About the use of mDNS service in modbus TCP slave

Postby asdaaa » Fri Nov 24, 2023 6:30 am

Hello,

I used the chip esp32_c3. When I used the example mb_tcp_slave, I enabled the option of "Resolve slave addresses using mDNS service" in menuconfig. Then I used Modbus Poll to resolve the domain name and successfully connected to esp32_c3.

But when I ported this example to my own project and turned on mdns, it didn't work,and I used Ethernet to communicate with the example. In my project, I didn't use the "example_connect" function to connect, I used the following method, but I compared my connection to "example_connect" in detail, and the process looks the same:
  1. void ethernet_init(void)
  2. {
  3.     esp_err_t ret;
  4.     ethConnectFlag = 0;
  5.     ret = nvs_flash_init();
  6.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  7.         ESP_ERROR_CHECK(nvs_flash_erase());
  8.         ret = nvs_flash_init();
  9.     }
  10.     ESP_ERROR_CHECK( ret );
  11.     // Initialize TCP/IP network interface (should be called only once in application)
  12.     ESP_ERROR_CHECK(esp_netif_init());
  13.     // Create default event loop that running in background
  14.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  15.     //Start mdns service and register device
  16.     start_mdns_service();
  17.     // Create instance(s) of esp-netif for SPI Ethernet(s)
  18.     esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  19.     esp_netif_config_t cfg_spi = {
  20.         .base = &esp_netif_config,
  21.         .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  22.     };
  23.     esp_netif_t *eth_netif_spi = { NULL };
  24.     char if_key_str[10];
  25.     char if_desc_str[10];
  26.     char num_str[3];
  27.         itoa(0, num_str, 10);
  28.         strcat(strcpy(if_key_str, "ETH_SPI_"), num_str);
  29.         strcat(strcpy(if_desc_str, "eth"), num_str);
  30.         esp_netif_config.if_key = if_key_str;
  31.         esp_netif_config.if_desc = if_desc_str;
  32.         esp_netif_config.route_prio = 64;
  33.         eth_netif_spi = esp_netif_new(&cfg_spi);
  34.  
  35.     s_example_esp_netif = eth_netif_spi; //给modbustcp部分用
  36.  
  37.     // Init MAC and PHY configs to default
  38.     eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
  39.     eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
  40.  
  41.     // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
  42.     gpio_install_isr_service(0);
  43.  
  44.     // Init SPI bus    /***初始化在外部flash中***/
  45.     // spi_bus_config_t buscfg = {
  46.     //     .mosi_io_num = 7,
  47.     //     .miso_io_num = 2,
  48.     //     .sclk_io_num = 6,
  49.     //     .quadwp_io_num = -1,
  50.     //     .quadhd_io_num = -1,
  51.     // };
  52.     // ESP_ERROR_CHECK(spi_bus_initialize(1, &buscfg, 3));
  53.  
  54.     // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  55.     spi_eth_module_config_t spi_eth_module_config = {
  56.         .spi_cs_gpio = 18,
  57.         .int_gpio = 19,
  58.         .phy_reset_gpio = -1,
  59.         .phy_addr = 1,
  60.     };
  61.     //INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
  62.     //获取esp mac地址
  63.     uint8_t base_mac_addr[ETH_ADDR_LEN];
  64.     esp_efuse_mac_get_default(base_mac_addr);
  65.     uint8_t local_mac_1[ETH_ADDR_LEN];
  66.     esp_derive_local_mac(local_mac_1, base_mac_addr);
  67.     spi_eth_module_config.mac_addr = local_mac_1;
  68.  
  69.     // Configure SPI interface and Ethernet driver for specific SPI module
  70.     esp_eth_mac_t *mac_spi;
  71.     esp_eth_phy_t *phy_spi;
  72.     esp_eth_handle_t eth_handle_spi = { NULL };
  73.     spi_device_interface_config_t spi_devcfg = {
  74.         .mode = 0,
  75.         .clock_speed_hz = 40 * 1000 * 1000,
  76.         .queue_size = 20
  77.     };
  78.         // Set SPI module Chip Select GPIO
  79.         spi_devcfg.spics_io_num = spi_eth_module_config.spi_cs_gpio;
  80.         // Set remaining GPIO numbers and configuration used by the SPI module
  81.         phy_config_spi.phy_addr = spi_eth_module_config.phy_addr;
  82.         phy_config_spi.reset_gpio_num = spi_eth_module_config.phy_reset_gpio;
  83.  
  84.         eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(1, &spi_devcfg);
  85.         w5500_config.int_gpio_num = spi_eth_module_config.int_gpio;
  86.         mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
  87.         phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);
  88.  
  89.         esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
  90.         ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle_spi));
  91.  
  92.         /* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
  93.        02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
  94.         */
  95.         if (spi_eth_module_config.mac_addr != NULL) {
  96.             ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi, ETH_CMD_S_MAC_ADDR, spi_eth_module_config.mac_addr));
  97.         }
  98.  
  99.         // attach Ethernet driver to TCP/IP stack
  100.         ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));
  101.  
  102.     // Register user defined event handers
  103.     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  104.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  105.  
  106.     /* start Ethernet driver state machine */
  107.     ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
  108. }
  109.  
  110. // An example application of Modbus slave. It is based on freemodbus stack.
  111. // See deviceparams.h file for more information about assigned Modbus parameters.
  112. // These parameters can be accessed from main application and also can be changed
  113. // by external Modbus master host.
  114. void modbus_tcp_init(void)
  115. {
  116.     //ESP_ERROR_CHECK(init_services());
  117.     //start_mdns_service();
  118.  
  119.     // Set UART log level
  120.     esp_log_level_set(TAG, ESP_LOG_INFO);
  121.  
  122.     mb_communication_info_t comm_info = { 0 };
  123.  
  124. #if !CONFIG_EXAMPLE_CONNECT_IPV6
  125.     comm_info.ip_addr_type = MB_IPV4;
  126. #else
  127.     comm_info.ip_addr_type = MB_IPV6;
  128. #endif
  129.     comm_info.ip_mode = MB_MODE_TCP;
  130.  
  131.     comm_info.ip_port = MB_TCP_PORT_NUMBER;
  132.     ESP_ERROR_CHECK(slave_init(&comm_info));
  133.  
  134.     // The Modbus slave logic is located in this function (user handling of Modbus)
  135.     slave_operation_func(NULL);
  136.  
  137.     //ESP_ERROR_CHECK(slave_destroy());
  138.     //ESP_ERROR_CHECK(destroy_services());
  139. }

The following is the initialization log of esp32_c3, which shows that hostname is successfully set:

2023-11-24 14:15:21 I (366) FLASHFP: Initializing external SPI Flash Pin assignments:
2023-11-24 14:15:21 I (376) FLASHFP: MOSI: 7 MISO: 2 SCLK: 6 CS: 10
2023-11-24 14:15:21 I (376) spi_flash: detected chip: gd
2023-11-24 14:15:21 I (376) spi_flash: flash io: dio
2023-11-24 14:15:21 I (386) FLASHFP: Initialized external Flash, size=8192 KB, ID=0xc84017
2023-11-24 14:15:21 I (386) FLASHFP: Adding external Flash as a partition, label="storage", size=8192 KB
2023-11-24 14:15:21 I (396) FLASHFP: Listing data partitions:
2023-11-24 14:15:21 I (396) FLASHFP: - partition 'nvs', subtype 2, offset 0x9000, size 16 kB
2023-11-24 14:15:21 I (406) FLASHFP: - partition 'otadata', subtype 0, offset 0xd000, size 8 kB
2023-11-24 14:15:21 I (416) FLASHFP: - partition 'phy_init', subtype 1, offset 0xf000, size 4 kB
2023-11-24 14:15:21 I (416) FLASHFP: - partition 'user_save', subtype 2, offset 0x310000, size 4 kB
2023-11-24 14:15:21 I (426) FLASHFP: - partition 'invt_save', subtype 2, offset 0x311000, size 512 kB
2023-11-24 14:15:21 I (436) FLASHFP: - partition 'storage', subtype 129, offset 0x0, size 8192 kB
2023-11-24 14:15:21 I (436) FLASHFP: spiflash_info has writed
2023-11-24 14:15:21 I (446) FLASHFP: spiflash_info.init_flag= aa55 total_count= 1553 write_pos= 2838344 read_pos= 2844680 un_read_count= 0 is_flash_full= 0
2023-11-24 14:15:21 I (466) system_api: Base MAC address is not set
2023-11-24 14:15:21 I (466) system_api: read default base MAC address from EFUSE
2023-11-24 14:15:21 I (466) SLAVE_TEST: mdns hostname set to: [mb_slave_tcp_01]
2023-11-24 14:15:21 I (486) w5500.mac: version=4
2023-11-24 14:15:21 I (496) esp_eth.netif.netif_glue: 6a:67:25:4e:fa:2c
2023-11-24 14:15:21 I (496) esp_eth.netif.netif_glue: ethernet attached to netif
2023-11-24 14:15:21 I (506) eth_example: Ethernet Started
2023-11-24 14:15:21 I (506) BTDM_INIT: BT controller compile version [76c24c9]
2023-11-24 14:15:21 I (506) BTDM_INIT: Bluetooth will use main XTAL as Bluetooth sleep clock.
2023-11-24 14:15:21 I (506) phy_init: phy_version 912,d001756,Jun 2 2022,16:28:07
2023-11-24 14:15:21 I (546) BTDM_INIT: Bluetooth MAC: 68:67:25:4e:fa:2d
2023-11-24 14:15:21
2023-11-24 14:15:21 I (546) NimBLE_BLE_PRPH: BLE Host Task Started
2023-11-24 14:15:21 I (556) NimBLE: GAP procedure initiated: stop advertising.
2023-11-24 14:15:21
2023-11-24 14:15:21 I (556) NimBLE: Device Address:
2023-11-24 14:15:21 I (556) NimBLE: 68:67:25:4e:fa:2d
2023-11-24 14:15:21 I (556) NimBLE:
2023-11-24 14:15:21
2023-11-24 14:15:21 I (556) NimBLE: GAP procedure initiated: advertise;
2023-11-24 14:15:21 I (566) NimBLE: disc_mode=2
2023-11-24 14:15:21 I (566) NimBLE: adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=512 adv_itvl_max=800
2023-11-24 14:15:21 I (576) NimBLE:
2023-11-24 14:15:21
2023-11-24 14:15:21 I (2506) eth_example: Ethernet Link Up
2023-11-24 14:15:23 I (2506) eth_example: Ethernet HW Addr 6a:67:25:4e:fa:2c
2023-11-24 14:15:23 I (4956) esp_netif_handlers: eth0 ip: 192.168.137.136, mask: 255.255.255.0, gw: 192.168.137.1
2023-11-24 14:15:26 I (4956) eth_example: Ethernet Got IP Address
2023-11-24 14:15:26 I (4956) eth_example: ~~~~~~~~~~~
2023-11-24 14:15:26 I (4956) eth_example: ETHIP:192.168.137.136
2023-11-24 14:15:26 I (4956) eth_example: ETHMASK:255.255.255.0
2023-11-24 14:15:26 I (4966) eth_example: ETHGW:192.168.137.1
2023-11-24 14:15:26 I (4966) eth_example: ~~~~~~~~~~~
2023-11-24 14:15:26 I (4976) MB_TCP_SLAVE_PORT: Socket (#54), listener on port: 502, errno=0
2023-11-24 14:15:26 I (4976) MB_TCP_SLAVE_PORT: Protocol stack initialized.
2023-11-24 14:15:26 I (5036) SLAVE_TEST: Modbus slave stack initialized.
2023-11-24 14:15:26 I (5036) SLAVE_TEST: Start modbus test...

Who is online

Users browsing this forum: No registered users and 110 guests