繁体   English   中英

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

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

我只是想将一条消息添加到另一条消息中(最多 60 次)

我的 .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;
}

我试图制作一个回调函数来添加重复的数据,但无法使其工作。

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;

我以为我可以像最后一行代码所示那样调用重复编码,但我不允许。

回调本身应该将 60 条相同的消息(功能)添加到功能文件中。

有人能帮我一下吗?

我自己从未使用过 nanopb 中的回调。 我确实一直在使用 .options 文件来静态分配所需的数组大小。 您的情况可能有点像您需要 60 条消息,但这就是您的做法:

您创建一个与 .proto 文件同名的文件,但给它扩展名 .options。 你把它和你的 .proto 文件放在同一个文件夹中。 在该文件中,您提到了重复的变量名称并为其分配了大小:

# XXXX.options
FeatureFile.features max_count:16

可以在此处找到有关 nanopb 选项的更多信息。

暂无
暂无

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

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