叒宇 · 2021年12月16日

【XR806开发板试用】1、UDP通信测试

XR806官方工程里已经给了WiFi连接的demo,基于这个demo可以很方便的添加代码实现UDP通信。
代码目录结构如下.

ohosdemo
├── BUILD.gn
├── hello_demo
├── iot_peripheral
├── LED
└── wlan_demo
    ├── BUILD.gn
    ├── main.c
    ├── test_case.c
    ├── test_case.h
    ├── udpechoserver.c
    └── udpechoserver.h

wlan_demo/BUILD.gn文件内容。

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",
   ]
}

//udpechoserver.c
#include <udpechoserver.h>

//#include "main.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include <string.h>
#include <stdio.h>

#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);
}

然后在void wifi\\_device\\_connect\\_test()函数里连接WiFi成功后调用udp\\_echoserver\\_init()就可以了.

if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
        printf("Error: GetDeviceMacAddress Fail\r\n");
        return;
    }
    printf("GetDeviceMacAddress Success.\r\n");
    for (int j = 0; j < 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 > 0) {
        OS_Sleep(1);
        printf("%d\n", i);
        i -= 1;
    }

    printf("\r\n=========== DisConnect Test Start ===========\r\n");

运行后串口调试助手显示
 title=
udp网络调试工具界面。后期就可以实现一个udp的上位机来处理数据。
 title=

推荐阅读
关注数
13823
内容数
139
全志XR806开发板相关的知识介绍以及应用专栏。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息