簡體   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