Go Down

Topic: String or Char Arrays Length Definition (Read 2 times) previous topic - next topic

PaulS

Quote
I don't know what to do with this phrase:

You are using the Serial Monitor to send data. At the bottom of the panel, there is a drop down list with options for appending data to what is in the Send box. What option do you have selected? Anything other than none causes the Serial Monitor application to append something to what you enter.

That something can be detected on the Arduino, and used as the end of packet marker. We need to know what that something is.

PaulS

If you are using "Carriage return" as the option, try something like this:

Code: [Select]
char inData[9];
byte index = 0;
bool errorPrinted = false;

void loop()
{
   while(Serial.available() > 0)
   {
       char c = Serial.read();
       if(char == '\n')
       {
          // Got the carriage return
          // Use the data in inData, if index in range

          index = 0;
          inData[index] = '\0';
          errorPrinted = false;
       }
       else
       {
          if(index < 8)
          {
             inData[index++] = c;
             inData[index] = '\0';
          }
          else
          {
             if(!errorPrinted)
             {
                 // Print an error

                 errorPrinted = true;
             }
          }
       }
   }
}

Go Up