【发布时间】:2021-10-19 18:38:31
【问题描述】:
我浏览了许多其他帖子,但似乎没有一个可以解决我的问题。
我在 C 中使用 nanoPB,在 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
}
如果bounds 填充到.pb 文件中并带有错误消息parent stream too short,则pb_decode_delimited 调用失败。
我不完全明白为什么流中没有足够的数据。我相信消息被正确编码,因为我可以在 python 中反序列化它。我怀疑需要设置一个标志或某些选项,以便我可以正确地拥有一个重复的子消息,其中包含重复的字段。
【问题讨论】:
-
尝试在C端验证
bytesRead的值和buffer的内容。然后你可以使用例如protogen.marcgravell.com/decode 检查它是否有效。如果您使用的是 Windows PC,还要验证流是否设置为二进制模式。 -
您能否详细说明如何将数据从 python 传输到嵌入式目标?
-
@jpa 我可以输出缓冲区,但是当我在您链接的站点上按解码时,没有任何反应。由于没有输出,我不确定是否有错误解码。
-
@Bart 现在我正在做单元测试,所以我传入了一个本地 .pb 文件。
-
您在什么平台上运行单元测试?您还可以在问题中包含数据的十六进制转储。
标签: c protocol-buffers nanopb