基于RT1052 Aworks nanopb string 类型固定长度使用方式(二十七)

本文主要是通过迁移的思维,记录本人初次使用周立功的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. 总结

固定长度解析比动态长度解析简单多了。

欢迎订阅
嵌入式实操”一个分享开发实践经验的地方。
文章会同时发布到我的 CSDN主页今日头条号 平台上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式实操

希望博文有助于您,您不必加班。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值