Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microcontroller to microcontroller communication library (over UART/RS232)

I want to interface two microcontrollers with a UART interface and I search a protocol to exchange data between them.

In practice, I want to exchange data periodically (ie: sensors reading) and also data on event (GPIO state). I have around 100-200 bytes to exchange every 100 milli second.

Does anybody know a protocol or library to achieve this kind of task ?

For now, I see protobuf and nano protobuff ? Is there something else ? It would be nice if I could add a software layer over the UART and use "virtual data stream" like if it was a TCP/IP connection to N ports.

Any idea ? Thanks

like image 281
ssinfod Avatar asked Dec 26 '22 01:12

ssinfod


1 Answers

I think the most straight forward way is to roll your own.

You'll find RS232 drivers in the manufacturers chip support library.

RS232 is a stream oriented transport, that means you will need to encode your messages into some frameing structure when you send them and detect frame boundaries on the receiver side. A clever and easy to use mechanism to do this is "Consistent Overhead Byte Stuffing".

https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing

This simple algorithm turns zeros in your messages into some other value, so the zero-byte can be used to detect start and end of frame. If a byte gets corrupted on the way you can even resynchronize to the stream and keep going.

The code on Wikipedia should be easy enough even for the smallest micro-processors.

Afterwards you can define your message format. You can probably keep it very simple and directly send your data-structures as is.

Suggestion for a simple message format:

Byte-ID   Meaning
---------------------------------
0         Destination port number
1         message type (define your own)
2 to n    message data

If you want to send variable length messages you can either send out a length byte or derive the length from the output of the Constant Overhead Byte Stuffing framing.

By the way, UART/RS232 is nice and easy to work with, but you may also want to take a look at SPI. The SPI interface is more suitable to exchange data between two micro-controllers. It is usually faster than RS232 and more robust because it has a dedicated clock-line.

like image 166
Nils Pipenbrinck Avatar answered Dec 28 '22 06:12

Nils Pipenbrinck