针对这个问题,我也研究看了一些实例,语句可以联用,再给你个实例参考看看吧
if(flag) //your own flag to trigger event
{
pudp_buf = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if(pudp_buf)
{
IP4_ADDR(&dest_ip, alarm_ip[0],alarm_ip[1],
alarm_ip[2],alarm_ip[3]);
memcpy(pudp_buf->payload,temp_out_buf,len);
err=udp_sendto(udp_comm_pcb,pudp_buf,&dest_ip,TCP_COMM_PORT);//Port to send
pbuf_free(pudp_buf);
if(err)
printf("udp_sendto err!\n");
}
}
另外有比较长的代码也可以参考下别人的用法基于LWIP的UDP实例
本工程硬件基于STM32F429+LAN8720A外设,使用RMII通信接口。工程由STM32CUBEMX直接生成。代码主要使用的是ST官方例程。
1. 服务器端
//定义端口号
#define UDP_SERVER_PORT 7 /* define the UDP local connection port */
#define UDP_CLIENT_PORT 7 /* define the UDP remote connection port */
//声明接收数据回调函数,在初始化函数中