Exception (28) Parsing Serial

I had read and thought I had at least a basic understanding of "Serial input Basics" so I wanted to adapt Example 5 to send two floats via the serial port. I am not a programmer and clearly I am missing something. I had no need of sending the text or the integer values I simply need to send two values representing Camera temperature and Dew Point. However when I send anything from the serial monitor I get an Exception(28)

Code is below, many thanks for your thoughts.
Geoff

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
float camTempF = 0.0;
float dewPointF = 0.0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 2 pieces of data - 2 floating point values");
    Serial.println("Enter data in this style <12.87, 24.70>  ");
    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(NULL, ","); // this continues where the previous call left off
    camTempF = atof(strtokIndx);     // convert this part to a float

    strtokIndx = strtok(NULL, ",");
    dewPointF = atof(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
  
    Serial.print("Camera Temp ");
    Serial.println(camTempF);
    Serial.print("Dew Point: ");
    Serial.println(dewPointF);
}

MOD EDIT

As you can see, the forum made a mess of your code.

Please use code tags (select and use the "</>" editor button). Before you do that, reformat the code by pressing CTRL-T in the Arduino editor, to indent sensibly.

In your parseData() function, I think the first call to strtokIndx should reference tempChars, not NULL:

void parseData() 
{      
    // split the data into its parts
    char *strtokIndx; // this is used by strtok() as an index
     
    strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
    camTempF = atof(strtokIndx);     // convert this part to a float
.
.
.

And edit the comment so it is not a lie.

Thank you! Yes indeed that fixed the problem.
Geoff

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.