本文在Aworks上移植google prococol buffer C语言库及测试样例使用。
1. 首先学习google protocol buffer 协议参考: https://developers.google.com/protocol-buffers
2. 寻找google protocol buffer 协议 C语言库库链接地址: https://github.com/nanopb/nanopb
3. 移植及验证个人的思路:
- 先把simple样例在PC端正常运行。
- 然后再把simple样例在目标版上验证。
- 最后使用自定义的message.proto生成代码,在PC端验证。
- 再移植message.proto生成的代码至目标板上面,走一次完整的注册流程开。有些人会问为什么不跳过2,直接跳至3会更省时间,本人的开发思路是为了尽快验证移植上是否存在问题,之前使用pbtools时就遇到C11语法问题导致移植失败。
#安将nanopb所需要python包:
root@iZuf6anc2b2vgfvms9d7elZ:~/tmp/PB/nanopb/examples/simple# pip install protobuf
Collecting protobuf
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/dc/a9/fdc31f2c7917d5a1e18f4ca2f955706dfdcaaba8fb0baa48becfc0b68f22/protobuf-3.19.3-py2.py3-none-any.whl (162kB)
100% |████████████████████████████████| 163kB 22.6MB/s
Installing collected packages: protobuf
Successfully installed protobuf-3.19.3
#编译simple样例
root@iZuf6anc2b2vgfvms9d7elZ:~/tmp/PB/nanopb/examples/simple# make
../../generator/protoc --nanopb_out=. simple.proto
cc -Wall -Werror -g -O0 "-I../.." -osimple simple.c simple.pb.c ../../pb_encode.c ../../pb_decode.c ../../pb_common.c
#运行样例
root@iZuf6anc2b2vgfvms9d7elZ:~/tmp/PB/nanopb/examples/simple# ./simple
Your lucky number was 13!
root@iZuf6anc2b2vgfvms9d7elZ:~/tmp/PB/nanopb/examples/simple#
将相关的文件提取至projects\easy_arm_rt1052_debug\user_code\pb
将相关的文件加入Keil工程
添加对应的头文件
将simple.c 的 main函数改为int simple_test_main(),并在工程main.c当中引用。将simple.c的printf改为aw_kprintf,并添加相关的头文件。
Index: D:/svn/rt1052-sdk2.1.2-BlueStone/projects/easy_arm_rt1052_debug/user_code/main.c
===================================================================
--- D:/svn/rt1052-sdk2.1.2-BlueStone/projects/easy_arm_rt1052_debug/user_code/main.c (revision 120)
D:/svn/rt1052-sdk2.1.2-BlueStone/projects/easy_arm_rt1052_debug/user_code/main.c (working copy)
@@ -17,14 17,17 @@
#define LED 0
extern int simple_test_main();
int aw_main (void)
{
aw_kprintf("\r\nApplication Start. \r\n");
simple_test_main();
-
while (1) {
- aw_led_toggle(LED);
//aw_led_toggle(LED);
simple_test_main();
aw_mdelay(500);
}
在内存中运行,如下图所示
AWorks for i.MX RT1050, build Feb 28 2022
AWorks SDK Version is 2.1.2 <2020-09-30>
current time: 1970-01-01 02:42:16
Appl|AWorks->>> ication Start.
AWorks for i.MX RT1050, build Feb 28 2022
AWorks SDK Version is 2.1.2 <2020-09-30>
current time: 1970-01-01 02:44:07
Appl|AWorks->>> ication Start.
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
Your lucky number was 13!
使用如下指令生成相关的文件
root@iZuf6anc2b2vgfvms9d7elZ:~/tmp/PB/nanopb/examples/message# ../../generator/protoc --nanopb_out=. BleMessage2.proto
最终会生成我们所需要的文件:BleMessage2.pb.c BleMessage2.pb.h,如下所示:
root@iZuf6anc2b2vgfvms9d7elZ:~/tmp/PB/nanopb/examples/message# ls
BleMessage2.pb.c BleMessage2.pb.h BleMessage2.proto Makefile README.txt
将BleMessage2.pb.c加入工程,并编写BleMessage2.c,用于自测。本人采用的是直接将联调时产生的编码后的二进制数据赋值到buffer当中直接进行解析。
Your direction was 1!
Your temperature was 1!
Your time was 1!
Your language_Type was 1!
Your SMS was 0!
Your weChat was 0!
Your QQ was 0!
Your mail was 0!
Your calendar was 0!
Your dingDing was 0!
Your blog was 0!
Your reminder was 0!
Your alipay was 0!
将结构体进行二进制序列化是进行跨平台通讯常用的一种方式,这是第一次使用google protocol buffer,内部机制还没能详细学习,只能先学会用,后面有时间再来深入学习。
,