About the use of mDNS service in modbus TCP slave
Posted: 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:
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...
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:
- void ethernet_init(void)
- {
- esp_err_t ret;
- ethConnectFlag = 0;
- ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK( ret );
- // Initialize TCP/IP network interface (should be called only once in application)
- ESP_ERROR_CHECK(esp_netif_init());
- // Create default event loop that running in background
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- //Start mdns service and register device
- start_mdns_service();
- // Create instance(s) of esp-netif for SPI Ethernet(s)
- esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
- esp_netif_config_t cfg_spi = {
- .base = &esp_netif_config,
- .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
- };
- esp_netif_t *eth_netif_spi = { NULL };
- char if_key_str[10];
- char if_desc_str[10];
- char num_str[3];
- itoa(0, num_str, 10);
- strcat(strcpy(if_key_str, "ETH_SPI_"), num_str);
- strcat(strcpy(if_desc_str, "eth"), num_str);
- esp_netif_config.if_key = if_key_str;
- esp_netif_config.if_desc = if_desc_str;
- esp_netif_config.route_prio = 64;
- eth_netif_spi = esp_netif_new(&cfg_spi);
- s_example_esp_netif = eth_netif_spi; //给modbustcp部分用
- // Init MAC and PHY configs to default
- eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
- eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
- // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
- gpio_install_isr_service(0);
- // Init SPI bus /***初始化在外部flash中***/
- // spi_bus_config_t buscfg = {
- // .mosi_io_num = 7,
- // .miso_io_num = 2,
- // .sclk_io_num = 6,
- // .quadwp_io_num = -1,
- // .quadhd_io_num = -1,
- // };
- // ESP_ERROR_CHECK(spi_bus_initialize(1, &buscfg, 3));
- // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
- spi_eth_module_config_t spi_eth_module_config = {
- .spi_cs_gpio = 18,
- .int_gpio = 19,
- .phy_reset_gpio = -1,
- .phy_addr = 1,
- };
- //INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
- //获取esp mac地址
- uint8_t base_mac_addr[ETH_ADDR_LEN];
- esp_efuse_mac_get_default(base_mac_addr);
- uint8_t local_mac_1[ETH_ADDR_LEN];
- esp_derive_local_mac(local_mac_1, base_mac_addr);
- spi_eth_module_config.mac_addr = local_mac_1;
- // Configure SPI interface and Ethernet driver for specific SPI module
- esp_eth_mac_t *mac_spi;
- esp_eth_phy_t *phy_spi;
- esp_eth_handle_t eth_handle_spi = { NULL };
- spi_device_interface_config_t spi_devcfg = {
- .mode = 0,
- .clock_speed_hz = 40 * 1000 * 1000,
- .queue_size = 20
- };
- // Set SPI module Chip Select GPIO
- spi_devcfg.spics_io_num = spi_eth_module_config.spi_cs_gpio;
- // Set remaining GPIO numbers and configuration used by the SPI module
- phy_config_spi.phy_addr = spi_eth_module_config.phy_addr;
- phy_config_spi.reset_gpio_num = spi_eth_module_config.phy_reset_gpio;
- eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(1, &spi_devcfg);
- w5500_config.int_gpio_num = spi_eth_module_config.int_gpio;
- mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
- phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);
- esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
- ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi));
- /* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
- 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
- */
- if (spi_eth_module_config.mac_addr != NULL) {
- ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi, ETH_CMD_S_MAC_ADDR, spi_eth_module_config.mac_addr));
- }
- // attach Ethernet driver to TCP/IP stack
- ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));
- // Register user defined event handers
- ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
- /* start Ethernet driver state machine */
- ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
- }
- // An example application of Modbus slave. It is based on freemodbus stack.
- // See deviceparams.h file for more information about assigned Modbus parameters.
- // These parameters can be accessed from main application and also can be changed
- // by external Modbus master host.
- void modbus_tcp_init(void)
- {
- //ESP_ERROR_CHECK(init_services());
- //start_mdns_service();
- // Set UART log level
- esp_log_level_set(TAG, ESP_LOG_INFO);
- mb_communication_info_t comm_info = { 0 };
- #if !CONFIG_EXAMPLE_CONNECT_IPV6
- comm_info.ip_addr_type = MB_IPV4;
- #else
- comm_info.ip_addr_type = MB_IPV6;
- #endif
- comm_info.ip_mode = MB_MODE_TCP;
- comm_info.ip_port = MB_TCP_PORT_NUMBER;
- ESP_ERROR_CHECK(slave_init(&comm_info));
- // The Modbus slave logic is located in this function (user handling of Modbus)
- slave_operation_func(NULL);
- //ESP_ERROR_CHECK(slave_destroy());
- //ESP_ERROR_CHECK(destroy_services());
- }
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...