【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");

运行后串口调试助手显示
在这里插入图片描述

udp网络调试工具界面。后期就可以实现一个udp的上位机来处理数据。
在这里插入图片描述

相关推荐

  1. XR806开发试用xr806 RTC实验

    2024-01-30 15:20:02       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-30 15:20:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-30 15:20:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-30 15:20:02       82 阅读
  4. Python语言-面向对象

    2024-01-30 15:20:02       91 阅读

热门阅读

  1. 超级自驱力——每个人都是自己的CEO

    2024-01-30 15:20:02       64 阅读
  2. Spring 学习2 --基于xml管理Bean

    2024-01-30 15:20:02       48 阅读
  3. 当量子计算机普及化:对未来生活方式的探讨

    2024-01-30 15:20:02       56 阅读
  4. 【leetcode100-077到080】【贪心】四题合集

    2024-01-30 15:20:02       58 阅读
  5. 服务器自启动服务总结

    2024-01-30 15:20:02       81 阅读
  6. LarkXR渲染服务器架构升级,探索XR新体验

    2024-01-30 15:20:02       61 阅读
  7. 前端自己整理的学习面试笔记

    2024-01-30 15:20:02       54 阅读
  8. XR虚拍技术:重塑微剧与短剧产业的创新引擎

    2024-01-30 15:20:02       65 阅读
  9. C#关键字ref和out

    2024-01-30 15:20:02       59 阅读
  10. 图的前向星表示2

    2024-01-30 15:20:02       53 阅读