我看了很多其他的帖子,但似乎没有一个能解决我的问题。
我在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脚本并将其序列化为协议for (为berevity而截断)
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文件中使用错误消息parent stream too short填充了bounds,则.pb调用将失败。
我不完全理解为什么流中没有足够的数据。我相信消息是正确编码的,因为我可以用python对其进行反序列化。我怀疑有一个标志或某个选项需要设置,这样我才能正确地拥有包含重复字段的重复子消息。
发布于 2021-10-20 14:45:47
我找到我的问题了。我不知道代码库中其他地方设置的最大缓冲区大小将缓冲区大小限制为64,并且我的一些消息是117+字符长度。因此它们不能被解码。
https://stackoverflow.com/questions/69636128
复制相似问题