Pages

Monday, November 6, 2023

MicroProto, a micro protocol for data exchange between devices


MicroProto is a mini protocol for data exchange between devices.

It is build and tested over the serial communication protocol, but it can be ported to others.
It is designed to send strings and byte arrays.
MicroProto is a variable length, in the current implementation you can send up to 999 commands, and 999 byte long byte array.

The command structure is the following:
STX|ACK|NAK DATA CRC ETX

Command parts are:
  • STX (hex 0x02): command begin 
  • ACK (hex 0x06): acknowledgment command begin
  • NAK (hex: 0x15): negative acknowledgment command begin
  • DATA: byte array
  • CRC: 1 byte CRC code
  • ETX (hex 0x03): command end
Send command can request an acknowledgment to be sent back. The acknowledgment command must start with ACK or NAK byte.

The ESC special char (hex: 0x1B) is used as escape character. Indeed if a ETX byte is sent in DATA, it is preceded by the ESC character.

Each command must have a CRC hex byte, this prevents communications errors.
CRC function is CRC polynomial x^8 + x^7 + x^2 +1  implemented using a lookup table to speed up the computation process.

Errors in communications may happens, due to timing or interference. However I'm using this protocol with decent results.


Embedded side, there are two ways this protocol process input data: "blocking" and "timed".
The blocking way try to read input from the selected channel, that is UART in this example. If a char is read, then the next one is processed, till the the end of protocol command, or till the threshold wait time passed.
In the timed way, two function are involved, one that read characters, and another that can be called to get the last command found. Most of the works is done inside the read/input function. This function read one, or up to a threshold bytes until the end of protocol command.
On both function a few other error preventing mechanism are implemented.
The timed functions most of the time it's the way to go, cause it does not stop the working loop that much. One can even put this function in a TIMER, and use the read last command function in the program loop.

There are a few parameters that can be set inside the header files, above all involve timing.

Send/Get character functions are passed as parameters during initialization, this makes this library portable to a few microcontrollers, and also to other than USART bus.

Indeed this library is been tested on
  • AVR ATmega/ATtiny
  • ESP8266
  • ESP32
  • Arduino Framework
  • STM32 family with HAL framework
A NET C# client library is implemented for test, you can use this library as a starting point to write your own client library or use the code straight in you .NET project.


Code

Notes

  • read risk disclaimer
  • excuse my bad english

No comments:

Post a Comment