嵌入式C语言 protobuf实现 nanopb

文章详细介绍了如何利用Nanopb,一个轻量级的C语言版Protobuf库,进行数据结构的序列化和反序列化。首先,创建protobuf协议文件定义数据结构,然后通过protoc命令生成C代码。接着,将生成的代码和Nanopb库文件加入工程,并编写编码和解码函数,最后展示了在嵌入式环境中对Student信息进行编码和解码的示例。
摘要由CSDN通过智能技术生成

Protobuf是Google公司开发的一种数据格式,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。它不依赖于语言和平台并且可扩展性极强。

nanopb是C语言版本的轻量级的protobuf,适用于资源受限的MCU

Nanopb - downloads 下载最新版本的nanopb

解压后将generator-bin目录所在路径添加进Path环境变量下

创建protobuf协议文件 student.proto

syntax = "proto2";
message CourseScore{
    required string coursename = 1;
    required uint32 score      = 2; 
}
message Student
{
     required string name              = 1;
     required uint32 studentid         = 2;
     optional string phonenum          = 3;
     repeated CourseScore coursescores = 4; 
}

可以指定string的长度,建立student.option文件

Student.name 		max_size:128
Student.phonenum 		max_size:12
CourseScore.coursename  max_size:128

下一步通过命令生成student.pb.h student.pb.c

protoc --nanopb_out=. student.proto

将student.pb.h student.pb.c 及几个解压后的pb文件拷贝进你的工程目录

pb.h
pb_common.c
pb_common.h
pb_decode.c
pb_decode.h
pb_encode.c
pb_encode.h
student.pb.h
student.pb.c

现在编写测试程序

#include <stdio.h>
#include "student.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include <stdint.h>
#define MAX_COURCE_NUM  3

typedef  struct _StuourseScore{
     char coursename[128];
     uint32_t score;
}CourseScoreInfo;

typedef  struct _StudentInfo{
     char name[128];
     uint32_t studentid;
     char phonenum[12];
     CourseScoreInfo coursescores[MAX_COURCE_NUM];
     int realCourseNum;
}StudentInfo;


static bool courseScoreEncode(pb_ostream_t* stream, const pb_field_t* field, void* const* arg){
    StudentInfo *studentInfo=(const StudentInfo *)*arg;
    CourseScore stuCourseScore;
    for(int i=0;i<studentInfo->realCourseNum;i++){
        strcpy(stuCourseScore.coursename,studentInfo->coursescores[i].coursename);
        stuCourseScore.score=studentInfo->coursescores[i].score;
        if (!pb_encode_tag_for_field(stream, field)) {
            return false;
        }
        if (!pb_encode_submessage(stream, CourseScore_fields, &stuCourseScore)) {
            return false;
        }
    }
    return true;
}

static bool courseScoreDecode(pb_istream_t* stream, const pb_field_t* field, void** arg){

    CourseScore courseScore = CourseScore_init_default;
    StudentInfo *studentInfoOut=(const StudentInfo *)*arg;
    if (!pb_decode(stream, CourseScore_fields, &courseScore)) {
        return false;
    }

    strcpy(studentInfoOut->coursescores[studentInfoOut->realCourseNum].coursename,courseScore.coursename);
    studentInfoOut->coursescores[studentInfoOut->realCourseNum].score=courseScore.score;
    studentInfoOut->realCourseNum++;
    return true;
}


int encodeMsg(const Student *pack_stu,uint8_t *buffer,int manxBufferLen){
    pb_ostream_t o_stream = {0};
    o_stream = pb_ostream_from_buffer(buffer, manxBufferLen);
    if(pb_encode(&o_stream, Student_fields, pack_stu)==false){
        printf("encode failed\n");
        return 0;
    }
    return o_stream.bytes_written;
}

int decodeMsg(const uint8_t *buffer,int encodeBufferLen,Student *unpack_stu){
    // 解包
    pb_istream_t i_stream = {0};
    i_stream = pb_istream_from_buffer(buffer, encodeBufferLen);
    if(pb_decode(&i_stream, Student_fields, unpack_stu)==true){
        return 1;
    }
    return 0;
}

// 嵌入式大杂烩
void protobuf_test(void)
{
  StudentInfo studentInfoIn;
  strcpy(studentInfoIn.name,"kaijdtf");
  studentInfoIn.studentid=123456;
  strcpy(studentInfoIn.phonenum,"98765432101");

  strcpy(studentInfoIn.coursescores[0].coursename,"shuxue");
  studentInfoIn.coursescores[0].score=60;
  strcpy(studentInfoIn.coursescores[1].coursename,"yingyu");
  studentInfoIn.coursescores[1].score=70;
  strcpy(studentInfoIn.coursescores[2].coursename,"yuwen");
  studentInfoIn.coursescores[2].score=80;
  studentInfoIn.realCourseNum=3;


  // 组包
  uint8_t encodeBuffer[1024] = {0};
  int     encodeBufferLen=0;
  Student pack_stu = Student_init_zero;
  strcpy(pack_stu.name,studentInfoIn.name);
  pack_stu.studentid=studentInfoIn.studentid;
  pack_stu.has_phonenum=true;
  strcpy(pack_stu.phonenum,studentInfoIn.phonenum);
  pack_stu.coursescores.funcs.encode=courseScoreEncode;
  pack_stu.coursescores.arg=&studentInfoIn;
  if((encodeBufferLen=encodeMsg(&pack_stu,encodeBuffer,1024))<=0){
      return ;
  }

  printf("mgsEncodeLen=%d\n",encodeBufferLen);
  for(int i=0;i<encodeBufferLen;i++)
      printf("%02X ",encodeBuffer[i]);
  printf("\n");



  StudentInfo studentInfoOut;
  memset(&studentInfoOut,0,sizeof(StudentInfo));
  Student unpack_stu =Student_init_zero;
  unpack_stu.coursescores.funcs.decode=courseScoreDecode;
  unpack_stu.coursescores.arg=&studentInfoOut;

  if(decodeMsg(encodeBuffer,encodeBufferLen,&unpack_stu)){
      strcpy(studentInfoOut.name,unpack_stu.name);
      studentInfoOut.studentid=unpack_stu.studentid;
      if(unpack_stu.has_phonenum)
            strcpy(studentInfoOut.phonenum,unpack_stu.phonenum);

      printf("studentInfoOut.name = %s\n", studentInfoOut.name);
      printf("studentInfoOut.studentid = %u\n", studentInfoOut.studentid);
      printf("studentInfoOut.phonenum = %s\n", studentInfoOut.phonenum);
      if(studentInfoOut.realCourseNum>0){
          for(int i=0;i<studentInfoOut.realCourseNum;i++)
              printf("cource name:%s score:%u\n",studentInfoOut.coursescores[i].coursename,studentInfoOut.coursescores[i].score);

      }

  }
}

int main()
{

    protobuf_test();
    return 0;
}

打印输出:

mgsEncodeLen=61
0A 07 6B 61 69 6A 64 74 66 10 C0 C4 07 1A 0B 39 38 37 36 35 34 33 32 31 30 31 22 0A 0A 06 73 68 75 78 75 65 10 3C 22 0A 0A 06 79 69 6E 67 79 75 10 46 22 09 0A 05 79 75 77 65 6E 10 50 
studentInfoOut.name = kaijdtf
studentInfoOut.studentid = 123456
studentInfoOut.phonenum = 98765432101
cource name:shuxue score:60
cource name:yingyu score:70
cource name:yuwen score:80

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值