Linux Mint下Nanopb的简单使用
1、下载Protocbuf,我使用的是Protoc2.5.0$ wget https://github.com/google/protobuf/archive/v2.5.0.zip2、解压$ unzip protobuf-2.5.0.zip3、安装
·
一、安装依赖包
$sudo apt-get install build-essential
二、安装Protobuf
1、下载Protobuf,我使用的是Protobuf2.6.1
$ wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
2、解压
$ sudo chmod 777 protobuf-2.6.1.tar.gz
$ tar -vxzf protobuf-2.6.1.tar.gz
3、安装
$ cd protobuf-2.6.1/
$ ./configure
$ make
$ sudo make install
4、添加环境变量
vim ~/.profile
加入:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
保存后退出,编译
source ~/.profile
5、测试
protoc --version
三、安装nanopb
1、下载
wget http://koti.kapsi.fi/~jpa/nanopb/download/nanopb-0.3.5-linux-x86.tar.gz
2、解压
$ tar -zxvf nanopb-0.3.5-linux-x86.tar.gz
3、复制
$ sudo cp nanopb-0.3.5-linux-x86 /opt/
四、生成文件
1、制作proto文件,Message.proto
message Person{
required int32 age = 1;
required string name = 2;
}
2、制作option文件,Message.options
Person.name max_size:16
3、生成C语言文件
$ /opt/nanopb-0.3.5-linux-x86/generator-bin/protoc --nanopb_out=. Message.proto
4、生成java语言文件
protoc --java_out=. Message.proto
五、测试代码
1、将解压的nanopb-0.3.5-linux-86文件夹下的pb.h, pb_common.c, pb_common.h, pb_decode.c, pb_decode.h, pb_encode.c, pb_encode.h复制到proto文件目录下。
2、新建main.c,内容如下:
#include "Message.pb.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
/* This is the buffer where we will store our message. */
uint8_t buffer[128];
size_t message_length;
bool status;
uint8_t i;
/* 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.
*/
Person person = Person_init_zero;
memset(buffer, 0, 128);
/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
person.age = 20;
strcpy(&person.name, "zhujun");
/* Now we are ready to encode the message! */
status = pb_encode(&stream, Person_fields, &person);
message_length = stream.bytes_written;
printf("encode ok\r\n");
for (i = 0; i < message_length; i++)
{
printf("%d,", buffer[i]);
}
printf("\r\n");
/* Then just check for any errors.. */
if (!status)
{
printf("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. */
Person person = Person_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, Person_fields, &person);
/* Check for errors... */
if (!status)
{
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return -1;
}
/* Print the data contained in the message. */
printf("age was [%d].\n", person.age);
printf("name was [%s].\n", person.name);
}
return 0;
}
六、问题
1、C在使用string类型的时候,必须通过options,限定string的长度,否则运行将出错。
2、nanopb可通过python生成.c和.h文件,但我没有成功,不知道缺少了哪个库。
更多推荐
已为社区贡献2条内容
所有评论(0)