/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
// test with "¥XYZab1¥XYZac0¥XYt12w" which will match the frame format for 3 frames
const byte startMarker = 0xA5; // Start marker
const byte payloadLength = 5; // Length of the payload
byte payload[payloadLength]; // Buffer to hold the payload
byte payloadIndex = 0; // Current index in the payload buffer
enum State { WAIT_FOR_START, RECEIVE_PAYLOAD, RECEIVE_CHECKSUM, PROCESS_FRAME } currentState = WAIT_FOR_START;
void processSerialData() {
while (Serial.available()) {
byte incomingByte = Serial.read(); // Read the incoming byte
switch (currentState) {
case WAIT_FOR_START:
if (incomingByte == startMarker) {
payloadIndex = 0; // Start a new frame
currentState = RECEIVE_PAYLOAD;
}
break;
case RECEIVE_PAYLOAD:
payload[payloadIndex++] = incomingByte; // Store payload byte
if (payloadIndex >= payloadLength) {
currentState = RECEIVE_CHECKSUM;
}
break;
case RECEIVE_CHECKSUM:
{ // compound statement needed because we define a local variable to this case
byte cksum = 0;
// Calculate checksum
for (int i = 0; i < payloadLength; i++) cksum += payload[i];
cksum ^= 0xFF;
// Verify checksum
if (cksum == incomingByte) {
currentState = PROCESS_FRAME; // Move to processing state
} else {
Serial.println("Checksum error");
Serial.print("Calculated Checksum = "); Serial.println(cksum);
Serial.print("Expected Checksum = "); Serial.println(incomingByte);
currentState = WAIT_FOR_START; // Reset state machine
}
}
break;
case PROCESS_FRAME:
Serial.print("Frame received correctly: ");
for (byte b : payload) {
if (b < 0x10) Serial.write('0'); // Print leading zero for single-digit hex
Serial.print(b, HEX);
Serial.write(' ');
}
Serial.println();
currentState = WAIT_FOR_START; // Reset to waiting for the next start marker
break;
}
}
}
void setup() {
Serial.begin(115200); // Initialize Serial with baud rate 115200
Serial.println(F("ready"));
}
void loop() {
processSerialData(); // Call the function to process serial data
// here you can do other stuff
}