Just making sure

26 views
Skip to first unread message

Prince Montano

unread,
Oct 9, 2023, 1:45:24 AM10/9/23
to nanopb
Good day!

For my project, I aim to serialise/deserialise my protobuf messages from the host computer using google-protobuf and deserialse/serialise the protobuf bytes using nanopb. I'm pretty sure my code has bugs still,  but here's what I've got

while (!end_receive){
if (rx_flag == 1) {
rx_flag = 0;
// Copy to the buffer so we don't accidentally read it when it changes or write to it
memcpy(buf_copy, UserRxBufferHS, split_size);

// Check for the header - if the header is present then do the necessary processing
if (buf_copy[0] == (char)0xE2 && buf_copy[1] == (char)0x98 && buf_copy[2] == (char)0xA2) {

uint8_t high_byte = buf_copy[3];
uint8_t low_byte = buf_copy[4];
uint16_t pbmsg_length = static_cast<uint16_t>(high_byte << 8) | static_cast<uint16_t>(low_byte);

// Append the remaining 512 - 5 bytes to the protobuf_bytes then update pbmsg_length
pb_bytes.insert(pb_bytes.end(), &(buf_copy[5]), &(buf_copy[split_size - 1]));

pbmsg_length = pbmsg_length - pb_bytes.size();
}

// Don't stop appending to the pb_bytes vector until out pbmsg_length becomes 0.
// If the received length is greater than 2 * 512 it will go to the code block below
else if (pbmsg_length > split_size) {
pb_bytes.insert(pb_bytes.end(), &(buf_copy[0]), &(buf_copy[split_size-1]));
pbmsg_length -= split_size;
}

// Going into this block means that pbmsg_length is less than the split size which means that
// this is the last split that we need to process
else {
pb_bytes.insert(pb_bytes.end(), &(buf_copy[0]), &(buf_copy[pbmsg_length]));
end_receive = true;
pbmsg_length = 0;

}
}
}

// Decoding time
pb_istream_t input_stream = pb_istream_from_buffer(reinterpret_cast<const pb_byte_t*>(pb_bytes.data()), pb_bytes.size());
message_actuation_ServoTargets fake_st = message_actuation_ServoTargets_init_zero;

decoded = pb_decode(&input_stream, message_actuation_ServoTargets_fields, &fake_st);
if (!decoded) {
HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_RESET);
HAL_Delay(500);

HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_RESET);
HAL_Delay(500);

HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_RESET);
HAL_Delay(500);
}

else {
HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_SET);
HAL_Delay(5000);
HAL_GPIO_WritePin(BUZZER_SIG_GPIO_Port, BUZZER_SIG_Pin, GPIO_PIN_RESET);
}

I've confirmed that the packet lengths check out but I have not checked everything yet. As expected, things dont work out the first time and I heard my buzzer ring 3 times. Just before I seriously go into debugging, I researched nanopb and I was getting mixed answers from the internet saying that google-protobuf / nanopb serialisation/deserialisation is not going to work, while others were saying that both frameworks release outputs that are byte to byte equivalent so it is going to work, so I was just a bit confused. Is my plan to use google-protobuf for the host and nanopb for the device in the right track? I use USB communications via /dev/ttyACM0 for my purposes, using an STM32 microcontroller.

Thank you very much in advance and please accept my apologies, I have just recently started my embedded systems journey.

Prince

Petteri Aimonen

unread,
Oct 9, 2023, 2:46:41 AM10/9/23
to nan...@googlegroups.com
Hi,

> Just before I seriously go into debugging, I researched nanopb and I
> was getting mixed answers from the internet saying that
> google-protobuf / nanopb serialisation/deserialisation is not going to
> work, while others were saying that both frameworks release outputs
> that are byte to byte equivalent so it is going to work

All protobuf libraries are compatible, that is the whole point of the format.
Nanopb test suite checks that the output is exactly equivalent to Google's C++
protobuf library.

For debugging, the first step would be to print out the the error message
from input_stream.errmsg, and also dump the data in pb_bytes as hex values.

Then you can copypaste those hex bytes to online decoder to check that the
message data is correct when you are passing it to nanopb:
https://protobuf-decoder.netlify.app/

--
Petteri

Prince Montano

unread,
Oct 9, 2023, 5:21:25 AM10/9/23
to nanopb
Thank you very much, I will do just that! Thank you for creating such a sophisticated framework with a great community :))
Reply all
Reply to author
Forward
0 new messages