STM32CubeMx + LWIP(实现UDP组播/MQTT/热插拔)系列 三 ----- IP地址的获取,UDP的连接

IP地址获取

IP地址的获取需要调用DHCP进行自动分配。

struct netif *Network_Interface;	
//cube生成的lwip.c文件中有定义全局变量 struct netif gnetif;之后的网络相关基本都是使用该全局变量,但是需要自己外部声明

//主函数循环例程
int timestamp = 0;
unsigned char Ip[20] = {0};
unsigned char Mask[20] = {0};
unsigned char gw[20] = {0}
Network_Interface = &gnetif;
dhcp_start(gnetif);
timestamp = HAL_GetTick();
while(1)
{
    MX_LWIP_Process(); 
    if(HAL_GetTick() - timestamp >= 5000){
        sprintf(Ip,"%d.%d.%d.%d",gnetif->ip_addr.addr>>24,
        gnetif->ip_addr.addr>>16,
        gnetif->ip_addr.addr>>8,
        gnetif->ip_addr.addr>>0);

        sprintf(Mask,"%d.%d.%d.%d",gnetif->netmask.addr>>24,
        gnetif->netmask.addr>>16,
        gnetif->netmask.addr>>8,
        gnetif->netmask.addr>>0);

        sprintf(gw,"%d.%d.%d.%d",gnetif->gw.addr>>24,
        gnetif->gw.addr>>16,
        gnetif->gw.addr>>8,
        gnetif->gw.addr>>0);

        printf("id address: %s\r\n",Ip);
        printf("mask code : %s\r\n",Mask);
        printf("gateway   : %s\r\n",gw);
    }
}

本系列文章进行实现。不进行具体的优化。

UDP连接


|1.                                                                                                      
stm32f1xx_hal_eth.c                                                                                      
static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)                                      
macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_NONE; //ETH_MULTICASTFRAMESFILTER_PERFECT;     

|2.                                                                                                       
ethernetif.c                                                                                             
err_t ethernetif_init(struct netif *netif)                                                               
netif->flags |= NETIF_FLAG_IGMP;  
                                                                       
|3.                                                                                                     
Cubemx                                                                                                   
LWIP_LOOPIF_MULTICAST (Multicast/IGMP on Loop Interface) Enabled                                         
MEMP_MEM_MALLOC (User Memory Pool Functions) Enabled                                                     
LWIP_IGMP(IGMP Module) Enabled                                                                           

我在使用UDP的时候,记录的笔记为上。需要修改以上部分。

连接部分

ip4_addr_t tIp4_Multicast_Address;
struct udp_pcb *ptUdp_Pcb;
int buf[64] = {0};

IP4_ADDR(&tIp4_Multicast_Address, 224,1,1,1); 
igmp_joingroup(IP_ADDR_ANY,&tIp4_Multicast_Address);	
ptUdp_Pcb = udp_new();
if(ptUdp_Pcb == NULL){
    printf("Created Udp Fail ");				
}	

err = udp_bind(ptUdp_Pcb,IP_ADDR_ANY,65000);
if(err != ERR_OK){
    printf( "Bind Udp Fail ");					
    udp_remove(ptUdp_Pcb);
    ptUdp_Pcb = NULL;
}
	
udp_recv(ptUdp_Pcb, Multicast_Receive, buf);				
printf( "Udp Multicast connecting ..... ");

接收部分

static void Multicast_Receive(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
    struct pbuf *q;
    u16 u16Current_len;
	
    if(p==NULL)
	return;

    //找出总数据长度
    u16Current_len = 0;
    for(q=p;q!=NULL;q=q->next){		
        u16Current_len += q->len;
    }
	
    for(q=p;q!=NULL;q=q->next){		
	printf("%s",(char*)q->payload);
    }

    pbuf_free(p);
}

发送部分

void Multicast_Send(void)
{  	
	struct pbuf *p = NULL; 	
	char buf[] = "Moliam!";
        ip_addr_t addr;
	p = pbuf_alloc(PBUF_TRANSPORT, register_node->Send_Buf_Len, PBUF_RAM);
				
	for(int i=0;i< sizeof(buf);i++)
	    *((u8 *)p->payload + i) = buf[i] ;
			
	p->len = sizeof(buf);
	udp_sendto(ptUdp_Pcb, p,&addr,65000);
			
	pbuf_free(p);

}

 

 

你可能感兴趣的:(STM32CUBE,lwip,c语言,嵌入式)