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]==',')