繁体   English   中英

在nanopb中解码重复的子消息

[英]Decoding repeated Submessage in nanopb

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

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

鉴于此 proto 消息和选项文件

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;
}
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 文件:

{
    "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脚本并将其序列化为protobuf(为了简洁而被截断)

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函数

// 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
}

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

我不完全理解为什么流中没有足够的数据。 我相信消息被正确编码,因为我可以在 python 中反序列化它。 我怀疑有一个标志或一些需要设置的选项,所以我可以正确地拥有一个重复的子消息,其中包含重复的字段。

我发现了我的问题。 我不知道在代码库的其他地方设置的最大缓冲区大小将缓冲区大小限制为 64,并且我的一些消息长度超过 117 个字符。 因此它们无法被解码。

暂无
暂无

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

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