OpenHarmony Socket通信—DAYU200遥控3861小车

2023年 7月 25日 92.1k 0

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

一、DAYU200和3861小车简单介绍

  • 润开鸿OpenHarmony标准系统开发板 DAYU200https://gitee.com/hihope_iot/docs/tree/master/HiHope_DAYU200
  • 购买链接:https://item.taobao.com/item.htm?spm=a230r.7195193.1997079397.7.6e3855b0FokvDV&id=655971020101&abbucket=15

[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区

3861小车
,购买链接:https://item.taobao.com/item.htm?spm=a230r.7195193.1997079397.35.2cb75ea4PUqbMk&id=632728730350&abbucket=15。

    [OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区

二、DAYU200遥控HiSpark-Pegasus智能小车样例介绍

  • 本样例通过TCP socket通信实现DAYU200开发板(OpenHarmony标准系统) 遥控 HiSpark-Pegasus智能小车(OpenHarmony轻量系统)前进、后退、左转、右转、停止等操作,实现了一个简单的OpenHarmony南北向开发互通案例。
  • 样例开发分成3568端小车控制hap应用和3861端c代码

1、3568端小车控制hap应用代码讲解

  • Socket连接 https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/connectivity/socket-connection.md
  • @ohos.net.socket (Socket连接) https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-socket.md#onmessage7-1

3568端小车控制hap应用基于 (需要特别注意开发环境!!!OpenHarmony应用开发更新速度很快)。

  • OpenHarmony3.2release
  • DevEco Studio 3.1.0.500
  • sdk 3.2.12.2

DAYU200通过tcp socket与3861建立通信,hap应用主要实现代码:https://gitee.com/hihope_iot/hispark-pegasus-smart-car/blob/master/DAYU200遥控HiSpark-Pegasus智能小车样例/DAYU200(3568)/car_socket_control/entry/src/main/ets/pages/Index.ets。

[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区

hap应用TCP协议进行通信大致步骤:

  • import需要的socket模块。
  • 创建一个TCPSocket连接,返回一个TCPSocket对象。
  • (可选)订阅TCPSocket相关的订阅事件。
  • 绑定IP地址和端口,端口可以指定或由系统随机分配。
  • 连接到指定的IP地址和端口。
  • 发送数据/接收数据。
  • Socket连接使用完毕后,关闭。

添加获取wifi信息和网络的权限,在entrysrcmainmodule.json5中。

"requestPermissions": [
      {
        "name": "ohos.permission.INTERNET",
        "usedScene": {
          "abilities": [
            "ohos.samples.socket.EntryAbility"
          ],
          "when": "always"
        }
      },
      {
        "name": "ohos.permission.GET_WIFI_INFO",
        "usedScene": {
          "abilities": [
            "ohos.samples.socket.EntryAbility"
          ],
          "when": "always"
        }
      }
    ]

建立tcp通信需要首先获取本地ip地址和端口号。

//为了获取本地IP地址,需要导入@ohos.wifiManager
import wifiManager from '@ohos.wifiManager';

// 解析标准系统开发板(3568)端本地IP地址
export function resolveIP(ip: number): string {
  if (ip  0xFFFFFFFF) {
    throw ('The number is not normal!');
  }
  return (ip >>> 24) + '.' + (ip >> 16 & 0xFF) + '.' + (ip >> 8 & 0xFF) + '.' + (ip & 0xFF);
}

// 获取标准系统开发板(3568)端本地IP地址
let localAddress = resolveIP(wifiManager.getIpInfo().ipAddress);

// 标准系统开发板(3568)本地ip地址和端口
let bindAddress = {
  address: localAddress,
  port: 5678, // 绑定端口,如1234
  family: 1
};

...
...

        //用一个test组件显示本地IP地址
        Text(this.message0)
          .fontSize(25)
          .margin({top:0})
          .fontWeight(FontWeight.Normal)
          .backgroundColor(Color.White)

        //用一个test组件显示本地socket端口号
        Text(this.message1)
          .fontSize(25)
          .margin({top:0})
          .fontWeight(FontWeight.Normal)
          .backgroundColor(Color.White)

tcp初始化,订阅TCPSocket相关的订阅事件,绑定本地ip地址和端口。

// 订阅TCPSocket相关的订阅事件
            tcp.on('message',value => {
              this.message_recv = this.resolveArrayBuffer(value.message)
              console.log(`TCP_data` + this.message_recv);
            });

            tcp.on('connect', () => {
              console.log("on connect")
            });
            tcp.on('close', () => {
              console.log("on close")
            });

            // 绑定本地ip地址和端口
            tcp.bind(bindAddress, err => {
              if (err) {
                promptAction.showToast({
                  message: '[3861_car_control] bind fail',
                  duration: 3000,
                  bottom: 10
                });
                console.log('[3861_car_control] bind fail');
                return;
              }
              promptAction.showToast({
                message: '[3861_car_control] bind success',
                duration: 3000,
                bottom: 10
              });
              console.log('[3861_car_control] bind success');
            });

绑定轻量系统(3861)的ip地址和端口。

// 3861 IP地址和端口
            let connectAddress = {
              address: this.car_ipaddress,
              port: num, // 端口号
              family: 1
            };

            //连接到指定的3861IP地址和端口
            tcp.connect({
              address: connectAddress, timeout: 6000
            }, err => {
              if (err) {
                promptAction.showToast({
                  message: '[3861_car_control] connect'+this.car_ipaddress+' fail',
                  duration: 3000,
                  bottom: 10
                });
                console.log('[3861_car_control] connect'+this.car_ipaddress+' fail');
                return;
              }
              promptAction.showToast({
                message: '[3861_car_control] connect'+this.car_ipaddress+' success',
                duration: 3000,
                bottom: 10
              });
              console.log('[3861_car_control] connect'+this.car_ipaddress+' success');
            });

3568发送数据至3861。

tcp.send({
              data: '0'
            }, err => {
             if (err) {
                console.log('[3861_car_control] send fail');
                return;
              }
              console.log('[3861_car_control] send success');
            })

3568接收3861发送的数据。

//解析标准系统开发板(3568)接收到的 轻量系统(3861)发送的数据
  resolveArrayBuffer(message: ArrayBuffer): string {
    if (message instanceof ArrayBuffer) {
      let dataView = new DataView(message)
      console.info(`length ${dataView.byteLength}`)
      let str = ""
      for (let i = 0;i  {
              this.message_recv = this.resolveArrayBuffer(value.message)
              console.log(`TCP_data` + this.message_recv);
            });

···
···

        //用一个text显示标准系统开发板(3568)接收到轻量系统(3861)发出的数据
        Text('接收到的3861发送的数据:' + this.message_recv)
          .fontSize(25)
          .margin({top:0})
          .fontWeight(FontWeight.Normal)
          .backgroundColor(Color.White)

关闭tcp连接。

tcp.close((err) => {
              promptAction.showToast({
                message: '[3861_car_control] close socket.',
                duration: 3000,
                bottom: 10
              });
              console.log('[3861_car_control] close socket.')
            });
            tcp.off('message');
            tcp.off('connect');
            tcp.off('close');

完成代码实现请查看: https://gitee.com/hihope_iot/hispark-pegasus-smart-car/blob/master/DAYU200遥控HiSpark-Pegasus智能小车样例/DAYU200(3568)/car_socket_control/entry/src/main/ets/pages/Index.ets。

如果要修改hap应用的名称,在car_socket_controlentrysrcmainresourceszh_CNelementstring.json文件中修改EntryAbility_label属性。

[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区

如果要修改应用图标修改car_socket_controlentrysrcmainresourcesbasemedia下icon.png(像素大小为114×114)。

[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区[OpenHarmony Socket通信]DAYU200遥控3861小车-开源基础软件社区

2、3861端小车代码讲解

代码地址: https://gitee.com/hihope_iot/hispark-pegasus-smart-car/tree/master/DAYU200遥控HiSpark-Pegasus智能小车样例/HiSpark-Pegasus-smart-car(3861)/car_tcp_control。

文件名

说明

BUILD.gn

OpenHarmony构建脚本

demo_entry_cmsis.c

OpenHarmony liteos-m程序入口

net_common.h

系统网络接口头文件

net_demo.h

demo脚手架头文件

net_params.h

网络参数,包括WiFi热点信息、端口信息

car_tcp_server_test.c

TCP socket控制小车实现代码

wifi_connecter.c

OpenHarmony WiFi STA模式API的封装实现文件

wifi_connecter.h

OpenHarmony WiFi STA模式API的封装头文件

本篇文章主要讲解主要实现代码car_tcp_server_test.c (笔者在代码提交了比较详细的注释)

3861tcp发送数据。

//3861发送数据
retval = send(connfd, request, strlen(request), 0);
if (retval

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论