张雄 · 2022年01月22日

【XR806开发板试用】XR806与鸿蒙,创建任务,串口转发TCPServer收到的数据

很荣幸获得评测开发板的机会,XR806的程序资料做的还是挺不错的。
目标:
1、学习用鸿蒙创建2个任务;
2、创建TCP Server收发数据。
任务ledThread:LED每秒亮灭一次,代表程序在运行。
任务MainThread:创建TCP Server,将接收到的字符串通过串口打印,同时通过TCP回传。

参考社区的环境搭建教程,编译成功。
先说下搭建环境遇到的问题,默认的工具链目录跟实际的会有差别需要根据实际情况修改下。否则会报错。
我用Debian虚拟机。之前按照社区很多文章,搭建都没成功,最后参考下面这个成功了。
环境搭建参考,https://aijishu.com/a/1060000...
1、~/xr806$ vim git-repo/repo
2、修改交叉编译工具。修改device/xradio/xr806/liteos_m/config.gni中的board_toolchain_prefix为实际环境下的gcc路径(/home/openharmony/tool/gcc-arm-none-eabi-10-2020-q4-major/bin)。
3、修改device/xradio/xr806/xr_skylark/gcc.mk

按以上步骤,编译的程序目录是/root/xr806/code/device/xradio/xr806/ohosdemo/hello_demo
我创建一个新目录,用来做实验/root/xr806/code/device/xradio/xr806/ohosdemo/Test
修改 /root/xr806/code/device/xradio/xr806/ohosdemo/BUILD.gn

group("ohosdemo") {
    deps = [# "hello_demo:app_hello",
        #"iot_peripheral:app_peripheral",
        #"wlan_demo:app_WlanTest",
        "Test:app_Test",
    ]
}

Test目录

.
├── BUILD.gn
└── src
    ├── main.c
    ├── test_case.c
    └── test_case.h

修改Test/BUILD.gn

import("//device/xradio/xr806/liteos_m/config.gni")

static_library("app_Test") {
   configs = []

   sources = [
      "main.c",
      "test_case.c",
   ]

   cflags = board_cflags

   include_dirs = board_include_dirs
   include_dirs += [
        ".",
        "//utils/native/lite/include",
            "//foundation/communication/wifi_lite/interfaces/wifiservice",
        "//kernel/liteos_m/kernel/arch/include",
        "include",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//third_party/lwip/src/include",
        "//third_party/cJSON",
   ]
}

main.c

#include <stdio.h>
#include "ohos_init.h"
#include "test_case.h"
#include "kernel/os/os.h"
#include "iot_pwm.h"
#include "iot_gpio.h"
#include "wifi_device.h"
#include "cJSON.h"
#include "lwip/sockets.h"

static OS_Thread_t g_main_thread;
static OS_Thread_t g_led_thread;
static void LedThread(void *arg)
{
    unsigned int gpio_id = 21; /* GPIOA_PIN21 */
    unsigned int gpio_cnt = 0;
    IotGpioDir dir;

    printf("gpio%d output test start\r\n", gpio_id);
    IoTGpioInit(gpio_id);
    IoTGpioSetDir(gpio_id, IOT_GPIO_DIR_OUT);
    IoTGpioGetDir(gpio_id, &dir);
    printf("gpio%d getdir %d\r\n", gpio_id, dir);
    while(1)
    {
        IoTGpioSetOutputVal(gpio_id, 1);
        OS_MSleep(500);
        IoTGpioSetOutputVal(gpio_id, 0);
        OS_MSleep(500);
    }
}

static void MainThread(void *arg)
{
    unsigned int tem = 0, hum = 0, s = 0;
    cJSON* dev_dat = NULL;
    int sfd = 0;
    int cfd = 0;
    char send_buf[512] = {0};
    char recv_buf[512] = {0};
    
    wifi_connect("要连得WIFI","WIFI密码");//请根据实际情况修改
    sfd=tcp_server_init(8000);        
    cfd=tcp_server_accept(sfd);
    while(1)
    {
         memset(recv_buf, 0, sizeof(send_buf));
        recv(cfd, recv_buf, sizeof(recv_buf), 0);
        printf("%s\r\n", recv_buf);
        send(cfd,recv_buf, strlen(recv_buf),0);
    }
}

void WifiTestMain(void)
{
    printf("Wifi Test Start\n");

    if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL,
                OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
        printf("[ERR] Create MainThread Failed\n");
    }
    if(OS_ThreadCreate(&g_led_thread,"ledThread",LedThread,NULL,
                OS_THREAD_PRIO_APP,4*1024)!=OS_OK){
        printf("[ERR] Create LedThread Failed\n");
    }    

}

SYS_RUN(WifiTestMain);

test_case.c

#include <stdio.h>
#include <string.h>
#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "kernel/os/os.h"
#include <stdlib.h>
#include "ohos_init.h"
#include "kernel/os/os.h"
#include "iot_gpio.h"
#include "wifi_device.h"
#include "cJSON.h"
#include "lwip/sockets.h"
void wifi_connect(char *ssid, char *passwd)
{
    char *ssid_want_connect = ssid;
    char *psk = passwd;

    if (WIFI_SUCCESS != EnableWifi()) {
        printf("Error: EnableWifi fail.\n");
        return;
    }

    printf("EnableWifi Success.\n");

    if (WIFI_STA_ACTIVE == IsWifiActive())
        printf("Wifi is active.\n");
    OS_Sleep(1);

    if (WIFI_SUCCESS != Scan()) {
        printf("Error: Scan fail.\n");
        return;
    }

    printf("Wifi Scan Success.\n");
    OS_Sleep(1);

    WifiScanInfo scan_results[30];
    unsigned int scan_num = 30;

    if (WIFI_SUCCESS != GetScanInfoList(scan_results, &scan_num)) {
        printf("Error: GetScanInfoList fail.\n");
        return;
    }

    WifiDeviceConfig config = { 0 };
    int netId = 0;

    int i;
    for (i = 0; i < scan_num; i++) {
        if (0 == strcmp(scan_results[i].ssid, ssid_want_connect)) {
            memcpy(config.ssid, scan_results[i].ssid,
                   WIFI_MAX_SSID_LEN);
            memcpy(config.bssid, scan_results[i].bssid,
                   WIFI_MAC_LEN);
            strcpy(config.preSharedKey, psk);
            config.securityType = scan_results[i].securityType;
            config.wapiPskType = WIFI_PSK_TYPE_ASCII;
            config.freq = scan_results[i].frequency;
            break;
        }
    }

    if (i >= scan_num) {
        printf("Error: No found ssid in scan_results\n");
        return;
    }
    printf("GetScanInfoList Success.\n");
    if (WIFI_SUCCESS != AddDeviceConfig(&config, &netId)) {
        printf("Error: AddDeviceConfig Fail\n");
        return;
    }
    printf("AddDeviceConfig Success.\n");

    if (WIFI_SUCCESS != ConnectTo(netId)) {
        printf("Error: ConnectTo Fail\n");
        return;
    }

    printf("ConnectTo Success\n");
    OS_Sleep(3);
    WifiLinkedInfo get_linked_res;

    if (WIFI_SUCCESS != GetLinkedInfo(&get_linked_res)) {
        printf("Error: GetLinkedInfo Fail\n");
        return;
    }
    printf("GetLinkedInfo Success.\n");

    printf("ssid: %s\n", get_linked_res.ssid);
    printf("bssid: ");
    for (int j = 0; j < WIFI_MAC_LEN; j++) {
        printf("%02X", get_linked_res.bssid[j]);
    }

    printf("\n");
    printf("rssi: %d\n", get_linked_res.rssi);

    unsigned char get_mac_res[WIFI_MAC_LEN];
    if (WIFI_SUCCESS != GetDeviceMacAddress(get_mac_res)) {
        printf("Error: GetDeviceMacAddress Fail\n");
        return;
    }
    printf("GetDeviceMacAddress Success.\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]);
}
int tcp_server_init(int port)
{
    int sfd = 0;
    struct sockaddr_in saddr;
    sfd = socket(AF_INET,SOCK_STREAM,0);
    
    memset(&saddr, 0, sizeof(struct sockaddr));
    saddr.sin_family  = AF_INET;
    saddr.sin_port    = htons(port);
    saddr.sin_addr.s_addr = INADDR_ANY;
    bind(sfd, (struct  socket*)&saddr, sizeof(struct sockaddr));

    listen(sfd,5);

    return sfd;
}

int tcp_server_accept(int sfd)
{
    int cfd = 0;
    struct  sockaddr_in caddr;
    memset(&caddr, 0, sizeof(struct sockaddr));
    int addrl = sizeof(struct sockaddr);
    cfd = accept(sfd , (struct sockaddr*)&caddr , &addrl);
    return cfd;
}

test_case.h

#ifndef __TEST_CASE_H__
#define __TEST_CASE_H__
void wifi_connect(char *ssid, char *passwd);
int tcp_server_init(int port);        
int tcp_server_accept(int sfd);
#endif

编译,看到下图中success就成功了:

编译
root@14d7bf89d2a0:/home/openharmony/code# hb build -f
查看编译报错,碰到报错一个个解决就可以了
root@01f65639f2ef:/home/openharmony/code/out/xr806/wifi_skylark# cat build.log

1642865427(1).png

下载
image.png

下载后按复位,让程序运行。看到LED闪烁。
用串口调试助手,和TCP调试助手连接模块。通过TCP发送数据。可以看到串口和TCP接收窗口返回了同样的数据。
image.png

参考:
环境搭建参考:https://aijishu.com/a/1060000...
TCP连接创建参考:https://aijishu.com/a/1060000...

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