i cobbled this together from online examples it works to an extent, but not well enough :
const word numChars = 1024;
char receivedChars[numChars];
char SMsg[200] = {0};
int Commands[8] = {0};
char recvChar;
boolean newData = false;
//example string
//<0087:0002:22:11:33:14:Benson is in the hedges and he isnt hiding.:87>
void setup() {
Serial.begin(115200);
Serial.println("<Arduino is ready -->");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
doOtherStuff(); // <<=== NEW --- This just simulates time spent elsewhere in a big program
}
void recvWithStartEndMarkers1() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) { // <<== NEW - get all bytes from buffer
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
while (Serial.available() > 0) {
Serial.read();
}
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
} // <<=== NEW
}
}
void showNewData() {
if (newData == true) {
if (Commands[6] == 87){
//Serial.print("This just in ... ");
//Serial.println(receivedChars);
parseData();
showParsedData();
newData = false;
}
}
}
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars, ":"); // this continues where the previous call left off
Commands[0] = atoi(strtokIndx); // convert this part to an integer
for (int x = 1; x <= 5; x++) {
strtokIndx = strtok(NULL, ":"); // this continues where the previous call left off
Commands[x] = atoi(strtokIndx); // convert this part to an integer
}
strtokIndx = strtok(NULL, ":"); // get the first part - the string
strcpy(SMsg, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ":"); // this continues where the previous call left off
Commands[6] = atoi(strtokIndx); // convert this part to an integer
if (Commands[6] != 87){Serial.println("BadCheckSum");}
}
void showParsedData() {
Serial.print("Message: ");
Serial.println(SMsg);
Serial.print("ACK: ");
Serial.println(Commands[6]);
Serial.print("Sender: ");
Serial.println(Commands[0]);
Serial.print("Route: ");
Serial.println(Commands[1]);
Serial.print("Command: ");
Serial.println(Commands[2]);
Serial.print("Modifier 1: ");
Serial.println(Commands[3]);
Serial.print("Modifier 2: ");
Serial.println(Commands[4]);
Serial.print("Modifier 3: ");
Serial.println(Commands[5]);
}
void doOtherStuff() {
delay(20); // <<===NEW - This just simulates time spent elsewhere in a big program
}