Serial write and read problem

Hello

I'm trying to make automatic SMS message resender with Arduino UNO and A GSM SIM900 module.
I've run into a problem when I try to read the incoming data, the sent command to GSMmodule also gets read. It is not a big problem but id like to know why this happens and how to solve it. I tried to use Serial.flush() command after sending data but it did not help. I have a creeping suspicion that the problem is that I use the Serial.readString() comand. but I could not find anything on the nets about it. here is my code

#include <SoftwareSerial.h>
SoftwareSerial GSM(9,10);

String red;

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    GSM.begin(9600);
    Serial.begin(9600);
    GSM.println("AT+CMGR=3"); //i send comand to gsm to read 3 message
}

void loop()
{
    
    if (GSM.available()>0) //  Check if serial available
   {
        red = GSM.readString(); //  read the incoming data
   }
   
   Serial.print(red); // print the String to serial
}

and here is the output

AT+CMGR=3

+CMGR: "REC READ","+*************","","21/05/12,11:31:25+12"
Hehshzhzhsgdgd
Hdhdhdhdhd
Hdhdhshdh

OK

I would suggest to study Serial Input Basics to handle this

Yes I had red through it but I did not find anything on using String variable and at least for me it looks easier to use it. I cant use end and start markers because the of the commands I send and data I receive from GSM. That's why I turned for help in here.

If it is easier but does not work, what good is it to you ?

The GSM likely has a \n as end marker, some answers might be multi-line

(You can accumulate data as it comes in a String instance if you don’t want to use a cString but that’s less efficient)

For String based read solutions see my tutorial Arduino Software Solutions
Re 'efficiency', the slowest and most inefficient part of software is the coder, use whatever you find easest.
For Arduino String usage and tips see my tutorial Taming Arduino Strings
Strings are very very safe to use in UNO.

1 Like

For GSM there are often an number of sequences of cmd/response complicated by the delays inbetween. I seem to remember someone has a simple library for handling 'send cmd' wait for response. Any one have the link?

1 Like