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

news/2024/5/18 16:37:21 标签: 单片机, udp, stm32

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的上位机来处理数据。
在这里插入图片描述


http://www.niftyadmin.cn/n/5358622.html

相关文章

探秘本庄村果园预售系统的技术之旅

✍✍计算机编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java实战 |…

Linux系统——防火墙

一、防火墙的认识 引言 安全技术 入侵检测系统&#xff08;Intrusion Detection Systems&#xff09;&#xff1a;特点是不阻断任何网络访问&#xff0c;量化、定位来自内外网络的威胁情况&#xff0c;主要以提供报警和事后监督为主&#xff0c;提供有针对性的指导措施和安全…

保姆式挑选钢化膜教程,不看真的后悔

覆盖率 我们怎样挑选钢化膜呢&#xff1f;选手机膜最重要的是看它的覆盖率&#xff0c;所谓覆盖就是手机膜覆盖住你的手机屏幕&#xff0c;一般覆盖率达到 97% 左右&#xff0c;几乎就感受不到膜的存在。 很多朋友应该听说过 2D 钢化膜&#xff0c;它是没有经过边缘抛光处理的…

vue 使用 v-viewer 用于图片浏览的Vue组件,支持旋转、缩放、翻转等操作,基于viewer.js。

作者连接 npm&#xff1a; npm install v-viewerlegacy viewerjs main.js 引入&#xff1a; // 引入Viewer插件 import VueViewer, { directive as viewerDirective } from v-viewer; // 引入Viewer插件的图片预览器的样式 import viewerjs/dist/viewer.css; // 使用Viewer图片…

vue2父组件向子组件传值时,子组件同时接收多个数据类型,控制台报警的问题

最近项目遇到一个问题,就是我的父组件向子组件(公共组件)传值时,子组件同时接收多个数据类型,控制台报警的问题,如下图,子组件明明写了可同时接收字符串,整型和布尔值,但控制台依旧报警: 仔细检查父组件,发现父组件是这样写的: <common-tabletooltip :content=…

复刻桌面小电视【包含代码分析】

宗旨&#xff1a;开源、分享、学习、进步&#xff0c;生命不息&#xff0c;折腾不止。 复刻小电视 感谢各位大佬的开源项目&#xff0c;让我有了学习的机会&#xff0c;如果侵权&#xff0c;请联系我删除。本人能力有限&#xff0c;如果有什么不对的地方&#xff0c;欢迎指正…

springboot147校园失物招领系统

校园失物招领系统 目 录 目 录 I 摘 要 III ABSTRACT IV 1 绪论 1 1.1 课题背景 1 1.2 研究现状 1 1.3 研究内容 2 2 系统开发环境 3 2.1 vue技术 3 2.2 JAVA技术 3 2.3 MYSQL数据库 3 2.4 B/S结构 4 2.5 SSM框架技术 4 3 系统分析 5 3.1 可行性分析 5 3.1.1 …

react 之 UseMemo

useMemo 看个场景 下面我们的本来的用意是想基于count的变化计算斐波那契数列之和&#xff0c;但是当我们修改num状态的时候&#xff0c;斐波那契求和函数也会被执行&#xff0c;显然是一种浪费 // useMemo // 作用&#xff1a;在组件渲染时缓存计算的结果import { useState …