serial read char string from serialmonitor

I’ve been having trouble getting the ArduinoMega to read a complete text string sent from the serial monitor into a char array. I’ve tried a few different ways to do it but it will loose 2-4 charachters off the end 90% of the time. I tried looking for answers but didn’t find any that have worked. I tried one of Nick Gammon’s examples with the same effect.

It is to ask for text strings from the user computer and store the strings in EEPROM for later use after restart.

bool interface()
{
  int l;
  int p;
  int pos = 0;
  int noBytes = 0;
  char inChar;
  char inData[128];
  boolean m = false;
  inbyte = Serial.read();
  if (inbyte == 'a'){
    Serial.println("CONFIGURE UNIT STREAM OUTPUT");
    //store 4 char stream ID at EEPROM 100-103;
    Serial.println("enter 4 letter stream indentifier");
    while( m == false){
      if(Serial.available() >= 4){ //also tried simply "> 0"
        inChar = Serial.read();
        switch(inChar){
        case '\n':
          inData[pos] = 0;

          Serial.println("you entered");
          for(int n=0; n<4; n++){
            Serial.print(inData[n]); 
          }
          Serial.println();
          p = 100;
          for(int n=0; n<4; n++){
            EEPROM.write(p, inData[n]);
            p++;
          }
          Serial.flush();
          m = true;
          break;
        case '\r':
          break;      
        default:
          if(pos < 5){
            inData[pos] = inChar;
            pos++;
          }
          break;  
        }//end switch  
      }//end if available
    }//end while
    m = false;

If I enter “ABCD” and send I get back AB and sometimes despite having called Serial flush I get the CD part on the next entry step.
I also tried it this way. Sometimes this step works correctly, sometimes not.

 //store 10 char city state at EEPROM 104-113
    Serial.println("enter 10 letter city state");
    while( m == false){
      if(Serial.available() >= 10){
        delay(100);
        noBytes = Serial.available();
        l = 0;
        while(Serial.available() > 0){
          inData[l] = Serial.read();
          l++;
        }
        Serial.println("you entered");
        for(int n=0; n<10; n++){
          Serial.print(inData[n]);
        }
        Serial.println();  
        int p = 104;
        for(int n=0; n<10; n++){
          EEPROM.write(p, inData[n]);
          p++;
        }
        Serial.flush();
        m = true;
      }  
    }

So are there any exaples of entering strings of text via serial monitor???

          Serial.flush();

Block until all pending data has been sent. How is THAT useful?

What is the option on the serial monitor, for appending data? If it is cr\lf, as it appears from your code, then a 4 character value with cr\lf appended is … hang on a second, got to take a shoe and sock off … um, let’s see … looks like 6 characters.

          Serial.println("you entered");
          for(int n=0; n<4; n++){
            Serial.print(inData[n]); 
          }

And, what does this show?

        delay(100);

There is no reason to delay. Serial data arrives at it’s own pace. You should read and store data until the end of packet marker arrives.

What isn’t clear is what “If I enter “ABCD” and send I get back AB and sometimes despite having called Serial flush I get the CD part on the next entry step.” means. If you are entering “ABCD”, and the Serial Monitor is sending “ABCD”, then the 4 characters “ABCD” should be stored in the array (and printed by the above code).

The flush() is not doing what you seem to think it does. It does not so anything with the incoming serial data buffer, as of 1.0.