A lightweight Arduino library for handling UART communication with a structured protocol. This library allows sending and receiving commands and data using a header-based protocol with a configurable baud rate.
- Send and receive commands with a header byte
- Send and receive data with configurable packet sizes
- Timeout handling for reading commands and data
- Debug logging (configurable)
Copy the UARTProtocol.h and UARTProtocol.cpp files into your Arduino project and include them in your sketch.
#include "UARTProtocol.h"HardwareSerial &serialPort = Serial1; // Use Serial1 or any available UART port
UARTProtocol uart(serialPort, 0xAA, 64, 115200);void setup() {
Serial.begin(115200); // Debugging output
uart.begin();
}uint8_t command = 0x01;
uart.SendCommand(command);byte data[] = {0x10, 0x20, 0x30};
uart.SendData(data, sizeof(data));uint8_t receivedCommand;
if (uart.ReadCommand(receivedCommand)) {
Serial.print("Received command: ");
Serial.println(receivedCommand, HEX);
}byte receivedData[3];
if (uart.ReadData(receivedData, 3)) {
Serial.println("Received Data: ");
for (int i = 0; i < 3; i++) {
Serial.print(receivedData[i], HEX);
Serial.print(" ");
}
Serial.println();
}if (uart.available()) {
Serial.println("Data available in buffer");
}Enable debugging by setting PROTOCOL_DEBUG_LOGS to 1 in UARTProtocol.h:
#define PROTOCOL_DEBUG_LOGS 1This will print debug logs to the serial monitor.
This library is open-source and free to use under the MIT License.