首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nanopb中重复子消息的解码

nanopb中重复子消息的解码
EN

Stack Overflow用户
提问于 2021-10-19 18:38:32
回答 1查看 58关注 0票数 1

我看了很多其他的帖子,但似乎没有一个能解决我的问题。

我在C中使用nanoPB,在json中创建消息,并将它们转换为pb发送到我的设备。

给定此proto消息和选项文件

代码语言:javascript
复制
message PB_BoundingArray {
    enum Shape {
        INVALID = 0;
        CIRCLE = 1;
        UNUSED = 2;
        TRIANGLE = 3;
        QUADRANGLE = 4;
    }
    Shape type = 1;
    repeated float point_x = 2;
    repeated float point_y = 3;
}

message PB_ConfigurationData {
    uint32 min = 1;
    uint32 max = 2;
    PB_BoundingArray bounds = 3;
}
代码语言:javascript
复制
PB_BoundingArray.point_x max_count:4
PB_BoundingArray.point_x fixed_count:true
PB_BoundingArray.point_y max_count:4
PB_BoundingArray.point_y fixed_count:true
PB_ConfigurationData.bounds max_count:10
PB_ConfigurationData.bounds fixed_count:true

我将创建一个json文件,如下所示:

代码语言:javascript
复制
{
    "bounds":
    [
        {
            "point_x":
            [
                39.845129,
                39.840504,
                39.840420,
                39.845119
            ],
            "point_y":
            [
                -102.126389,
                -102.126111,
                -102.118611,
                -102.118611
            ],
            "type": "QUADRANGLE"
        }
    ],
    "max": 90000,
    "min": 10800
}

并使用python脚本并将其序列化为协议for (为berevity而截断)

代码语言:javascript
复制
from lib.length_delimited_protobuf import serialize, deserialize
...
elif args.json_file:
    with open(args.json_file, "r") as input_file:
        message = Parse(input_file.read(), message_type)
        sys.stdout.buffer.write(serialize(message))
...

标准输出被重定向到一个文件,因此它被保存。如果我将该文件提供给C函数

代码语言:javascript
复制
// where buffer is char buffer from a raw read of the .pb file
pb_istream_t stream = pb_istream_from_buffer(buffer, bytesRead);
if(!pb_decode_delimited(&stream, PB_ConfigurationData_fields, &current))
{
    // error handling
}

如果在pb_decode_delimited文件中使用错误消息parent stream too short填充了bounds,则.pb调用将失败。

我不完全理解为什么流中没有足够的数据。我相信消息是正确编码的,因为我可以用python对其进行反序列化。我怀疑有一个标志或某个选项需要设置,这样我才能正确地拥有包含重复字段的重复子消息。

EN

回答 1

Stack Overflow用户

发布于 2021-10-20 14:45:47

我找到我的问题了。我不知道代码库中其他地方设置的最大缓冲区大小将缓冲区大小限制为64,并且我的一些消息是117+字符长度。因此它们不能被解码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69636128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档