在使用 Netcat 调试我的程序后,我找出了问题的原因,现在我可以发送和接收 UDP 数据包了。
我做的第一件事是激活调试消息。关于这一点,我不得不手动定义 LWIP_DEBUG,因为 STM32CubeMX 不想通过自动生成来定义它。
我将我的代码修改为:
- ...
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include
- #include
- #include
- #include
- #include
- #include "lwip/pbuf.h"
- #include "lwip/udp.h"
- #include "lwip/tcp.h"
- #include "lwip/opt.h"
- #include "lwip/init.h"
- #include "lwip/netif.h"
- #include "lwip/timeouts.h"
- #include "lwip/ip4_addr.h"
- #include "netif/etharp.h"
- #include "ethernetif.h"
- /* USER CODE END Includes */
- ...
- /* Private variables ---------------------------------------------------------*/
- UART_HandleTypeDef huart1;
- /* USER CODE BEGIN PV */
- extern struct netif gnetif;
- extern ip4_addr_t ipaddr;
- extern ip4_addr_t netmask;
- extern ip4_addr_t gw;
- extern uint8_t IP_ADDRESS[4];
- extern uint8_t NETMASK_ADDRESS[4];
- extern uint8_t GATEWAY_ADDRESS[4];
- /* USER CODE END PV */
- ...
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_LWIP_Init();
- /* USER CODE BEGIN 2 */
- char str[] = "Hello UDP World!";
- err_t err = 0;
- ip_addr_t dstaddr;
- ip_addr_t srcaddr;
- struct eth_addr dstmac;
- struct eth_addr srcmac;
- IP4_ADDR(&dstaddr, 192, 168, 0, 2);
- IP4_ADDR(&srcaddr, 192, 168, 0, 10);
- struct udp_pcb * pcb;
- struct pbuf * pb;
- pcb = udp_new();
- dstmac.addr[0] = 0x98;
- dstmac.addr[1] = 0xDE;
- dstmac.addr[2] = 0xD0;
- dstmac.addr[3] = 0x01;
- dstmac.addr[4] = 0x2C;
- dstmac.addr[5] = 0x8A;
- srcmac.addr[0] = 0x00;
- srcmac.addr[1] = 0x80;
- srcmac.addr[2] = 0xE1;
- srcmac.addr[3] = 0x00;
- srcmac.addr[4] = 0x00;
- srcmac.addr[5] = 0x00;
- etharp_add_static_entry(&dstaddr, &dstmac);
- etharp_add_static_entry(&srcaddr, &srcmac);
- err = udp_bind(pcb, &srcaddr, 55151);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- err = 0;
- pb = pbuf_alloc(PBUF_TRANSPORT, sizeof(str), PBUF_RAM);
- memcpy(pb->payload, str, sizeof(str));
- err = udp_bind(pcb, &srcaddr, 55151);
- err = udp_connect(pcb, &dstaddr, 55151);
- err = udp_sendto(pcb, pb, &dstaddr, 55151);
- MX_LWIP_Process();
- udp_disconnect(pcb);
- udp_remove(pcb);
- pbuf_free(pb);
- HAL_Delay(1000);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- ...
如您所见,我将 MAC 地址附加到源和远程 IPv4 地址结构。我还通过使用 RAM 然后使用 memcpy 函数将字符串复制到缓冲区有效负载来修改缓冲区的分配。
使用 Netcat,我用这个命令更改了我的接收命令,因为它比以前的命令更明确:“$nc -u -s 192.168.0.2 -p 55151 192.168.0.10 55151”用于接收命令和“$echo” -e 'Hello ' | nc -u4 -w 1 -s 192.168.0.2 -p 55151 192.168.0.10 55151" 用于发送命令。
最后,主要错误是在调用 MX_LWIP_Process() 函数之前先断开连接然后删除 udp_pcb。因此,当程序试图发送 UDP 数据包时(或在其他试图接收 UDP 数据包的程序上),udp_pcb 不可用。
|