This thread gives an outstanding explanation of the use of serial communications:
http://forum.arduino.cc/index.php?topic=396450.0
Robin2 gives in example 5 a means to send in one message various values: integer, float, character,..
I use this with succes to send 3 floats, 2 integers and one character by HC-12 radio.
Example: <99.99,22.25,3.47,11,22,A>
However, from time to time the receiver crashes when receiving messages; using serial monitor debugging I discovered that the receiving controller always crashes while processing the last string (the character, A).
To debug, I inserted serial output prints after each parsing command:
strtokIndx = strtok(tempChars, ","); // get the first part - a float
integer1FromPC = atof(strtokIndx); // convert this part (first value) to a float: dutycycle
Serial.print(F(" 1 "));
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integer2FromPC = atof(strtokIndx); // convert this part (second value) to a float: temperature
Serial.print(F(" 2 "));
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integer3FromPC = atof(strtokIndx); // convert this part (third value) to a float: battery voltage
Serial.print(F(" 3 "));
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integer4FromPC = atoi(strtokIndx); // convert this part (fourth value) to an integer: minutes past the hour
Serial.print(F(" 4 "));
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integer5FromPC = atoi(strtokIndx); // convert this part (fifth value) to an integer: hour of the day
Serial.print(F(" 5 "));
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
strcpy(messageFromPC, strtokIndx); // this is a text string: copy it to messageFromPC:
// sensor identification, sensor "A" or sensor "B", or C or D
Serial.println(F(" 6 "));
Serial.println();
When a crash occurs, I still get the value "5" on the serial monitor, but not the value "6", always.
This is the same parsing code as used in Robin2 example 5.
I suspect that once in a while the message is corrupted during transmission and that either a null character or something else that is not right is being received.
Would it be possible to include code that distinguishes, in the message to be parsed, whether there are either 0 or null values, or some other way to prevent the code execution to cause the controller to crash?