Hi,
> I was reading about pb and most of the examples transport the messages
> over data integrity streams layers (streams, tcp/ip..etc.. )
That is correct, protocol buffers itself does not define any
encapsulation format. This is both an advantage (flexibility) as well as
a drawback (you have to invent your own).
> Considering nanopb for embedded systems, lets say I want to exchange
> information via serial port.. What would be the best way to make a
> data integrity protocol to exchange pb data?
If you trust your serial port not to corrupt data, you could use one of
these simple methods:
1) Encode length as varint, then write the message.
2) Write the message, write 0 byte to terminate.
Otherwise, you'll need some checksum method. I have used the following,
but it is just one possible way. There is no standard:
1. Length (2 bytes = N)
2. Packet type (1 byte)
3. Protobuf message (N bytes)
4. Checksum (1 byte)
where checksum is just 0xFF XOR all bytes that come before it.
--
Extra rambling about the theoretically best way:
The problem with encoding length first is that you need to know it
before you write out the message. In practise this means that either you
need a memory buffer for the whole message or have to compute the length
separately.
I have been contemplating to use a Consistent Overhead Byte Stuffing
method. It would allow small memory usage when used together with
nanopb streams. However the plain COBS uses block size of 255, which is
already quite a lot for many smaller controllers.
However this is all a bit useless from practical point of view, because
if you make the packet format very complicated, everyone else will have
hard time decoding it.
--
Petteri