导航

    全志在线开发者论坛

    • 注册
    • 登录
    • 搜索
    • 版块
    • 话题
    • 在线文档
    • 社区主页

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

    Wireless & Analog Series
    1
    1
    1232
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • Z
      zthzz LV 3 最后由 编辑

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

      运行后串口调试助手显示
      1.png
      udp网络调试工具界面。后期准备实现一个udp的上位机来处理数据。
      2.png

      1 条回复 最后回复 回复 引用 分享 0
      • 1 / 1
      • First post
        Last post

      Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

      行为准则 | 用户协议 | 隐私权政策