nanopb 的安装与使用
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash

·
下载与安装:
wget https://koti.kapsi.fi/~jpa/nanopb/download/nanopb-0.3.7-linux-x86.tar.gz
tar -xvf nanopb-0.3.7-linux-x86.tar.gz
sudo cp nanopb-0.3.7-linux-x86/ /opt/ -r
使用方式:
- 定义协议Message.proto
syntax = "proto2";
message Loginrequst{
required string username = 1;
required string password = 2;
}
message Loginrepone{
required int32 status = 1;
required int32 errno = 2;
}
- options文件
Loginrequst.username max_size:16
Loginrequst.password max_size:16
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
- 使用nanopb 生成c 代码
$ls
Message.options Message.proto
#命令使用方式
$/opt/nanopb-0.3.7-linux-x86/generator-bin/protoc --nanopb_out=. Message.proto
#查看生成文件
$ls
Message.options Message.pb.c Message.pb.h Message.proto
#查看工具链文件
$ls /opt/nanopb-0.3.7-linux-x86/pb*
/opt/nanopb-0.3.7-linux-x86/pb_common.c /opt/nanopb-0.3.7-linux-x86/pb_decode.c /opt/nanopb-0.3.7-linux-x86/pb_encode.c /opt/nanopb-0.3.7-linux-x86/pb.h
/opt/nanopb-0.3.7-linux-x86/pb_common.h /opt/nanopb-0.3.7-linux-x86/pb_decode.h /opt/nanopb-0.3.7-linux-x86/pb_encode.h
#将这些文件与Message.pb.c Message.pb.h copy到我们的项目中即可,编译时无需链接任何库
- 编码使用
#include <Message.pb.h>
#include <pb.h>
#include <pb_encode.h>
#include <pb_decode.h>
int test_nanopb(void)
{
/* 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.
*/
Loginrequst login= Loginrequst_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));
strcpy((char *)&login.username, "devin");
strcpy((char *)&login.password,"123456");
/* Now we are ready to encode the message! */
status = pb_encode(&stream, Loginrequst_fields, &login);
message_length = stream.bytes_written;
/* 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. */
Loginrequst login = Loginrequst_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, Loginrequst_fields, &login);
/* Check for errors... */
if (!status)
{
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return -1;
}
/* Print the data contained in the message. */
printf("username was [%s].\n", login.username);
printf("password was [%s].\n", login.password);
}
return 0;
}
int main(int argc,char *argv[])
{
test_nanopb();
return 0;
}
- 执行结果
username was [devin].
password was [123456].
推荐内容
阅读全文
AI总结




A beautiful web dashboard for Linux
最近提交(Master分支:7 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
相关推荐
查看更多
linux-dash

A beautiful web dashboard for Linux
linux-dash

A beautiful web dashboard for Linux
linux-dash

热门开源项目
活动日历
查看更多
直播时间 2025-04-23 19:00:00

GitTalk:国内首个微服务编排框架Juggle实战解析
直播时间 2025-04-22 18:31:56

字节AI 黑科技!从 Manus Agent 入门 Eino
直播时间 2025-04-09 14:34:18

樱花限定季|G-Star校园行&华中师范大学专场
直播时间 2025-04-07 14:51:20

樱花限定季|G-Star校园行&华中农业大学专场
直播时间 2025-03-26 14:30:09

开源工业物联实战!
所有评论(0)