0

I'm using arduino for encoding the massage, i have tried for required and success for encoding and decoding back, but for repeated, after i encode it, the size of buffer is 0, so i cant send my buffer to other arduino

here is my code

file.ino

  {

   for(int i=0;i<7;i++)
    message.header[i]=i+1;

//this is my variabel, i declare in .proto = repeated int32 header = 4  [(nanopb).max_count = 10, (nanopb).fixed_length = true];

  stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

  bool status = pb_encode(&stream, Message_fields, &message);

 Serial.println(stream.bytes_written); 

//when i print this after encode, the data is loss, but when the field type is required, it will show some data bytes

}
Nick
  • 138,499
  • 22
  • 57
  • 95
Estu
  • 3
  • 1

1 Answers1

0

Your header variable is fixed-length array of 10 entries. That should be ok. If it was not a fixed-length one there would be separate header_count field that you would have to set to the actual number of entries. You can look inside generated .pb.h to double-check that there is no header_count field.

Your code does not show the length of buffer you have allocated. Is it perhaps too short? Though that message should take only about 14 bytes.

You could also check whether status is true, i.e. whether encoding was successful. If it was not, you can find more information from stream.errmsg.

jpa
  • 10,351
  • 1
  • 28
  • 45
  • thanks, i have solve it by setting my header_ count variable to 10. and what do you mean with separare header_count if the fixedlength is false? and i want ask one question, can i make array if i declare the header variable without [(nanopb).max_count = 10, (nanopb).fixed_length = true]? the generate code give me pb_callback_t and i dont understad how to use it – Estu Aug 22 '19 at 07:17
  • @Estu, you could have just max_count. Without that, it will give you a function pointer, and then you could write a callback function that generates arbitrary amount of entries. – jpa Aug 22 '19 at 08:19
  • okey, thank you very much. do you know how to start studying that callback function? because im new on it – Estu Aug 22 '19 at 16:17
  • oh i forgot, if the array in nanopb has fixed length and max count, can it communicate with protobuffer from google – Estu Aug 23 '19 at 03:03