简体   繁体   English

如何使用 Nanopb 将 subMsg 添加到重复的 msg?

[英]How to add subMsg to msg repeated using Nanopb?

I'm simply trying to add one message to another message (up to 60 times times)我只是想将一条消息添加到另一条消息中(最多 60 次)

My .proto file looks as follows;我的 .proto 文件如下所示;

syntax = "proto3";

message FeatureFile {    
    string fileName= 2;
    string Id= 3;
    repeated Feature features = 1;
}

message Feature {
    int32 version = 1;
    int32 epochTime = 2;
    int32 noOfObs= 3;
    int32 frequency = 4;
}

I have tried to make a callback function to add repeated data, but cannot make it work.我试图制作一个回调函数来添加重复的数据,但无法使其工作。

bool encode_string(pb_ostream_t* stream, const pb_field_t* field, void* const* arg)
{
    const char* str = (const char*)(*arg);

    if (!pb_encode_tag_for_field(stream, field))
        return false;

    return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}

bool encode_repeatedMsg(pb_ostream_t* stream, const pb_field_t* field, void* const* arg)
{
    const char* obj = (const char*)(*arg);
    int i;

    for (i = 0; i < 60; i++)
    {
        if (!pb_encode_tag_for_field(stream, field))
            return false;

        if (!pb_encode_submessage(stream, Feature_fields, *arg))
            return false;
    }
    return true;
}

int main()
{
    FeatureFile featurefile = FeatureFile_init_zero;

    Feature feature = Feature_init_zero;

    featurefile.fileName.arg = "092536.csv";
    featurefile.fileName.funcs.encode = &encode_string;
    featurefile.Id.arg = "";
    featurefile.Id.funcs.encode = &encode_string;
    feature.version = 1;
    feature.epochTime = 12566232;
    feature.noOfObs = 260;
    feature.frequency = 200;

    featurefile.features.funcs.encode = &encode_repeatedMsg;

I thought I could call the repeated encoding like the last line of code shows, but I doesn't allow me.我以为我可以像最后一行代码所示那样调用重复编码,但我不允许。

The callback itself is supposed to add 60 of the same messages (feature) to the the featurefile.回调本身应该将 60 条相同的消息(功能)添加到功能文件中。

Can anyone help me here?有人能帮我一下吗?

I myself have never used the callbacks in nanopb.我自己从未使用过 nanopb 中的回调。 I do have been using the .options file to statically allocate the desired array size.我确实一直在使用 .options 文件来静态分配所需的数组大小。 Your case this might be a bit much as your require 60 messages but this is how you do it:您的情况可能有点像您需要 60 条消息,但这就是您的做法:

You create a file with the same name as your .proto file but give it the extension .options.您创建一个与 .proto 文件同名的文件,但给它扩展名 .options。 You place it in the same folder as your .proto file.你把它和你的 .proto 文件放在同一个文件夹中。 In that file you mention there repeated variable name and assign it a size:在该文件中,您提到了重复的变量名称并为其分配了大小:

# XXXX.options
FeatureFile.features max_count:16

More information on the nanopb options can be found here .可以在此处找到有关 nanopb 选项的更多信息。

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

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