我浏览了许多其他帖子,但似乎没有一个可以解决我的问题。
我nanoPB
在 C 中使用,在 json 中创建消息并将它们转换pb
为发送到我的设备。
鉴于此原始消息和选项文件
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, ¤t))
{
// error handling
}
如果在文件中填充了错误消息,则pb_decode_delimited
调用失败。bounds
.pb
parent stream too short
我不完全理解为什么流中没有足够的数据。我相信消息被正确编码,因为我可以在 python 中反序列化它。我的怀疑是需要设置一个标志或某些选项,以便我可以正确地拥有一个重复的子消息,其中包含重复的字段。