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
#include "student.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include
#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;irealCourseNum;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;i0){
for(int i=0;i
打印输出:
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