Pages: [1]   Go Down
Author Topic: Menu problem by typing string!  (Read 616 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Here is it:
Code:
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:
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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please??

Thanks!
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
    }
  }
}
« Last Edit: May 28, 2012, 12:35:17 pm by Arrch » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much!
It works fine!
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: