Author Topic: Framing algorithm for fixed packet sizes  (Read 2757 times)

0 Members and 1 Guest are viewing this topic.

Offline paeionTopic starter

  • Contributor
  • Posts: 20
  • Country: au
Framing algorithm for fixed packet sizes
« on: March 18, 2023, 02:55:03 am »
Hi all,

I am working on an application that visualises sensor data in real time. An ESP32 collects the sensor data and sends 18-byte packets over bluetooth serial to the PC, using python and the Qt framework for the GUI. The ESP32 bluetooth latency is fairly inconsistent so I want to limit the packet overhead to achieve the required throughput. I am currently transmitting the data at 500Hz but hoping to increase that towards 1kHz. Using 2-bytes as start-of-frame and another 2 for end-of-frame, I have a crude way of synchronising and delimiting my packets but I still get framing errors. I am fairly certain ASCII-encoding is too much overhead for the ESP32.

I have looked into COBS encoding but the 255 byte-lookahead is not ideal and I am not overly confident in implementing the buffer for that (C++). SLIP encoding also looks interesting but I am wondering if the non-constant packet sizes from byte stuffing will cause more troubles for the bluetooth serial communication than a constant-overhead communication protocol. I can live without a checksum/ CRC for what I am trying to do.

What framing algorithm/ communication protocol would you recommend for transmitting fixed-length 18-byte packets at the desired 1000Hz?
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4876
  • Country: us
Re: Framing algorithm for fixed packet sizes
« Reply #1 on: March 18, 2023, 04:46:15 am »
COBS only needs look ahead for the lesser of 254 bytes or 1 packet.  If your messages are only 18 bytes, that is all the look ahead you need.  That also means that there is always exactly one padding byte per message at the very beginning so you can easily do the null removal transformation in place. 

If you have longer messages you can also modify COBS to trade overhead for lookahead.  Standard COBS adds a padding byte for every 254 consecutive non-zero bytes.  If you instead are willing to add a padding byte for every 32 consecutive non-zero bytes, then you only need 32 byte lookahead.

Variable packet sizes also shouldn't be a problem for a bluetooth serial interface, so a simple escaping protocol like the SLIP encoding should also work fine but the worst case overhead will be a lot higher.  How much you care about that I don't know.

 

Offline paeionTopic starter

  • Contributor
  • Posts: 20
  • Country: au
Re: Framing algorithm for fixed packet sizes
« Reply #2 on: March 19, 2023, 07:26:47 am »
Thanks for your reply ejeffrey, I think you've cleared up my confusions about COBS encoding!

It is probably worth the time implementing both encoding methods and testing out which one fits my requirements best.

What exactly does "null removal transformation in place" mean if I were to COBS encode and send just 18-byte packages?
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 3937
  • Country: gb
Re: Framing algorithm for fixed packet sizes
« Reply #3 on: March 19, 2023, 09:15:24 am »
What exactly does "null removal transformation in place" mean

It means doing the COBS encoding in the buffer you already have, without copying to a new buffer. But for such small buffers it wouldn't really matter much which way you you choose to do it.

Here is my COBS thread with example code for MCU in C, and PC code in Python.
 
The following users thanked this post: mikerj, paeion

Offline paeionTopic starter

  • Contributor
  • Posts: 20
  • Country: au
Re: Framing algorithm for fixed packet sizes
« Reply #4 on: March 20, 2023, 10:31:00 pm »
Very useful resources, thank you for the thread link!

I have resolved my packet framing issues with COBS encoding now. I get a consistent 2-byte overhead as my data always has \x00 present and it has improved the serial communication latency significantly.

I will have to experiment further with increasing the lookahead to reduce the packet framing overhead but for the moment its a good starting point :D
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->