简体   繁体   中英

Using 'repeated' inside 'repeated' data in Nanopb

How to properly encode data with NanoPB when having several nested 'repeated' fields?

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. 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. However I am struggling to encode this with NanoPB, the decoding part is in 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 . 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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