微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > lwip UDP实验遇到的问题

lwip UDP实验遇到的问题

时间:10-02 整理:3721RD 点击:
我用的是STM32F107VCT6的芯片
u8 *replay = "I got a message!\n";
void APP_udp_server_init(void)
{
//创建控制块
if(pAppUdpSvrPcb == NULL)
pAppUdpSvrPcb = udp_new();

  /* Bind the upcb to the UDP_PORT port */
   /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
   udp_bind(pAppUdpSvrPcb, IP_ADDR_ANY, udplocalport);
   
   /* Set a receive callback for the upcb */
   udp_recv(pAppUdpSvrPcb, APP_udp_callback, replay);     //注册回调函数APP_udp_callback,每当接收到网络调试助手发来的信息就会执行回调函数
}
//回调函数,功能是将I got a message!发回网络调试助手
void APP_udp_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
    struct ip_addr destAddr = *addr;
    if(p != NULL)
   {
       /* Tell the client that we have accepted it */
        memset(p->payload,0,p->len);
        memcpy(p->payload,arg,strlen(arg));
        udp_sendto(upcb,p,&destAddr,port);
       /* Free the p buffer */
       pbuf_free(p);
       udp_disconnect(upcb);
   }
}
理想的结果应该是显示I got a message!    然而结果却为I (I和一个空格)。也就是说每次发送都只能显示两个字符
后来我在memset和memcpy中间也加了udp_sendto(upcb,p,&destAddr,port);,结果变成了,先输出一个空格,再输出I got a message!(如图1所示)
请各位大佬给我解释一下,出现这两种结果的原因,拜托了

图1


已经解决了,哈哈

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top