Why the condition is not respected while I have a coma

Dear all

I experiencing a funny problem.
I am working with Arduino Leonardo and Arduino 1.5.7

I am listing Serial1.read().
When it read +CMGL (with a space afeter the L), the code read the next position (7th) which mach to a SMS position.
But as a SMS position can have to digit, I have to make the program reading the next position (8th) until it meet the coma.
Here is the an exemple of string to read

+CMGL: 2,“REC UNREAD”,"+41763435206","",“14/09/25,13:25:42+08”

It work while I print the catched position with Serial print.
In my code there is a condition if

 if(buffer[x] == ',')

if it’s true, it add a pipe between the number. And this work.

But now I need to put in buffer, the SMS poisition the use it later in my code.

It very funny, because when I uncomment that part of code

Serial.print(F("----"));
                  for(bbb=0; bbb < message_pos_buff_index; bbb++)
                  {
                    Serial.print(F("pass?"));
                    Serial.print(message_pos_buff[bbb]);
                  }
                  Serial.print(F("---"));

it does read the coma. so the condition

if(buffer[x]==',')

return false
when I comment the above code, it return true!!!

Could you explain me that???

Here is my code

Serial.println(AT+CMGL=\"REC UNREAD\",1);
char replyCommand [7] = {'+','C','M','G','L',':',' '};
  //char compm [7] = {'0','0','0','0','0','0','0 '};
  uint8_t x=0;
    boolean answer=false;
    //char buffer[BUFFERSIZE];
    unsigned long previous;

    memset(buffer, '\0', BUFFERSIZE);    // Initialize the string

    while(Serial1.available() > 0) Serial1.read();    // Clean the input buffer
    
    int y = 0;
    previous = millis();
    int timeout = 5000;
    int count_message = 0;
    char message_pos[10];
    char message_pos_buff[4];
    int message_pos_buff_index = 0;
    int bbb;
    int ma = 0;

    // this loop waits for the answer
    do{
        if(Serial1.available() > 0){
          if(x < BUFFERSIZE-1) // Do not fill the buffer more the it size
          {
            // READ THE RESPONSE, CARACTER BY CARACTER
              buffer[x] = Serial1.read(); 
              //delay(500);
// DISPLAY THE CARACTER BY POSITION
              Serial.print(x); Serial.print(F(":")); Serial.println(buffer[x]);
            
// IF THERE WERE 6 FOLLOWING CARACTER MATCHING THE MASK, CONTINUE
              if(y) 
              {
// IF CARACTER MATCH TO COMA
// IF THE NUMBER is 10, IT WILL GO TO else AND WHEN IT MEET A COMA IT CONTINUE HERE
                if(buffer[x] == ',')
                {
// DEBUG                  
                  Serial.print(F(".("));
                  Serial.print(message_pos_buff_index);
                  Serial.println(F(")."));
                  // HERE I WANT TO READ MY SMS POSITION. IF THE POSITION IS 2, IT SHOULD GO ONCE IN THE FOR LOOP. IF ITS 10 IT SHOULD GO TWICE. BUT WHEN I UNCOMMENT IT, if(buffer[x]==',') return false, when it should return true. WHY?
                  /* 
                  Serial.print(F("----"));
                  for(bbb=0; bbb < message_pos_buff_index; bbb++)
                  {
                    Serial.print(F("pass?"));
                    Serial.print(message_pos_buff[bbb]);
                  }
                  Serial.print(F("---"));
*/
                  
                  
                  message_pos_buff_index=0;
                  Serial.print(F("..("));
                  Serial.print(message_pos_buff_index);
                  Serial.println(F(").."));
                  
                  Serial.println(F(" | "));
                  
                  count_message++;
                  y=0;
                  
     
                  
                  
                }
                else
                {
                  // IF CARACTER DOES NOT MATCH, PRINT THE 7th CARACTER
Serial.println(buffer[x]);
                // DEBUG
                Serial.print(F("message_pos_buf_index "));Serial.println(message_pos_buff_index);
                // PUT CARACTER INTO A BUFFER
                 message_pos_buff[message_pos_buff_index] = buffer[x];
// INCREMENT THE BUFFER INDER FOR THE NEXT CARACTER, IF THE POSITION IS ABOVE 9
                 message_pos_buff_index++;
     // DEBUG
                 Serial.print(F("("));
                 Serial.print(message_pos_buff_index);
                 Serial.println(F(")"));
                 
                 
                
                }
              }
              
              
              //'+','C','M','G','L',':',' '
              // COMPARE THE CARACTER
              if (buffer[x] == replyCommand[ma])    
              {
// IF THERE 6 SAME CARATER AS THE MASK PUT Y TO 1 (SEE UP IN THE CODE )
                if(ma==6)
                {
                  y=1;
                   
                }
                else
                {
                  y=0;
                }
// IF IT MATCH INCREMENT TO CONTROLL IF THERE 6 CARACETERS WHICH ARE FOLLOWING
                ma++;
          
              }
              else
              {
// IF THE 2nd OR ONE BETWEEN THE 6 REQUIRED CARACTER, PUT ma TO 0
                ma = 0;
              }
              
            
            x++;
          }
        }
        // Waits for the asnwer with time out
    }
    
    while( ((millis() - previous) < timeout));  
   
   Serial.print(count_message); Serial.println(F(" messages")); 
   /*
   for(int s=0; s<=count_message;s++)
   {
     Serial.println(message_pos[s]);
   }
   */
  
}

Any idea, or how can make it better?
What’s bug with

if(buffer[x]==',')

You need to get the code to the point where it compiles. You have a do statement with no test expression, the definition of buffer[] is commented out, but you still try to use it. There are a host of other issues, too. Try to get the code to compile and then repost it. Also, use Ctrl-T to reformat the code before you post it.

Dear emkojac

Thank for your reply
I do not understand you comment. the test expressionn of the do loop is here
while( ((millis() - previous) < timeout));
the timeout is 5000.

the print of buffer[x] is here, just above the test of the mask //’+’,‘C’,‘M’,‘G’,‘L’,’:’,’ ’

Serial.println(buffer[x]);
                
                }
              }
              
              
              //'+','C','M','G','L',':',' '
              
              if (buffer[x] == replyCommand[ma])

When I compile it does not display any errors.
:slight_smile:
Did I understood you well?

if I keep it commented

/* 
                  Serial.print(F("----"));
                  for(bbb=0; bbb < message_pos_buff_index; bbb++)
                  {
                    Serial.print(F("pass?"));
                    Serial.print(message_pos_buff[bbb]);
                  }
                  Serial.print(F("---"));
*/

the serial.println(buffer[x]) and the print of the pipe works well…

A coma is a very different thing from a comma. You are talking about commas

You did not post your entire code - hence the assumption that it would not compile.

If you are not sure how to identify where a comma comes in a series of characters write a short 10 or 15 line program as a learning exercise and try to parse a fixed string such as buffer[] = "abc,def,456,ghj"; Then it will be much easier to give you advice.

...R

Hello
but I post my code!??
I change the way to do. I am using a String

String sms_pos

to concatenate the desired value

sms_pos.concat(buffer[x]);
sms_pos.concat(",");

When I wrote comma (or coma), I menas “,” :slight_smile:
Here is my code, which is now “loghter”

Serial1.println(cmd);
  //waitResp(10000,"OK",true);  
  
  char replyCommand [7] = {'+','C','M','G','L',':',' '};
  //char compm [7] = {'0','0','0','0','0','0','0 '};
    int x=0;
    boolean answer=false;
    //char buffer[BUFFERSIZE];
    unsigned long previous;

    memset(buffer, '\0', BUFFERSIZE);    // Initialize the string

    while(Serial1.available() > 0) Serial1.read();    // Clean the input buffer
    
    int y = 0;
    previous = millis();
    int timeout = 5000;
    int count_message = 0;
    String sms_pos;
    int ma = 0;

    // this loop waits for the answer
    do{
        if(Serial1.available() > 0){
          if(x < BUFFERSIZE-1) // Do not fill the buffer more the it size
          {
              buffer[x] = Serial1.read(); // response
              //Serial.print(x); Serial.print(F(":")); Serial.println(buffer[x]);
            
              if(y) // and count_message < 10 
              {
/*
                Serial.print(F("("));
                Serial.print(buffer[x]);
                Serial.print(F(")"));
    */            
                if(buffer[x] == ',')
                {
                  Serial.print(F(" | "));
                  sms_pos.concat(",");
                  count_message++;
                  y=0;
                }
                else
                {
                Serial.print(buffer[x]);
                sms_pos.concat(buffer[x]);
                }
              }
           
              //'+','C','M','G','L',':',' '
              
              if (buffer[x] == replyCommand[ma])    
              { 
                if(ma==6)
                {
                  y=1;
                   
                }
                else
                {
                  y=0;
                }
                ma++;
          
              }
              else
              {
                ma = 0;
              }
              
            
            x++;
          }
        }
        // Waits for the asnwer with time out
    }
    
    while( ((millis() - previous) < timeout));  
   
   Serial.print(count_message); Serial.println(F(" messages")); 
   Serial.println(sms_pos);

it display this

2|4|5|67| 4 messages
2,4,5,67,

and it should dipslay

2|4|5|6|7|8|9|10|11|12|13| 11 message
2,4,5,6,7,8,9,10,11,12,13

When I comment

sms_pos.concat(buffer[x]);
sms_pos.concat(",");

it return wrongly

2|45|6|7|4 messages

:sweat_smile:

Ok, there were a mistake on last code
Correction:

uint8_t x=0;

Here is the code now

  Serial1.println("AT+CMGL=\"REC UNREAD\",1");
  //waitResp(10000,"OK",true);  
  
  char replyCommand [7] = {'+','C','M','G','L',':',' '};
  //char compm [7] = {'0','0','0','0','0','0','0 '};
    uint8_t x=0;
    //boolean answer=false;
    //char buffer[BUFFERSIZE];
    unsigned long previous;

    memset(buffer, '\0', BUFFERSIZE);    // Initialize the string

    while(Serial1.available() > 0) Serial1.read();    // Clean the input buffer
    
    int y = 0;
    previous = millis();
    int timeout = 5000;
    int count_message = 0;
    String sms_pos;
    int ma = 0;

    // this loop waits for the answer
    do{
        if(Serial1.available() > 0){
          if(x < BUFFERSIZE-1) // Do not fill the buffer more the it size
          {
              buffer[x] = Serial1.read(); // response
             // Serial.print(x); Serial.print(F(":")); Serial.println(buffer[x]);
            
              if(y)
              {
                if(buffer[x] == ',')
                {
                  Serial.print(F("|"));
                  sms_pos.concat(",");
                  count_message++;
                  y=0;
                }
                else
                {
                Serial.print(buffer[x]);
                sms_pos.concat(buffer[x]);
                }
              }
           
              //'+','C','M','G','L',':',' '
              if (buffer[x] == replyCommand[ma])    
              { 
                if(ma==6)
                {
                  y=1;
                   
                }
                else
                {
                  y=0;
                }
                ma++;
          
              }
              else
              {
                ma = 0;
              }
              
            
            x++;
          }
        }
        // Waits for the asnwer with time out
    }
    
    while( ((millis() - previous) < timeout));  
   
   Serial.print(count_message); Serial.println(F(" messages")); 
   Serial.println(sms_pos);

and it display this

2|4|5|67|8|9|10|11|12|13|10 messages
2,4,5,67,8,9,10,11,12,13,

while it should display this!!!

2|4|5|6,7|8|9|10|11|12|13|10 messages
2,4,5,6,7,8,9,10,11,12,13,

with a coma witween 5 and 6!!

I uncomment ths

Serial.print(x); Serial.print(F(":")); Serial.println(buffer[x])

and there is a , after the 5. Why the

if(buffer[x]==',')

do not reconize it?

Ok, apparently, this solve my problem

if(String(buffer[x]) == ",")

any comment from your point of view?

Cheers

Here is the code now

Are you using the Arduino IDE?
If so that code will not compile because there are statements outside of any function definition.

If you say it does compile then you are not posting all your code or you are lying.

Diffusez tout votre code;

    //char buffer[BUFFERSIZE];   // <===== buffer[] n'est PAS defini, 
    unsigned long previous;

    memset(buffer, '\0', BUFFERSIZE);    // mais???

Ou est: setup(), loop()??

Hello thank for helping.

You are right I have not post all of my code as setup or BUFFER SIZE.
This is because it works.
(J'ai pas juger utile de mettre cette partie)
If I add to put all, as setup , it will take a lot of line to read

My problem where around that condition

 if(String(buffer[x]) == ",")

ans since I changed it, it works as espected.

But in order to answer to your request here is how I declared thse variable
That is define before setup()

// sendATcommandBuff
#define BUFFERSIZE 400
char buffer[BUFFERSIZE];

setup look like this

void setup()
{    
    Serial.begin(baud_rate);
    delay(1000);
    //while(!Serial); 
    Serial1.begin(baud_rate);
    while(!Serial1);

    delay(4000);
   
    #ifdef DEBUG 
      Serial.println(F("\nStarting module"));
      Serial.println(F("********************\n"));
    #endif
    
    pinMode(pin_gsm,OUTPUT);              // Set the pins
    pinMode(pin_gps,OUTPUT);
    pinMode(pin_power, OUTPUT);
    
    gsm_enable();
    
    
    powerup:
    if(power_up(5))
    {
      #ifdef DEBUG 
        Serial.println(F("  Module is started"));
      #endif
      
      delay(3000);

      //sms_setting();

      if(gps_start() == 0)
      {
        goto powerup;
      }

      while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000, true, false, false,false) == 0);

      readyToGo = true;
      
      #ifdef DEBUG
        Serial.println(F("\nReady To go\n")); 
      #endif
    }
    else
    {
      #ifdef DEBUG
        Serial.println(F(" Module could not start"));
      #endif
      
      readyToGo = false;
    }
}

loop has only that function

serialhwread();

I do not think it's necessary to past the code, because the code has nothing to with my issue.
The code only read what I enter to the terminal to lunch a commans, as "sms,l" to list my UNREAD SMS.

Thank

Vous pouvez joindre votre programme sans énumérer tout cela . Nous avons besoin de voir tout cela .

pierrot10:
I do not think it's necessary to past the code, because the code has nothing to with my issue.

I see.

http://snippets-r-us.com/

We like to compile your code and see for ourselves what data types you use etc. It is quicker to do that than drag out of you, piece by piece, over a couple of days, enough information to see the problem. So please post it. You can do attachments if it is too large to fit.

How to use this forum

... the code has nothing to with my issue ...

Why are you posting in Programming Questions then?

                if(ma==6)
                {
                  y=1;
                   
                }
                else
                {
                  y=0;
                }
                ma++;
          
              }
              else
              {
                ma = 0;
              }
              
            
            x++;

Are you entering the Obscure Code competition here?

Hello,
then here my ino file with complete code :slight_smile:
My work is around sms_list() function
I received the SMS position in a String sms_pos.
the posisiton delimted by a comma (,).

Now I am working to splite that string to a char and then to use the sms_read function.

Cheers and have a nice week-end

smi908_sms.ino (18.6 KB)

pierrot10:
In my code there is a condition if

 if(buffer[x] == ',')

No there isn't, now we've got the code we can see that that's not true.

There is this if:

              if (buffer[x] == replyCommand[ma])

In which the RHS might be ',' in some circumstances, but the codes
opaque to a quick inspection (which indicates it ought to be restructured
in a clear way).