Menu problem by typing string!

Hello everyone!I've got a problem.In my loop i have done a menu with choices.

Here is it:

void loop()  {   

  if( Serial.available() > 0 )  {      
    char choice = (char)Serial.read();    
    switch (choice) {
      case 'x':  {
                   eeprom_erase_all();           
                   break;
      }
               
      case 'c': { 
                   eeprom_registration_check();  
                   break;
      }
               
      case 'r':{  
                  reset();                      
                  break;
      }
                 
      case 'm':{  
                  eeprom_memory_map();
                  break;
      }
      
      case 'w':{
                     readSerialString();
      }
       
      case 'l':{
                 __read_EEPROM();                      
                 break;   
      }
    }    
  }  
}

The choice l is not ready yet!My problem has to do with the choice 'w' in the function readSerialString.Here is the function:

 void  readSerialString() {
     while(Serial.available()>0) {
          // get the new byte:
          char inChar = Serial1.read(); 

          // add it to the inputString:
          inputString += inChar;
      
          // if the incoming character is a newline, set a flag
          // so the main loop can do something about it:
          if (inChar == '\n') {
             stringComplete = true;              
           } 
           if( stringComplete )
           {
              Serial.print(inputString);
           }
           //inputString = "";
           stringComplete = false;                 
       }                
}

More specifically!I want to send the command choice 'w' and the start receiving chars from serial!The characters are sending by an application in C# which i've done!
Is that possible?Can you help me please for one more time?

The problem here is that Serial.available() just returns what is in the serial buffer. It bears little or no resemblance to what you are sending.

Your routine will have to loop until it sees some pre-defined terminating character. The most common is the carriage return ('\r') or new-line ('\n') characters.

Could you give me an example please or send me something about it??????????????????

Please??

Thanks!

void  readSerialString() {
     while(true) {
       if(Serial.available()>0)
       {
          // get the new byte:
          char inChar = Serial1.read(); 

          // add it to the inputString:
          inputString += inChar;
      
          // if the incoming character is a newline, set a flag
          // so the main loop can do something about it:
          if (inChar == '\n') {
              Serial.print(inputString);
              return;
           }
           //inputString = "";
       }  
    }              
}

That will (hopefully - untested) run, reading one character at a time, until it receives a new-line ('\n'), at which point it returns back to your main loop.

Edit: The above post will completely pause your program until the full string is received. If that is acceptable, then it's the easier option. Otherwise, you'll want to see my post below:

You need a state machine. When 'w' is received, it should set a flag that will interpret any more serial data until the terminating character, as part of the string.

Pseudocode Example:

char state = 0;

void loop() {

  if serial available {
    read character
    if state is 0 {
       if character is 'w' then set state to 1 and clear string buffer
       ...
    } else if state is 1 {
       add character to string, test for terminator, parse string, etc..
       when done, set state back to 0
    }
  }
}

yes, a state machine is good. But, judging by the number of question marks, I doubt the OP is going to get his head around that.

Thank you very much!
It works fine!

majenko:
yes, a state machine is good. But, judging by the number of question marks, I doubt the OP is going to get his head around that.

The OP likely isn't the only one who will read this thread looking for a similar answer...

As an aside, I recommend that you don't put compound statements around your switch cases. It gives a misleading impression that you expect the execution of that case to stop when it reaches the closing brace. As you know, it will actually continue, falling through to following cases if necessary, until it reaches a break or the end of the switch statement. For example, when you get a 'w' your code will call __read_EEPROM();

This is the opposite to other conditional expressions like for, do, while, if, else: they only control the first following statement, so you should always make that a compound statement to avoid giving the misleading impression that any subsequent statements will also be conditional.

Switch tutorial: http://hacking.majenko.co.uk/node/43