如何给以太网接口设备设置固定IP

daya123
Posts: 12
Joined: Fri Jun 25, 2021 7:50 am

如何给以太网接口设备设置固定IP

Postby daya123 » Thu Sep 16, 2021 6:26 am

编译环境:idf4.3
设置方法:使用例程中protocol_examples_common共用联网组件
设置方法如下:
在上述组件connect.c文件的

Code: Select all

static esp_netif_t *eth_start(void)
函数中添加以太网连接回调函数:

Code: Select all

    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_ipv4_event, netif));
回调函数如下:

Code: Select all

static void on_eth_ipv4_event(void *esp_netif, esp_event_base_t event_base,
						 int32_t event_id, void *event_data)
{
	static int set_up= 0 ;
	switch (event_id) {
	case ETHERNET_EVENT_CONNECTED:
		if(set_up == 0)
		{
				set_up= 1;
				tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
				tcpip_adapter_ip_info_t eth;
				eth.ip.addr = ipaddr_addr("10.10.10.172");
				eth.netmask.addr = ipaddr_addr("255.255.255.0");
				eth.gw.addr=ipaddr_addr("10.10.10.1");
				tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &eth);
		}

		break;
	default:
		break;
	}
}
ip可以设置成功,但是注册的两个事件:ETHERNET_EVENT_CONNECTED和IP_EVENT_ETH_GOT_IP 却在频繁调用回调事件,这正常吗?应该如何正确设置固定IP
Attachments
start.png
start.png (41.98 KiB) Viewed 2213 times
err.png
err.png (10.7 KiB) Viewed 2213 times

Who is online

Users browsing this forum: Baidu [Spider] and 109 guests