当前位置:网站首页>基于RT1052 Aworks nanopb string 类型固定长度使用方式(二十七)
基于RT1052 Aworks nanopb string 类型固定长度使用方式(二十七)
2022-07-31 21:34:00 【嵌入式实操】
本文主要是通过迁移的思维,记录本人初次使用周立功的Aworks框架进行开发
本文在Aworks上使用nanopb库对string类型使用限制最大长度
1. 定义string消息协议
// A very simple protocol definition, consisting of only
// one message.
syntax = "proto2";
package com.bst.modules.iot.proto;
import "nanopb.proto";
message LaneStatus {
bool showLane = 1; //true:显示/ false:隐藏
string laneBackInfo = 2 [(nanopb).max_size = 128]; //车道背景信息
string laneSelectInfo = 3 [(nanopb).max_size = 128]; //车道前景信息
}
使用如下命令进行编译
..\..\generator-bin\protoc --nanopb_out=. simple.proto
最终生成的结构体
/* Struct definitions */
typedef struct _com_bst_modules_iot_proto_LaneStatus {
bool showLane; /* true:显示/ false:隐藏 */
char laneBackInfo[128]; /* 车道背景信息 */
char laneSelectInfo[128]; /* 车道前景信息 */
} com_bst_modules_iot_proto_LaneStatus;
2.测试代码
int lane_status_test_main()
{
/* This is the buffer where we will store our message. */
uint8_t buffer[128] = {
0};
size_t message_length = 0;
bool status;
/* Encode our message */
{
/* Allocate space on the stack to store the message data. * * Nanopb generates simple struct definitions for all the messages. * - check out the contents of simple.pb.h! * It is a good idea to always initialize your structures * so that you do not have garbage data from RAM in there. */
com_bst_modules_iot_proto_LaneStatus sign = com_bst_modules_iot_proto_LaneStatus_init_zero;
/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
/* Fill in the lucky number */
sign.showLane = true;
memcpy(sign.laneBackInfo, "laneBackInfo", strlen("laneBackInfo"));
memcpy(sign.laneSelectInfo, "laneSelectInfo", strlen("laneSelectInfo"));
/* Now we are ready to encode the message! */
status = pb_encode(&stream, com_bst_modules_iot_proto_LaneStatus_fields, &sign);
message_length = stream.bytes_written;
aw_kprintf("message_length: %d\n", stream.bytes_written);
/* Then just check for any errors.. */
if (!status)
{
aw_kprintf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
}
/* Now we could transmit the message over network, store it in a file or * wrap it to a pigeon's leg. */
/* But because we are lazy, we will just decode it immediately. */
{
/* Allocate space for the decoded message. */
com_bst_modules_iot_proto_LaneStatus message = com_bst_modules_iot_proto_LaneStatus_init_zero;
/* Create a stream that reads from the buffer. */
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
/* Now we are ready to decode the message. */
status = pb_decode(&stream, com_bst_modules_iot_proto_LaneStatus_fields, &message);
/* Check for errors... */
if (!status)
{
aw_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
/* Print the data contained in the message. */
aw_kprintf("Your showLane was %d!\n", (int)message.showLane);
aw_kprintf("Your laneBackInfo was %s!\n", (int)message.laneBackInfo);
aw_kprintf("Your laneSelectInfo was %s!\n", (int)message.laneSelectInfo);
}
return 0;
}
3. 验证
AWorks for i.MX RT1050, build Mar 1 2022
AWorks SDK Version is 2.1.2 <2020-09-30>
current time: 1970-01-01 01:35:37
Appl|AWorks->>> ication Start.
message_length: 32
Your showLane was 1!
Your laneBackInfo was laneBackInfo!
Your laneSelectInfo was laneSelectInfo!
4. 总结
固定长度解析比动态长度解析简单多了。
边栏推荐
- Judging decimal points and rounding of decimal operations in Golang
- Pytorch lstm time series prediction problem stepping on the pit "recommended collection"
- 信息学奥赛一本通 1941:【07NOIP普及组】Hanoi双塔问题 | 洛谷 P1096 [NOIP2007 普及组] Hanoi 双塔问题
- STM32 full series development firmware installation guide under Arduino framework
- Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
- Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
- MATLAB program design and application 2.4 Common internal functions of MATLAB
- 高效并发:Synchornized的锁优化详解
- 【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
- leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
猜你喜欢

In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved

老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11

Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...

C程序设计-方法与实践(清华大学出版社)习题解析

Financial profitability and solvency indicators

Go mode tidy reports an error go warning “all” matched no packages

idea中搜索具体的字符内容的快捷方式

高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询

The principle of ReentrantLock (to be continued)
![[Intensive reading of the paper] iNeRF](/img/a7/910667911e1ce8996b9d22de63ea04.png)
[Intensive reading of the paper] iNeRF
随机推荐
Introduction to Audio Types and Encoding Formats in Unity
Book of the Month (202207): The Definitive Guide to Swift Programming
Talking about the algorithm security of network security
NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
第六章
全网一触即发,自媒体人的内容分发全能助手——融媒宝
Douyin fetches video list based on keywords API
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
BM5 合并k个已排序的链表
架构实战营模块八作业
Realize serial port receiving data based on STM32 ring queue
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
The principle of ReentrantLock (to be continued)
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
Carbon教程之 基本语法入门大全 (教程)
LevelSequence source code analysis
Istio introduction