Protocol Buffer nano在Mbed OS中的使用

     Protocol Buffer 是google 公司开发的结构化数据序列化/反序列化方法。它比json和XML 效率更高。我们在ModularIoT 中使用Protocol buffer 实现消息体的编解码。当然也希望在微处理器cortex-M 的微服务器中也可以使用protobuf的编解码。网络上看见一个Protocol Buffer Nano 的项目。而且在Mbed OS 社区也发现了相关的项目。

ubuntu上安装 protoc-c 工具

apt-get install protobuf-c-compiler

编写proto 文件

syntax = "proto3";
package websocket;

message WebsocketMessage {
    string Topic =1;
    bytes  Body=2;
 }
message GenericRPC {
    string Method =1;
	string To=2;
	string From =3;
	int32 Code=4;
    bytes  parameters=5;
 }

下载包 nanopb 包

nanopb 并不是标准的protobuf 所以要下载专用的程序包 nanopb

git clone https://github.com/nanopb/nanopb.git

protobuf nano 要使用特殊的转换器,当你下在了 protobufnano 包后,还需要安装

安装python-protobuf

 sudo apt-get update

sudo apt-get install python-protobuf

转换过程

protoc –osimple.pb simple.proto

python  ../.. /generator/nanopb-generator.py simple.bp

遇到的问题

在这过程中,发现protoc 的版本和 protobuf-python 的版本不一致。解决的方法很简单,使用下面的命令:

sudo pip install –U protobuf

完美地解决了问题

protobuf nano 的使用

Mbed OS 的社区中有一个nanopb-test 程序。我们对它进行的部分的修改。

#include "mbed.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include "threeaxis.pb.h"
Serial pc(USBTX, USBRX);
int main() {
    pc.baud(115200);
    gyro_message GyroOut, GyroIn;
    
    uint8_t bufferout[150];
    uint8_t bufferin[150];
    
    GyroOut.X=1.1;
    GyroOut.Y=2.1;
    GyroOut.Z=3.1;
    pc.printf("starting..\r\n");
    while(1){
        GyroOut.X+=0.1;
        GyroOut.Y+=0.2;
        GyroOut.Z+=0.3;
        
        pc.printf("Raw values: x: %4.2f, y: %4.2f, z: %4.2f\r\n", GyroOut.X, GyroOut.Y, GyroOut.Z); //print values before encoding
         pb_ostream_t streamout = pb_ostream_from_buffer(bufferout, sizeof(bufferout));
        if (pb_encode(&streamout, gyro_message_fields, &GyroOut)) { //encode message
            pc.printf("encoded\n");
        }
        
        else { //print error message if encoding fails
            pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&streamout));
            return 0;
        }
        pc.printf("protopb length =%d\n",streamout.bytes_written);
          for(int i=0;i<=150;i++) //copy output buffer to input buffer
            bufferin[i]=bufferout[i];
        pc.printf("decoding...\r\n");
        pb_istream_t streamin = pb_istream_from_buffer(bufferin, sizeof(bufferin)); //create input stream
      
        if (pb_decode(&streamin, gyro_message_fields, &GyroIn)) { //decode message
            pc.printf("Decoded values: x: %4.2f, y: %4.2f, z: %4.2f\r\n", GyroIn.X, GyroIn.Y, GyroIn.Z); //print decoded values
        }
        
        else { //print error message if decoding fails
            pc.printf("Decoding failed: %s\n", PB_GET_ERROR(&streamin));
            return 0;
        }
        wait(2);
    }
}

遇到了新的问题

1  使用上面提及的方法传送到mbed 的项目中,发现版本不同,无法编译

  于是将nanopb 包中的代码传送到mbed 项目中。它们是 

 编译通过了

2 如果proto 文件中的结构造含有字符串,那么要使用 和proto 文件名一致的options 文件来规定字符串的最大长度。但是不知道 nanopb_generator.py  的options 如何标志,好在还有一个方法。就是在proto 文件中标志nano 的长度

syntax = "proto3";
package websocket;
import "nanopb.proto";
message WebsocketMessage {
    string Topic =1 [(nanopb).max_size = 32];
    bytes  Body=2 [(nanopb).max_size = 256];
 }
message GenericRPC {
    string Method =1 [(nanopb).max_size = 32];
	string To=2 [(nanopb).max_size = 32];
	string From =3 [(nanopb).max_size = 32];
	int32 Code=4;
    bytes  Parameters=5 [(nanopb).max_size = 240];
 }

注意:import nanopb.proto, nanopb.proto 文件在nanobp 包中 nanopb/generator/proto 中,可以直接将这个文件Copy 到你的当前目录中。

这样便成功了

我的例中,这个例子实现了protobuf 的分层嵌套。GenericRPC 嵌入在WebsocketMessage 中,要分两步编解码。感兴趣的就慢慢看吧,我是调通了的。

#include "mbed.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include "websocket.pb.h"
Serial pc(USBTX, USBRX);
int main() {
    pc.baud(115200);
    websocket_WebsocketMessage websocketMessage,websocketMessage_in;
    websocket_GenericRPC genericRPC,genericRPC_in;
  
    uint8_t bufferout[512];
    uint8_t bufferin[512];
    uint8_t pbuf[4];
    pbuf[0]=0x00;
    pbuf[1]=0x01;
    strcpy(genericRPC.Method,"modbus.query");
    strcpy(genericRPC.From,"microserver2");
    strcpy(genericRPC.To,"weatherstation2");
    genericRPC.Code=0;
    genericRPC.Parameters.size=4;
    memcpy(&genericRPC.Parameters.bytes,pbuf ,4);
   
    pc.printf("starting encode..\r\n");
    while(1){
        //encode genericRPC 
         pb_ostream_t streamout = pb_ostream_from_buffer(bufferout, sizeof(bufferout));
        if (pb_encode(&streamout, websocket_GenericRPC_fields, &genericRPC)) { //encode message
            pc.printf("GenericRPCencoded\n");
        }
        
        else { //print error message if encoding fails
            pc.printf("Encoding GenericRPC failed: %s\n", PB_GET_ERROR(&streamout));
            return 0;
        }
        pc.printf("genericRPC length =%d\n",streamout.bytes_written);
         strcpy(websocketMessage.Topic,"weatherstation2.RPC");
         websocketMessage.Body.size=streamout.bytes_written;
         memcpy(&websocketMessage.Body.bytes,bufferout ,streamout.bytes_written);
         // encode websocketMessage
         streamout = pb_ostream_from_buffer(bufferout, sizeof(bufferout));
          if (pb_encode(&streamout, websocket_WebsocketMessage_fields, &websocketMessage)) { //encode message
            pc.printf("WebsocketMessage\n");
        } else { //print error message if encoding fails
            pc.printf("Encoding WebMessage failed: %s\n", PB_GET_ERROR(&streamout));
            return 0;
        }
         pc.printf("websocketMassage length =%d\n",streamout.bytes_written);
        //  for(int i=0;i<streamout.bytes_written;i++) //copy output buffer to input buffer
        //    bufferin[i]=bufferout[i];
        memcpy(bufferin,bufferout,streamout.bytes_written);
        pc.printf("decoding...\r\n");
        pb_istream_t streamin = pb_istream_from_buffer(bufferin, streamout.bytes_written); //create input stream
      
        if (pb_decode(&streamin, websocket_WebsocketMessage_fields, &websocketMessage_in)) { //decode message
           pc.printf("Decoded websockerMessage ,Topic:%s\n",websocketMessage_in.Topic);
           int bodylength=websocketMessage_in.Body.size;
           memcpy(bufferin,websocketMessage_in.Body.bytes,bodylength);
           streamin = pb_istream_from_buffer(bufferin, bodylength); 
            if (pb_decode(&streamin, websocket_GenericRPC_fields, &genericRPC_in)) {
                  pc.printf("Decoded Method:%s,From:%s,To:%s\n",genericRPC_in.Method,genericRPC_in.From,genericRPC_in.To  ); //print decoded values
                } else {
                     pc.printf("Decoding GenericRPC failed: %s\n", PB_GET_ERROR(&streamin));
                    return 0;
                    }
        }      
        else { //print error message if decoding fails
            pc.printf("Decoding WebsocketMessage failed: %s\n", PB_GET_ERROR(&streamin));
            return 0;
        }
        wait(2);
    }
}

在STM32 的USB 串口显示的内容为

 

当你搞不定时,哪怕是网路上的垃圾,对你也许就是救命稻草。但愿对你有所帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值