<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[【XR806开发板试用】1、UDP通信测试]]></title><description><![CDATA[<p dir="auto">XR806官方工程里已经给了WiFi连接的demo，基于这个demo可以很方便的添加代码实现UDP通信。<br />
代码目录结构如下.</p>
<pre><code>ohosdemo
├── BUILD.gn
├── hello_demo
├── iot_peripheral
├── LED
└── wlan_demo
    ├── BUILD.gn
    ├── main.c
    ├── test_case.c
    ├── test_case.h
    ├── udpechoserver.c
    └── udpechoserver.h
</code></pre>
<p dir="auto">wlan_demo/BUILD.gn文件内容.</p>
<pre><code>import("//device/xradio/xr806/liteos_m/config.gni")

static_library("app_WlanTest") {
   configs = []
   sources = [
      "main.c",
      "test_case.c",
      "udpechoserver.c"
   ]

   cflags = board_cflags

   include_dirs = board_include_dirs
   include_dirs += [
       ".",
       "//utils/native/lite/include",
       "//foundation/communication/wifi_lite/interfaces/wifiservice",
       "//third_party/lwip/src/include",
   ]
}
</code></pre>
<pre><code>//udpechoserver.c
#include &lt;udpechoserver.h&gt;

//#include "main.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;

#define UDP_SERVER_PORT    5554   /* define the UDP local connection port */
#define UDP_CLIENT_PORT    5555   /* define the UDP remote connection port */

void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb,struct pbuf *p, const ip_addr_t *addr, u16_t port);

void udp_echoserver_init(void)
{
	struct udp_pcb *upcb;
	err_t err;
	/* Create a new UDP control block  */
	upcb = udp_new();
	if (upcb)
	{
		/* Bind the upcb to the UDP_PORT port */
		/* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
		err = udp_bind(upcb, IP_ADDR_ANY, UDP_SERVER_PORT);
		if (err == ERR_OK)
		{
			/* Set a receive callback for the upcb */
			udp_recv(upcb, udp_echoserver_receive_callback, NULL);
			printf("udp bind OK \r\n");
		}else	
			printf("udp bind error \r\n");
	}
}

void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb,struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
	printf("udp receive_callback \r\n");
	/* Connect to the remote client */
	udp_connect(upcb, addr, UDP_CLIENT_PORT);
	/* Tell the client that we have accepted it */
	udp_send(upcb, p);
	/* free the UDP connection, so we can accept new clients */
	udp_disconnect(upcb);
	/* Free the p buffer */
	pbuf_free(p);
}
</code></pre>
<p dir="auto">然后在void wifi_device_connect_test()函数里连接WiFi成功后调用udp_echoserver_init()就可以了.</p>
<pre><code>	if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
		printf("Error: GetDeviceMacAddress Fail\r\n");
		return;
	}
	printf("GetDeviceMacAddress Success.\r\n");
	for (int j = 0; j &lt; WIFI_MAC_LEN - 1; j++) {
		printf("%02X:", get_mac_res[j]);
	}
	printf("%02X\n", get_mac_res[WIFI_MAC_LEN - 1]);
	printf("\r\n======= Connect Test End, Wait a second =======\r\n");

	//udp初始化;
	udp_echoserver_init();

	//死循环.让线程停在这个位置.
	while (1)
	{
		OS_Sleep(100);
	}

	i = 800;
	while (i &gt; 0) {
		OS_Sleep(1);
		printf("%d\n", i);
		i -= 1;
	}

	printf("\r\n=========== DisConnect Test Start ===========\r\n");
</code></pre>
<p dir="auto">运行后串口调试助手显示<br />
<img src="/assets/uploads/files/1639649380617-1.png" alt="1.png" class=" img-responsive img-markdown" width="1224" height="547" /><br />
udp网络调试工具界面。后期准备实现一个udp的上位机来处理数据。<br />
<img src="/assets/uploads/files/1639649516019-2.png" alt="2.png" class=" img-responsive img-markdown" width="855" height="556" /></p>
]]></description><link>https://bbs.aw-ol.com/topic/801/xr806开发板试用-1-udp通信测试</link><generator>RSS for Node</generator><lastBuildDate>Wed, 13 May 2026 03:38:23 GMT</lastBuildDate><atom:link href="https://bbs.aw-ol.com/topic/801.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 Dec 2021 10:21:24 GMT</pubDate><ttl>60</ttl></channel></rss>