STM32
直播中

李艺银

7年用户 938经验值
私信 关注
[问答]

STM32F4使用lwip无法发送udp包怎么解决?

debug日志如下:

  • udp_send
  • pbuf_header: old 20006da8 new 20006da0 (8)
  • udp_send: added header in given pbuf 20003058
  • udp_send: sending datagram of length 19
  • udp_send: UDP packet length 19
  • udp_send: UDP checksum 0x0000
  • udp_send: ip_output_if (,,,,0x11,)
  • pbuf_header: old 20006da0 new 20006d8c (20)
  • ip4_output_if: st0
  • IP header:
  • +-------------------------------+
  • | 4 | 5 |  0x00 |        39     | (v, hl, tos, len)
  • +-------------------------------+
  • |        1      |000|       0   | (id, flags, offset)
  • +-------------------------------+
  • |  255  |   17  |    0x0000     | (ttl, proto, chksum)
  • +-------------------------------+
  • |  192  |  168  |  137  |    1  | (src)
  • +-------------------------------+
  • |  192  |  168  |  137  |    2  | (dest)
  • +-------------------------------+
  • ip4_output_if: call netif->output()
  • pbuf_header: old 20006d8c new 20006d7e (14)
  • ethernet_output: sending packet 20003058

把ethernet_output执行完后,返回程序,一直返回到发送函数,然后直接hardfault。所以无法定位到错误的程序在哪里。

我的project配置是:
1. STM32F407ZG
2. lwip是用stm32cubemx自动生成的
3. 这个项目可以接收tcp,可以发送tcp,可以收到udp包,就是不能发送。
4. 核心函数如下:

  •   while (1)
  •   {
  •                 MX_LWIP_Process();
  •                 if(isStart==1)udp_send(udp_PCB,udp_p);
  •   /* USER CODE END WHILE */

  •   /* USER CODE BEGIN 3 */
  •   }

这里是先建立tcp连接,另外一个pc发送start命令,板子收到start,令isStart=1,然后就发送udp包。
5. udp client初始化如下:

  • void UDP_client_init()
  • {
  •         udp_p = pbuf_alloc(PBUF_RAW,sizeof(UDPData),PBUF_RAM);
  •         udp_p -> payload = (void*)UDPData;
  •         Set_IP4_ADDR( ipaddr2,UDP_REMOTE_IP);
  •         udp_pcb = udp_new();
  •         udp_bind(udp_pcb,IP_ADDR_ANY,UDP_Client_PORT);
  •         udp_connect(udp_pcb, ipaddr2,UDP_REMOTE_PORT);
  • }

NOTE:
为什么加TCP连接?
1. 因为直接发udp也是不行的。
2. 我的这个是程序查询接收以太网包的,没有用中断。所以怀疑是arp没有解析到的原因。结果并不是。

回帖(1)

乔婧

2024-4-26 15:54:14
是pbuf为的type为PBUF_RAM,改成PBUF_ROM。或者,将udp_p -> payload = (void*)UDPData;改成memcpy或者pbuf_take。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分