Go Down

Topic: Menu problem by typing string! (Read 665 times) previous topic - next topic

sirus

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

Here is it:
Code: [Select]

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:
Code: [Select]

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?

majenko

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.

sirus

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

Please??

Thanks!

majenko

Code: [Select]

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.

Arrch

#4
May 28, 2012, 07:32 pm Last Edit: May 28, 2012, 07:35 pm by Arrch Reason: 1
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:

Code: [Select]
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
   }
 }
}

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.

sirus

Thank you very much!
It works fine!

Arrch


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...

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.


Go Up