Multiple SMS to serial

Hi, can anybody help me !

I try make solution that send multiple sms and takes command in serial.
Problem is:

first i send sms 1 → number and message text “xxxxxxxxxx#test 1”
Modul send message correctly.

then i send sms 2 “xxxxxxxxxxx#test 2” and serial flush put old commands in data message like “AT+CMGS=” +CMGS: 116 OK xxxxxxxxxxxxxx"
can’t send because number and message text is not correct.
Serial command are not cleared ? Serial.flush() ?

then i send sms 3 “xxxxxxxxxxx#test 3”
Modul send message correctly.

then i send sms 4 “xxxxxxxxxxx#test 4” and serial flush put old commands in data message like “AT+CMGS=” +CMGS: 116 OK xxxxxxxxxxxxxx"
Same problem !

Sorry my English.

Here is my code:

#include <LiquidCrystal.h>

int led = 13;
int onModulePin = 2;  

String readString;
String nroString;

LiquidCrystal lcd(12, 11, 9, 8, 7, 6);

void switchModule(){

  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);

}

void setup(){

  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);
  
  switchModule();                 
  delay(5000);
  
  Serial.println("AT+CMGF=1");  
  delay(5000);
  
  Serial.flush(); 
 
  lcd.clear(); 
  lcd.print("Wait serial...");
}

void Send(){
      
          lcd.clear(); 
          lcd.print(readString);
          
//   Serial.println("AT+CMGF=1");       
//   delay(500);        
    Serial.print("AT+CMGS=");   
    Serial.print(34,BYTE);
    Serial.print(nroString); 
    lcd.clear();
    lcd.print(nroString);  
    Serial.println(34,BYTE);
    delay(500);   
    Serial.print(readString);
//    lcd.clear();
//    lcd.print(readString); 
    delay(500);
    Serial.println(0x1A,BYTE); 
    delay(500);  
//    lcd.clear();    
//    lcd.print("Ready..");                 
          
          readString = "";
          nroString = "";

     Serial.flush(); 

}

void loop(){

     while (Serial.available()) {      
//          lcd.clear(); 
//          lcd.print("Serial....");
  
	  delay(10);

          if (Serial.available() >0) {
          
//            lcd.clear(); 
//            lcd.print("Serial read..");
            
  	    char c = Serial.read();
  	    readString += c;
  
              if (c == '#'){
                 nroString =  readString;
                 
                  }          
                }             
             }
  
    if (readString.length() >0 && nroString.endsWith("#")) {
 
          nroString = nroString.replace('#', "");      
          readString = readString.replace(nroString, "");
          readString = readString.replace('#', "");
     
          Send();      
      
    }
    
 }
     while (Serial.available()) {      
//          lcd.clear(); 
//          lcd.print("Serial....");
  
      delay(10);

          if (Serial.available() >0) {

The while statement checks to see that there is serial data available. If there is, the body of the statement is executed. You do nothing for 10 milliseconds. Why?

Then, you test to see if there is serial data available. Where would it have gone? Once a character is put in the buffer, the only function that can remove it is Serial.read(), which was most certainly not called by delay().

Removing the code dealing with the LCD screen, which just confuses the issue, Send looks like this:

void Send()
{
    Serial.print("AT+CMGS=");   
    Serial.print(34,BYTE);
    Serial.print(nroString); 
    Serial.println(34,BYTE);
    delay(500);   
    Serial.print(readString);
    delay(500);
    Serial.println(0x1A,BYTE); 
    delay(500);  
    readString = "";
    nroString = "";

    Serial.flush(); 
}

This sends a command, some value (a delimiter?), a string, some value, and a string, then waits for half a second, sends another string, waits for half a second, sends a final value, then dumps any reply that might have been received into the bit bucket.

Why are you dumping any reply that might have been received into the bit bucket?

Now, the thing about this code that has me scratching my head is that it appears as though you are using the serial port for two different purposes - talking to the user on the PC and talking to the SMS module. Is that indeed what you are doing? If so, by now you know that that won't work.

Thanks for help.

i change my code and i use SMS.h library.

No problem is: gsm.SendSMS(char*, char*); and i have String nroString and String readString.

How i convert String to char* ?

Thx fo help

        if (Serial.available() >0) {
      
             char c = Serial.read();
             readString += c;
               
              if (c == '#'){
                 nroString =  readString; 
                }                       
        }
  }
  
   if (readString.length() >1) {

          nroString = nroString.replace('#', "");      
          readString = readString.replace(nroString, "");
          readString = readString.replace('#', "");


   ret_val2 = gsm.SendSMS(nroString , readString);

How i convert String to char* ?

The String class has a toCharArray() method that does exactly that.

Then, you test to see if there is serial data available. Where would it have gone?

Maybe it hasn't arrived. ;)

Maybe it hasn't arrived.

Then why would Serial.available() say it had?

PaulS:

Maybe it hasn’t arrived.

Then why would Serial.available() say it had?

If the serial available sees something in the input buffer, something is there. Then what is in the input buffer is removed. From what I’ve seen, a “while” loop checks the input buffer much quicker than bites can be placed in the input buffer by the usual serial protocol, so in the next while loop the serial input buffer may appear to be empty with more data is still on the way. If the code is depending on the “empty buffer” as an indication that the transmission is complete, then errors can occurr due to this mismatch. It all depends on the code being used as to how the end of transmission or a complete data packet is determined. Appropriate receiving code has to be used for however the data is being sent. For simple data transmissions, simple receiving code can be used.

Both the while statement and the if statement use the same method to determine that there is CURRENTLY data in the buffer that can be read. Ergo, one of them is unnecessary. Neither is using psychic divination to determine that more data may be coming. Neither knows whether that data will belong to the current packet, or not.

Like it or not, the if statement is completely unnecessary, as is the delay.

PaulS: Like it or not, the if statement is completely unnecessary, as is the delay.

Well, as in the past, I challange you to post the code that works the same to capture a string without the delay. There may be a redundent "if" test as the "while" appears to be a convient combination of a "if" in a loop. Waiting on the improved code (and don't drift off into "delimiters"). :)