// Empfänger const byte numChars = 32; char receivedChars[numChars]; char tempChars[numChars]; // temporary array for use when parsing // variables to hold the parsed data char messageFromPC[numChars] = {0}; float integerFromPC = 0.0; float wert1 = 0.0; float wert2 = 0.0; float wert3 = 0.0; float wert4 = 0.0; float wert5 = 0.0; boolean newData = false; //============ void setup() { Serial.begin(19200); Serial.println("bereit"); Serial.println(); } //============ void loop() { recvWithStartEndMarkers(); if (newData == true) { strcpy(tempChars, receivedChars); // this temporary copy is necessary to protect the original data // because strtok() used in parseData() replaces the commas with \0 parseData(); showParsedData(); newData = false; } } //============ void recvWithStartEndMarkers() { static boolean recvInProgress = false; static byte ndx = 0; char startMarker = '<'; char endMarker = '>'; char rc; 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 parseData() { // split the data into its parts char * strtokIndx; // this is used by strtok() as an index strtokIndx = strtok(tempChars, ","); // get the first part - the string strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC strtokIndx = strtok(NULL, ","); wert1 = atof(strtokIndx); strtokIndx = strtok(NULL, ","); wert2 = atof(strtokIndx); strtokIndx = strtok(NULL, ","); wert3 = atof(strtokIndx); strtokIndx = strtok(NULL, ","); wert4 = atof(strtokIndx); strtokIndx = strtok(NULL, ","); wert5 = atof(strtokIndx); } //============ void showParsedData() { Serial.print("Text: "); Serial.println(messageFromPC); Serial.print("Wert 1: "); Serial.println(wert1); Serial.print("Wert 2: "); Serial.println(wert2); Serial.print("Wert 3: "); Serial.println(wert3); Serial.print("Wert 4: "); Serial.println(wert4); //Serial.print("Wert 5: "); //Serial.println(wert5); }