简体   繁体   English

在 Nanopb 的“重复”数据中使用“重复”

[英]Using 'repeated' inside 'repeated' data in Nanopb

How to properly encode data with NanoPB when having several nested 'repeated' fields?具有多个嵌套的“重复”字段时,如何使用 NanoPB 正确编码数据?

This is my schema:这是我的架构:

message Report {

  message SensorData {
     required uint32 sensorid = 1;
     required uint32 sample = 2;
  }

  message DeviceData {
    required uint32 devid = 1;
    repeated SensorData sensor_data = 2;
  }

  required uint32 reportnum = 1;
  repeated DeviceData dev_data = 2;

}

I have already made a working version in which SensorData fields are embedded inside DeviceData message based on the server.c example from the NanoPB source.我已经制作了一个工作版本,其中基于 NanoPB 源中的 server.c 示例将SensorData字段嵌入到DeviceData消息中。 This way I have only one repeated field and everything works fine.这样我只有一个重复的字段,一切正常。 However this way I have to repeat the 'devid' field for every sensorid and every 'sample', instead of giving it just one time and then loop through an array of SensorData messages.但是,通过这种方式,我必须为每个sensorid和每个“样本”重复“devid”字段,而不是只给它一次,然后循环遍历一系列SensorData消息。 However I am struggling to encode this with NanoPB, the decoding part is in Python.但是我正在努力用 NanoPB 对其进行编码,解码部分是在 Python 中。 Can someone give me an example how to properly encode data in this case?有人能给我一个例子,如何在这种情况下正确编码数据?

For me the simplest way to do this was by statically defining the size of the array's using a nanopb options file .对我来说,最简单的方法是使用nanopb 选项文件静态定义数组的大小。 After that you can access each element just like an array.之后,您可以像访问数组一样访问每个元素。

report.dev_data[i].devid[j] = 1234;
report.dev_data[i].sensor_data[j] = 9876;

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

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