49 "Ten Thousand Miles" Raspberry Pi Car——PicoW Learning (C Language UDP Communication)
[Copy link]
UDP communication has the advantage of high speed. There are routines for reference for TCP communication, but there are none for UDP communication.
Target
Create a new "echo" routine project for the PicoW module based on the UDP protocol and STA mode. You can communicate with PicoW through a mobile phone and print out relevant information through the serial port.
New Construction
Refer to the project creation method in the previous post , use the "pico project generato" tool, name the project "Test_IwIP_UDP", check "Console over UART" and "Background IwIP".
Coding
The previous work has completed the construction of the project framework. The main code is posted below.
Add header file
#include"lwip/udp.h"
Add definitions and global variables
Mainly the WIFI username and password, where XX needs to be replaced with your own, communication port number and PCB.
#defineWIFI_SSID "XXXXXXXXXXX"
#defineWIFI_PASSWORD "XXXXXXXXX"
#defineRCV_FROM_PORT 1234
structudp_pcb*rcv_udp_pcb;
Main function writing
The main tasks are to initialize the chip, connect to the WiFi router, bind UDP, register the receive callback function, and flash the LED. UDP does not have as many functions as TCP, such as successful connection, successful sending, etc., and there is no callback function.
intmain()
{
staticboolLED_State=0;
stdio_init_all();
puts("Test IwIP UDP");
if(cyw43_arch_init()) {
printf("Wi-Fi init failed");
return-1;
}
cyw43_arch_enable_sta_mode();
printf("Connecting to Wi-Fi...\n");
if(cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n");
return1;
} else{
printf("Connected.\n");
}
rcv_udp_pcb=udp_new();
err_terr=udp_bind(rcv_udp_pcb,IP_ADDR_ANY,RCV_FROM_PORT);
udp_recv(rcv_udp_pcb, RcvFromUDP,NULL);
while(true) {
sleep_ms(100);
if(LED_State){
LED_State=false;
}else{
LED_State=true;
}
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, LED_State);
}
return0;
}
Writing receiving callback function and sending function
In the receiving function, the sender's related information is printed out, and the received information is sent. In theory, UDP cannot confirm whether the two are connected successfully, nor can it send a "connection successful" message to the Client after the connection is successful like TCP. Only when the information is received can we know who sent the information and then send it back.
voidSendUDP(constip_addr_t*dst_ip, intport, void*data, intdata_size)
{
structpbuf*p=pbuf_alloc(PBUF_TRANSPORT,data_size+1,PBUF_RAM);
char*pt=(char*) p->payload;
memcpy(pt,data,data_size);
pt[data_size]='\0';
cyw43_arch_lwip_begin();
udp_sendto(rcv_udp_pcb,p,dst_ip,port);
printf("send -> %s\n", pt);
cyw43_arch_lwip_end();
pbuf_free(p);
}
voidRcvFromUDP(void*arg, structudp_pcb*pcb, structpbuf*p, constip_addr_t*addr,u16_tport)
{
char*buffer=calloc(p->len+1,sizeof(char));// use calloc to >
strncpy(buffer,(char*)p->payload,p->len);
printf("received from %d.%d.%d.%dport=%d\n",addr->addr&0xff,
(addr->addr>>8)&0xff,
(addr->addr>>16)&0xff,
addr->addr>>24,port);
printf("Length = %d, Total Length = %d\n",p->len,p->tot_len);
printf("payload -> %s\n", buffer);
free(buffer);
pbuf_free(p);
SendUDP(addr,port,buffer,p->len);
}
running result
Run the "lb_socket_Android" APP on your phone. This is the UDP communication software I wrote myself. It is introduced in the previous post and downloaded at the end. Use the method in the previous post to know the IP address of PicoW, then enter the port number and click Connect. Edit the data to be sent, click Send, and you will see the same data received.
Connect to the serial debugging assistant. On the serial debugging assistant, there will be information about the entire WiFi connection process, such as the relevant information printed by the receiving callback function.
APP source code and writing: socket learning (Android sending and receiving)
|