Listen to the serial port for binary commands and act upon them.
This demo will simply print all received bytes back to the terminal once a complete command has been received, i.e. a byte stream ending in a specific order as set by the end-of-line (EOL) sentinel.
For convenience, this demo has the EOL set to '!!' such that you can type these ASCII characters in a terminal to terminate the command.
https://github.com/Dennis-van-Gils/DvG_StreamCommand Dennis van Gils 29-08-2022
#include <Arduino.h>
const uint8_t BIN_BUF_LEN = 16;
uint8_t bin_buf[BIN_BUF_LEN];
const uint8_t EOL[] = {0x21, 0x21};
void setup() { Serial.begin(9600); }
void loop() {
if (bsc_available == -1) {
Serial.println("Buffer has overrun and bytes got dropped.");
} else if (bsc_available) {
Serial.println("Received command bytes:");
for (uint16_t idx = 0; idx < data_len; ++idx) {
Serial.print((char)bin_buf[idx]);
Serial.write('\t');
Serial.print(bin_buf[idx], DEC);
Serial.write('\t');
Serial.println(bin_buf[idx], HEX);
}
}
}
A lightweight Arduino library to listen to a stream, such as Serial or Wire, for incoming commands (o...
Class to manage listening to a stream, such as Serial or Wire, for incoming binary commands (or binar...
Definition: DvG_StreamCommand.h:91
int8_t available(bool debug_info=false)
Poll the stream for incoming bytes and append them one-by-one to the command buffer buffer....
Definition: DvG_StreamCommand.cpp:132
uint16_t getCommandLength()
Return the length of the command without the EOL sentinel in bytes, only when a complete command has ...
Definition: DvG_StreamCommand.cpp:175
void reset()
TODO: descr.
Definition: DvG_StreamCommand.h:140