diff --git a/modbus/BUILD.gn b/modbus/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..7c89fc001df289eb7baf5d9b571b92588dd743a5 --- /dev/null +++ b/modbus/BUILD.gn @@ -0,0 +1,97 @@ +# Copyright (c) 2023 iSoftStone Information Technology (Group) Co.,Ltd. +# +# SPDX-License-Identifier: LGPL-2.1-or-later + +import("//build/ohos.gni") + +modbus_path = "//third_party/modbus" +modbus_part_name = "modbus" +modbus_subsystem_name = "thirdparty" + +modbus_includes = [ + "${modbus_path}", + "${modbus_path}/src", +] + +config("modbus_config") { + cflags = [ + "-Wall", + "-Wmissing-declarations", + "-Wmissing-prototypes", + "-Wnested-externs", + "-Wpointer-arith", + "-Wpointer-arith", + "-Wsign-compare", + "-Wchar-subscripts", + "-Wstrict-prototypes", + "-Wshadow", + "-Wformat-security", + ] +} + +ohos_shared_library("modbus") { + install_enable = true + include_dirs = modbus_includes + sources = [ + "${modbus_path}/src/modbus-data.c", + "${modbus_path}/src/modbus-rtu.c", + "${modbus_path}/src/modbus-tcp.c", + "${modbus_path}/src/modbus.c", + ] + configs = [ ":modbus_config" ] + part_name = "${modbus_part_name}" + subsystem_name = "${modbus_subsystem_name}" +} + +template("swanlink_modbus_test") { + ohos_executable(target_name) { + use_exceptions = true + include_dirs = [ "tests" ] + include_dirs += modbus_includes + deps = [ ":modbus" ] + configs = [ ":modbus_config" ] + part_name = "${modbus_part_name}" + subsystem_name = "${modbus_subsystem_name}" + forward_variables_from(invoker, "*") + } +} + +swanlink_modbus_test("unit-test-client") { + sources = [ "tests/unit-test-client.c" ] +} + +swanlink_modbus_test("unit-test-server") { + sources = [ "tests/unit-test-server.c" ] +} + +swanlink_modbus_test("random-test-client") { + sources = [ "tests/random-test-client.c" ] +} + +swanlink_modbus_test("random-test-server") { + sources = [ "tests/random-test-server.c" ] +} + +swanlink_modbus_test("bandwidth-client") { + sources = [ "tests/bandwidth-client.c" ] +} + +swanlink_modbus_test("bandwidth-server-one") { + sources = [ "tests/bandwidth-server-one.c" ] +} + +swanlink_modbus_test("bandwidth-server-many-up") { + sources = [ "tests/bandwidth-server-many-up.c" ] +} + +group("modbus_tests") { + deps = [ + ":unit-test-client", + ":unit-test-server", + ":random-test-client", + ":random-test-server", + ":bandwidth-client", + ":bandwidth-server-one", + ":bandwidth-server-many-up", + ] +} diff --git a/modbus/README.OpenSource b/modbus/README.OpenSource new file mode 100755 index 0000000000000000000000000000000000000000..50e53b21888edc89801839f67a6459cd98df9251 --- /dev/null +++ b/modbus/README.OpenSource @@ -0,0 +1,11 @@ +[ + { + "Name": "modbus", + "License": "LGPL v2.1 or Later", + "License File": "COPYING.LESSER", + "Version Number": "3.1.10", + "Owner": "swanlink", + "Upstream URL": "https://github.com/stephane/libmodbus", + "Description": "libmodbus is a free software library to send/receive data with a device which respects the Modbus protocol. This library can use a serial port or an Ethernet connection. The Protocol Reference Guide which can be obtained from http://www.modbus.org." + } +] \ No newline at end of file diff --git a/modbus/README_zh.md b/modbus/README_zh.md new file mode 100755 index 0000000000000000000000000000000000000000..9230d9671457608ff9f2e3f6319051af4b6bbc02 --- /dev/null +++ b/modbus/README_zh.md @@ -0,0 +1,16 @@ +# modbus 三方库说明 + +## 功能简介 + +modbus 是用 C 语言编写的第三方Modbus库来实现modbus通讯,支持 RTU(串行)和 TCP(以太网)通信模式,可根据 Modbus 协议发送和接收数据。 + +## 使用约束 + +- ROM版本:OpenHarmony3.2 beta4 +- 三方库版本:v3.1.10 +- 当前适配的功能:完成modbus鸿蒙编译的适配 +- License:[LGPL v2.1 or Later](https://github.com/stephane/libmodbus/COPYING.LESSER) + +## 集成方式 + +- [系统Rom包集成](./docs/rom_integrate.md)。 diff --git a/modbus/adapted/src/config.h b/modbus/adapted/src/config.h new file mode 100755 index 0000000000000000000000000000000000000000..558be8210710c6a346e7b75bc8c4a241eb987faa --- /dev/null +++ b/modbus/adapted/src/config.h @@ -0,0 +1,233 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the `accept4' function. */ +#define HAVE_ACCEPT4 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ARPA_INET_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_BYTESWAP_H 1 + +/* Define to 1 if you have the declaration of `TIOCM_RTS', and to 0 if you + don't. */ +#define HAVE_DECL_TIOCM_RTS 1 + +/* Define to 1 if you have the declaration of `TIOCSRS485', and to 0 if you + don't. */ +#define HAVE_DECL_TIOCSRS485 1 + +/* Define to 1 if you have the declaration of `__CYGWIN__', and to 0 if you + don't. */ +#define HAVE_DECL___CYGWIN__ 0 + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ERRNO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_FCNTL_H 1 + +/* Define to 1 if you have the `getaddrinfo' function. */ +#define HAVE_GETADDRINFO 1 + +/* Define to 1 if you have the `gettimeofday' function. */ +#define HAVE_GETTIMEOFDAY 1 + +/* Define to 1 if you have the `inet_ntop' function. */ +#define HAVE_INET_NTOP 1 + +/* Define to 1 if you have the `inet_pton' function. */ +#define HAVE_INET_PTON 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LIMITS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LINUX_SERIAL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_NETDB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_NETINET_IN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_NETINET_TCP_H 1 + +/* Define to 1 if you have the `select' function. */ +#define HAVE_SELECT 1 + +/* Define to 1 if you have the `socket' function. */ +#define HAVE_SOCKET 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the `strerror' function. */ +#define HAVE_STRERROR 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the `strlcpy' function. */ +/* #undef HAVE_STRLCPY */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_IOCTL_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_PARAMS_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_TERMIOS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_WINSOCK2_H */ + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "libmodbus" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "https://github.com/stephane/libmodbus/issues" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "libmodbus" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "libmodbus 3.1.10" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "libmodbus" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "http://libmodbus.org/" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "3.1.10" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# define _ALL_SOURCE 1 +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# define _POSIX_PTHREAD_SEMANTICS 1 +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# define _TANDEM_SOURCE 1 +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# define __EXTENSIONS__ 1 +#endif + + +/* Version number of package */ +#define VERSION "3.1.10" + +/* _ */ +#define WINVER 0x0501 + +/* Enable large inode numbers on Mac OS X 10.5. */ +#ifndef _DARWIN_USE_64_BIT_INODE +# define _DARWIN_USE_64_BIT_INODE 1 +#endif + +/* Number of bits in a file offset, on hosts where this is settable. */ +/* #undef _FILE_OFFSET_BITS */ + +/* Define for large files, on AIX-style hosts. */ +/* #undef _LARGE_FILES */ + +/* Define to 1 if on MINIX. */ +/* #undef _MINIX */ + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +/* #undef _POSIX_1_SOURCE */ + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +/* #undef _POSIX_SOURCE */ + +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT32_T */ + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT8_T */ + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +/* #undef inline */ +#endif + +/* Define to the type of a signed integer type of width exactly 64 bits if + such a type exists and the standard includes do not define it. */ +/* #undef int64_t */ + +/* Define to `unsigned int' if does not define. */ +/* #undef size_t */ + +/* Define to `int' if does not define. */ +/* #undef ssize_t */ + +/* Define to the type of an unsigned integer type of width exactly 16 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint16_t */ + +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint32_t */ + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint8_t */ diff --git a/modbus/adapted/src/modbus-version.h b/modbus/adapted/src/modbus-version.h new file mode 100755 index 0000000000000000000000000000000000000000..7f41cce25eacaf03337f865183746e76856605bf --- /dev/null +++ b/modbus/adapted/src/modbus-version.h @@ -0,0 +1,51 @@ +/* + * Copyright © Stéphane Raimbault + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef MODBUS_VERSION_H +#define MODBUS_VERSION_H + +/* The major version, (1, if %LIBMODBUS_VERSION is 1.2.3) */ +#define LIBMODBUS_VERSION_MAJOR (3) + +/* The minor version (2, if %LIBMODBUS_VERSION is 1.2.3) */ +#define LIBMODBUS_VERSION_MINOR (1) + +/* The micro version (3, if %LIBMODBUS_VERSION is 1.2.3) */ +#define LIBMODBUS_VERSION_MICRO (10) + +/* The full version, like 1.2.3 */ +#define LIBMODBUS_VERSION 3.1.10 + +/* The full version, in string form (suited for string concatenation) + */ +#define LIBMODBUS_VERSION_STRING "3.1.10" + +/* Numerically encoded version, eg. v1.2.3 is 0x010203 */ +#define LIBMODBUS_VERSION_HEX \ + ((LIBMODBUS_VERSION_MAJOR << 16) | (LIBMODBUS_VERSION_MINOR << 8) | \ + (LIBMODBUS_VERSION_MICRO << 0)) + +/* Evaluates to True if the version is greater than @major, @minor and @micro + */ +#define LIBMODBUS_VERSION_CHECK(major, minor, micro) \ + (LIBMODBUS_VERSION_MAJOR > (major) || \ + (LIBMODBUS_VERSION_MAJOR == (major) && LIBMODBUS_VERSION_MINOR > (minor)) || \ + (LIBMODBUS_VERSION_MAJOR == (major) && LIBMODBUS_VERSION_MINOR == (minor) && \ + LIBMODBUS_VERSION_MICRO >= (micro))) + +#endif /* MODBUS_VERSION_H */ diff --git a/modbus/adapted/tests/unit-test.h b/modbus/adapted/tests/unit-test.h new file mode 100755 index 0000000000000000000000000000000000000000..50d80703fbc77f8e5947a12a21541f44762d65e7 --- /dev/null +++ b/modbus/adapted/tests/unit-test.h @@ -0,0 +1,107 @@ +/* tests/unit-test.h. Generated from unit-test.h.in by configure. */ +/* + * Copyright © Stéphane Raimbault + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _UNIT_TEST_H_ +#define _UNIT_TEST_H_ + +/* Constants defined by configure.ac */ +#define HAVE_INTTYPES_H 1 +#define HAVE_STDINT_H 1 + +// clang-format off +#ifdef HAVE_INTTYPES_H +#include +#endif +#ifdef HAVE_STDINT_H +# ifndef _MSC_VER +# include +# else +# include "stdint.h" +# endif +#endif +// clang-format on + +#define SERVER_ID 17 +#define INVALID_SERVER_ID 18 + +const uint16_t UT_BITS_ADDRESS = 0x130; +const uint16_t UT_BITS_NB = 0x25; +const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B }; + +const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4; +const uint16_t UT_INPUT_BITS_NB = 0x16; +const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 }; + +const uint16_t UT_REGISTERS_ADDRESS = 0x160; +const uint16_t UT_REGISTERS_NB = 0x3; +const uint16_t UT_REGISTERS_NB_MAX = 0x20; +const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 }; + +/* Raise a manual exception when this address is used for the first byte */ +const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x170; +/* The response of the server will contains an invalid TID or slave */ +const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x171; +/* The server will wait for 1 second before replying to test timeout */ +const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x172; +/* The server will wait for 5 ms before sending each byte */ +const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x173; + +/* If the following value is used, a bad response is sent. + It's better to test with a lower value than + UT_REGISTERS_NB_POINTS to try to raise a segfault. */ +const uint16_t UT_REGISTERS_NB_SPECIAL = 0x2; + +const uint16_t UT_INPUT_REGISTERS_ADDRESS = 0x108; +const uint16_t UT_INPUT_REGISTERS_NB = 0x1; +const uint16_t UT_INPUT_REGISTERS_TAB[] = { 0x000A }; + +/* + * This float value is 0x47F12000 (in big-endian format). + * In Little-endian(intel) format, it will be stored in memory as follows: + * 0x00 0x20 0xF1 0x47 + * + * You can check this with the following code: + + float fl = UT_REAL; + uint8_t *inmem = (uint8_t*)&fl; + int x; + for(x = 0; x < 4; x++){ + printf("0x%02X ", inmem[ x ]); + } + printf("\n"); + */ +const float UT_REAL = 123456.00; + +/* + * The following arrays assume that 'A' is the MSB, + * and 'D' is the LSB. + * Thus, the following is the case: + * A = 0x47 + * B = 0xF1 + * C = 0x20 + * D = 0x00 + * + * There are two sets of arrays: one to test that the setting is correct, + * the other to test that the getting is correct. + * Note that the 'get' values must be constants in processor-endianness, + * as libmodbus will convert all words to processor-endianness as they come in. + */ +const uint8_t UT_IREAL_ABCD_SET[] = {0x47, 0xF1, 0x20, 0x00}; +const uint16_t UT_IREAL_ABCD_GET[] = {0x47F1, 0x2000}; +const uint8_t UT_IREAL_DCBA_SET[] = {0x00, 0x20, 0xF1, 0x47}; +const uint16_t UT_IREAL_DCBA_GET[] = {0x0020, 0xF147}; +const uint8_t UT_IREAL_BADC_SET[] = {0xF1, 0x47, 0x00, 0x20}; +const uint16_t UT_IREAL_BADC_GET[] = {0xF147, 0x0020}; +const uint8_t UT_IREAL_CDAB_SET[] = {0x20, 0x00, 0x47, 0xF1}; +const uint16_t UT_IREAL_CDAB_GET[] = {0x2000, 0x47F1}; + +/* const uint32_t UT_IREAL_ABCD = 0x47F12000); +const uint32_t UT_IREAL_DCBA = 0x0020F147; +const uint32_t UT_IREAL_BADC = 0xF1470020; +const uint32_t UT_IREAL_CDAB = 0x200047F1;*/ + +#endif /* _UNIT_TEST_H_ */ diff --git a/modbus/bundle.json b/modbus/bundle.json new file mode 100755 index 0000000000000000000000000000000000000000..fc41954f69975f6b1a4f961c50fab1d90b192874 --- /dev/null +++ b/modbus/bundle.json @@ -0,0 +1,32 @@ +{ + "name": "@ohos/modbus", + "description": "modbus is an Open Source library to communicate with Modbus devices: RTU (serial) and TCP (Ethernet) support, available for Linux (packaged), FreeBSD, Mac OS and Windows, written in C, great test coverage and documentation, security audits, no dependencies.", + "version": "3.1.10", + "license": "LGPL v2.1 or later", + "publishAs": "code-segment", + "segment": { + "destPath": "third_party/modbus" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "modbus", + "subsystem": "thirdparty", + "syscap": [], + "features": [], + "adapted_system_type": [], + "rom": "", + "ram": "", + "deps": { + "components": [], + "third_party": [] + }, + "build": { + "sub_component": [ + "//third_party/modbus:modbus" + ], + "inner_kits": [], + "test": [] + } + } +} diff --git a/modbus/docs/pic/running_result.png b/modbus/docs/pic/running_result.png new file mode 100755 index 0000000000000000000000000000000000000000..2f3c53e859950f762eeb6b16848fb03088d97c36 Binary files /dev/null and b/modbus/docs/pic/running_result.png differ diff --git a/modbus/docs/rom_integrate.md b/modbus/docs/rom_integrate.md new file mode 100755 index 0000000000000000000000000000000000000000..36165d8cd02bac6b00df20a98586d2baa584937c --- /dev/null +++ b/modbus/docs/rom_integrate.md @@ -0,0 +1,152 @@ +# modbus如何集成到系统Rom + +## 准备源码工程 + +本库是基于OpenHarmony 3.2 beta4版本,在润和RK3568开发板上验证的。如果是从未使用过RK3568,可以先查看[润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld)。 + +### 准备系统Rom源码 + +系统源码获取方法请参照:[OpenHarmony3.2beta4源码下载](https://gitee.com/openharmony/docs/blob/OpenHarmony-v3.2-Beta5/zh-cn/release-notes/OpenHarmony-v3.2-beta4.md#源码获取) + +### 增加构建脚本及配置文件 + +- 下载本仓库代码 + + ```shell + cd ~/ + git clone git@gitee.com:openharmony-sig/tpc_c_cplusplus.git --depth=1 + ``` + +- 仓库代码目录结构说明 + + ```shell + cd ~/tpc_c_cplusplus/modbus/ # 进入到仓库代码库目录 + ``` + + ```shell + modbus + |-- adapted # 存放三方库适配需要的代码文件 + |-- docs # 存放 modbus 相关文档的文件夹 + |-- BUILD.gn # 编译构建脚本 + |-- bundle.json # 三方库组件定义文件 + |-- README_zh.md # modbus 主页说明文档 + |-- README.OpenSource # modbus 源码的下载地址,版本,license等信息 + ``` + +- 将本仓库文件夹拷贝到OpenHarmony源码的third_party目录下 + + ```shell + cp -arf ~/tpc_c_cplusplus/modbus ~/OpenHarmony/third_party + ``` + +### 准备三方库源码 + +- 三方库源码下载 + + modbus-3.1.10源码下载到modbus目录并将代码拷贝出来。 + + ```shell + cd ~/OpenHarmony/third_party/modbus + git clone https://github.com/stephane/libmodbus.git -b v3.1.10 + ``` + +## 系统Rom中引入三方库 + +准备完三方库代码后,我们需要将三方库加入到编译构建体系中。标准系统编译构建可以参考文档[编译构建指导](https://gitee.com/openharmony/docs/blob/OpenHarmony-3.2-Beta4/zh-cn/device-dev/subsystems/subsys-build-all.md)。
+我们默认三方库是属于OpenHarmony的thirdparty子系统,如果需要自己定义子系统参考文档[如何为三方库组件中添加一个三方库](https://gitee.com/openharmony-sig/knowledge/blob/master/docs/openharmony_getstarted/port_thirdparty/README.md)。
+新增需要编译的组件,在OpenHarmony源码的vendor/hihope/rk3568/config.json文件中,新增需要编译的组件,如下代码段所示,在thirdparty子系统下面新增modbus组件 + +```json +{ + "subsystem": "thirdparty", + "components": [ + { + "component": "musl", + "features": [] + }, + { + "component": "modbus", + "features": [] + } + ] +} +``` + +## 系统Rom中引入三方库测试程序 + +如果需要编译测试用例,在OpenHarmony源码的vendor/hihope/rk3568/config.json文件,在对应组件的features中打开编译选项,如下 + +```json +{ + "subsystem": "thirdparty", + "components": [ + { + "component": "musl", + "features": [] + }, + { + "component": "modbus", + "features": [] + } + ] +} +``` + +## 编译工程 + +- 进入源码根目录 + + ```shell + cd ~/openharmony + ``` + +- 选择产品 + + ```shell + hb set # 运行hb set后会出现产品列表,在此我们选择 rk3568 + ``` + +- 执行编译 + + ```shell + hb build --target-cpu arm #编译32位系统 + hb build --target-cpu arm64 #编译64位系统 + ``` + +- 生成的可执行文件和库文件都在out/rk3568/thirdparty/modbus目录下,同时也打包到了镜像中 + +## 运行效果 + +将编译生成的库和测试文件放到板子上运行,为避免每次将文件推入设备都要烧录整个镜像,我们使用hdc_std工具将文件推到开发板上 + +- 首先将hdc_std工具编译出来
+ 执行以下命令进行编译hdc_std工具(工具编译出来所在路径out/sdk/ohos-sdk/windows/toolchains/hdc_std.exe): + + ```shell + hb set #源码根目录下使用hb set 选择产品ohos-sdk + hb build #然后编译 + ``` + +- 将工具拷贝到Windows,可以为工具目录配置环境变量,也可以在工具所在目录打开windows命令行 +- 将原生库测试需要的所有文件打包成modbus.tar,并拷贝到windows下 +- 将文件推送到开发板,在windows命令行进行如下操作 + + ```shell + hdc_std shell mount -o remount,rw / #修改系统权限为可读写 + hdc_std file send modbus.tar / #将文件包推入开发板 + hdc_std shell #进入开发板 + tar xvf modbus.tar #解压 + #注意需要将库拷贝到/system/lib 或者/system/lib64目录下 + ``` + +- 运行测试程序 + 在测试目录运行测试程序unit-test-server,然后再另一个窗口运行测试程序unit-test-client,则两个窗口会同时看到数据发送和接收的打印信息,如下图所示:
+  ![image-20220922143536085](./pic/running_result.png) + +## 参考资料 + +- [润和RK3568开发板标准系统快速上手](https://gitee.com/openharmony-sig/knowledge_demo_temp/tree/master/docs/rk3568_helloworld)。 +- [OpenHarmony3.2beta4](https://gitee.com/openharmony/docs/blob/OpenHarmony-v3.2-Beta5/zh-cn/release-notes/OpenHarmony-v3.2-beta4.md)。 +- [OpenHarmony三方库组织地址](https://gitee.com/openharmony-tpc)。 +- [如何为三方库组件中添加一个三方库](https://gitee.com/openharmony-sig/knowledge/blob/master/docs/openharmony_getstarted/port_thirdparty/README.md)。 +- [知识体系](https://gitee.com/openharmony-sig/knowledge)。