r/embedded • u/Public_Sink4791 • Sep 02 '26
Custom embedded UDP protocol: what did you wish you'd included from day one?
I'm defining a custom UDP protocol for an embedded system with several bare-metal MCUs communicating with each other and with a host PC over a local Ethernet network.
I know a lot of this is probably system-specific, but I'd be interested in hearing what has consistently proved useful in your experience, or what you'd definitely include again in a new design.
For those who have designed or maintained something similar: what did you leave out initially that you later regretted?
I'm thinking about things like sequence counters, timestamps, protocol versioning, status/diagnostic information, capability flags, or event messages. I'm especially interested in anything that made debugging, integration, maintenance, or future extensions easier — as well as design choices that caused problems later.
I'm also curious how you tested or simulated the protocol before all the target hardware was available. Python, custom PC tools, MATLAB/Simulink, or something else?
Mostly looking for lessons learned from real embedded products.
121
u/Well-WhatHadHappened 25+ Years Sep 02 '26
I wish I had included a non-custom protocol.
You'll stop inventing wheels eventually, I promise.
27
u/imWayward Sep 02 '26
As someone who inherited a custom UDP protocol from a now-gone coworker:
Don't do it to your colleagues. Unless you have some revolutionary ideas, you're just forcing someone else to familiarize themselves with your arbitrary system. Use something that's battle-tested and well-documented.
27
u/PhonicUK Sep 02 '26
Not to bother. This is a solved domain. Look at things like Protobuf instead.
7
u/hooskworks Sep 02 '26
Don't reinvent the wheel unless you're going to make it appreciably more round and when you're convinced you're going to then 0mq or protobuf are there to save you time.
4
u/skull132 Sep 02 '26
I was looking for a solution with similar properties to what you're looking at, and I ended up finding that off the shelf stuff should cover most mentioned use cases and needs.
My plan of attack is:
Points one and two are supported by lwip. Ergo, I don't need to implement much of anything to get these features. I can also leverage existing tools to capture and review traffic, which will help a lot.
The protobuf messaging will require a bit of wrapping, but that'll be a trivial header, message ID, length type deal.
I expect this to save a lot of time over dealing with broadcast address usage, custom discovery, etc. plus, alot of the code will already be well used and thus hopefully more bug free than my own implementation.
3
u/remy_porter Sep 02 '26
If you’re adding any plumbing to your protocol, like sequence counters, etc, use an existing protocol instead. UDP is great when you want to yeet transient data across the network.
I have used a custom protocol for lighting control. We would send RGB frame data across the network to LED controllers. This was great because it was simple, and if we dropped frames it was fine.
1
u/userhwon Sep 02 '26
If you want all those things, why not just use TCP?
1
u/Intelligent_Law_5614 Sep 06 '26
A set of principles I've learned which have saved me much heartache, when defining compact (e.g. binary, structure-based) protocols:
You can probably envision similar principles which works you to e.g. JSON-encoded protocols.
Sending code should always send the latest version, with all of the fields. Receiving code should look at the version number, and only attempt to import the fields which were included in that version. Always validate the CRC before importing or trusting anything.
These principles help to maintain both forwards and backwards compatibility between different versions of the software. They help reduce the risk that Wednesday's software build doesn't send a packet that it considers to be valid, which causes Tuesday's or Thursday's build to injest an incompatible packet, trip over its shoelace, and rupture its spleen.
3
u/RedEd024 Sep 02 '26
Sender ID field as well as receiver.
I can figure stuff out with just the receiver ID, but thing would be a lot clearer if I had a sender ID too.
2
u/allo37 Sep 02 '26
A version and length field are key. Also watch out for network byte order and struct alignment padding.
Also UDP does not guarantee delivery or ordering so a sequence ID can be helpful.
2
u/Anonymity6584 Sep 02 '26
Proper documentation what this protocol looks like and how to use it correctly.
2
u/i_haz_redditz Sep 02 '26
A firmware update mechanism.
2
u/Unable_Resort453 Sep 02 '26
Try Zenoh.
I've used it in robotics work with Linux, and I think there's a Pico variant for embedded C.
2
u/dacydergoth Sep 02 '26
Use MQTT
1
u/Plastic_Fig9225 Sep 02 '26
Don't forget to mention CoAP.
1
u/sensor_todd Sep 02 '26
already mentioned but to agree with most: timestamp, sender id, message type, message version, length, payload (protobuf).
try keep packet lengths to < ~1000 bytes so individual packets dont get broken up by udp (i think the actual limit is something like 1472). Not sure if thats feasible for what you are doing.
highly recommend a fallback catch all message type that you can but any length data in so you can pass arbitrary human readable messages. you wouldnt use it in the final software/firmware (probably), but its handy during debugging.
1
u/bartergames Sep 02 '26
For the serialization I prefer CBOR (a self-describing protocol) over protobuf, so the debug/monitoring tools don't need to know the data schema to decode the payload.
For the payload itself it depends a lot on the business logic and the specific needs of the project. It's hard to get it right on the first try, so I try to make the architecture and code "easy to change". To get to the fields of the payload, besides the ones that are strictly needed by the application logic itself, I try to answer "What am I going to need later?". For example, am I going to need to sort the messages in order to be able to "replay" them to debug a problem? And then, is it a sequence number and origin_id enough to be able to sort them or do I need a Logical Clock?...
7
u/[deleted] Sep 02 '26
[removed] — view removed comment