简体   繁体   English

如何将数据从protobuf嵌入式c传输到Google protobuf C ++ / Java库?

[英]How to transfer data from protobuf-embedded-c to Google protobuf C++/Java library?

I am using protobuf-embedded-c on a small system to transfer different data from the it to a PC. 我在小型系统上使用protobuf-embedded-c将不同的数据从它传输到PC。 The problem is, that if I use the embedded library on both ends everything works. 问题是,如果我在两端都使用嵌入式库,则一切正常。 If i use the google C++ on the PC it doesn't work anymore. 如果我在PC上使用google C ++,它将无法再使用。 I think i traced the problem to the embedded library having length prefixes in every message, but i cannot seem to do this in a good way on the C++ library. 我认为我已将问题追溯到每条消息中都有长度前缀的嵌入式库,但是我似乎无法在C ++库中很好地做到这一点。 Here is the test application which i used to debug this: 这是我用来调试此应用程序的测试应用程序:

person.proto: person.proto:

enum PhoneType {
  MOBILE = 0;
  HOME = 1;
  WORK = 2;
}

message PhoneNumber {
  required float number = 1;
  required PhoneType type = 2;
}

main.cpp: main.cpp:

#include <stdint.h>
#include <stdio.h>

#define __PROTOBUF_CPP_IMPL

#ifdef __PROTOBUF_CPP_IMPL
#include "person.pb.h"
#else
    extern "C"{
        #include "person.h"
    }
#endif

int main(int argc, char *argv[])
{
    static const uint32_t outputbuflen = 1024;
    uint8_t outputbuffer[outputbuflen];
    uint32_t writtenlenght = 0;

#ifdef __PROTOBUF_CPP_IMPL

    // C++ implementation.
    printf("Google C++ implementation;\n");
    PhoneNumber number;
    number.set_number(0800123123.0);
    number.set_type(MOBILE);

    writtenlenght = number.ByteSize();
    numberSerializeToArray(outputbuffer, writtenlenght);

#else

    // Embedded c implementation.
    printf("embedded-c implementation:\n");
    PhoneNumber number;
    number._number = 0800123123.0;
    number._type = _MOBILE;

    writtenlenght = PhoneNumber_write_delimited_to(&number, outputbuffer, 0);

#endif

    for(uint32_t i = 0; i < writtenlenght; i++){
        printf("%.2X ", outputbuffer[i]);
    }
    printf("\n");
    return 0;
}

The real question: What methods should be used in the Google protobuf C++/Java library to communicate succesfully with the embedded library? 真正的问题:Google protobuf C ++ / Java库中应使用哪些方法与嵌入式库成功通信? The simple answer would probably be to add (read when parsing) the prefix to all messages, but this breaks the code. 简单的答案可能是在所有消息中添加(解析时读取)前缀,但这会破坏代码。 Should I just look for a better embedded library? 我应该只是寻找更好的嵌入式库吗?

Update: I tried this great little library called nanopb and it works great for now. 更新:我尝试了一个名为nanopb的小型库,它现在运行良好。 Bottom line: protobuf-embedded-c in NOT compatible with the google protobuf implementation! 底线:protobuf-embedded-c与Google protobuf实现不兼容!

Protobuf比较。

Give offset as -1 instead of 0. For example: 将偏移量设置为-1而不是0。例如:

writtenlenght = PhoneNumber_write_delimited_to(&number, outputbuffer, -1);

or you can just use PhoneNumber_write(&number, outputbuffer, 0); 或者您可以只使用PhoneNumber_write(&number, outputbuffer, 0);

In protobuf-embedded-c's generated C file , there exists 在protobuf-embedded-c的生成的C文件中 ,存在

int $name$_read(void *_buffer, $if(empty)$$else$struct $name$ *_$name$, $endif$int offset, int limit);
int $name$_write($if(empty)$$else$struct $name$ *_$name$, $endif$void *_buffer, int offset);

which appear to do the expected, reading/writing a message without a size prefix. 看起来像预期的那样,读/写没有大小前缀的消息。 I don't know why these aren't exposed in the generated H file , but you should be able to add it yourself. 我不知道为什么这些没有显示在生成的H文件中 ,但是您应该可以自己添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM