Errors in SoftwareSerial output

Hi everyone. Im using SoftSerial to communicate with m590 gsm. Sometimes (only sometimes, mostly everything is OK) i got string like this

P⸮PSTPUM⸮j⸮h)⸮EQUM⸮0,CONNECT,TCP,2047

And sometimes i cant read the whole answer of module

+TCPRECV:0,225,HTTP/1 ength: 3 Content-Type: text/html

(there's should be a message 225 symbols length). I've tried different baudrates, but its always the same arduino reads SS with whis func

String readSerial() { String tmp = ""; while (gsmSerial.available()) tmp += char(gsmSerial.read()); return tmp; }

What is wrong?

What is wrong?

You are using the String class. Stop that.

You only posted a snippet of code. Do NOT do that again.

because "String tmp" is local to readSearial(), once you return from readSerial(), tmp is no longer existed. So you code will work until it doesn't.

arduino_new: because "String tmp" is local to readSearial(), once you return from readSerial(), tmp is no longer existed. So you code will work until it doesn't.

No, it is ok to return classes or structures from a function. The compiler sorts it out for you.

PaulS is probably right about String memory corruption, but it's hard to tell without the whole sketch.

It could also be electrical noise, but it's hard to tell without the schematic and a picture of the setup.

tell me pls, what is the principal difference between functions

String sendAtCmdGetResponce(String cmd, uint32_t timeout)
{
Serial.print("Sending: ");Serial.print(cmd);
uint32_t timeToStop = millis() + timeout;
String answer;
if (cmd != “”) gsmSerial.println(cmd);

answer = “”;
while (millis() < timeToStop)
{
while (gsmSerial.available())
answer += (char)gsmSerial.read();
}
return answer;
}
//////
String readSerial()
{
String tmp = “”;
while (gsmSerial.available())
tmp += char(gsmSerial.read());
return tmp;
}

String sendCmdGetResponce(String cmd, String desiredResponce, unsigned short waitForResponce)
{
String responce;
gsmSerial.println(cmd);
uint32_t timeToStop = millis() + waitForResponce;
while (millis() < timeToStop)
{
responce = readSerial();
if (responce.indexOf(desiredResponce) > -1) return responce;
delay(defaultDelay);
}
return cmd + “: Timeout”;
}

both returns answer with errors P⸮PSTPUM⸮j⸮h)⸮EQUM⸮0,CONNECT,TCP,2047

but first at least writes full server responce

+TCPRECV:0,225,HTTP/1.1 200 OK
Date: Thu, 15 Feb 2018 15:25:57 GMT
Server: Apache/2.2.31 (FreeBSD) mod_fcgid/2.3.9 PHP/5.4.45 mod_ssl/2.2.31 OpenSSL/1.0.2h DAV/2
X-Powered-By: PHP/5.4.45
Content-Length: 3
Content-Type: text/html

833

and no, i wont publish full sketch.

tell me pls, what is the principal difference between functions

String sendAtCmdGetResponce(String cmd, uint32_t timeout) 

String sendCmdGetResponce(String cmd, String desiredResponce, unsigned short waitForResponce)

The second one wastes twice as much memory as the first one.

They both waste memory with the String class.

and no, i wont publish full sketch.

Good luck.

and how u think i should store text variable with unkwnown length?

kossr: and how u think i should store text variable with unkwnown length?

Use a buffer that is big enough.

And if you don't want so show your sketch, either post a simple example that exhibits the behaviour or figure it out yourself.

kossr: and how u think i should store text variable with unkwnown length?

First check that it will never exceed the available memory on your Arduino. Then set aside enough storage to hold that size.

See Resources Serial

replace

tmp += char(gsmSerial.read());

with

of charters read = gsmSerial.readUntil( fill your buffer, length of message expected)

Then you can check the # of characters received if it matches the length of message expected or for errors such as timeout or invalid # of characters received

I guess you don't have enough memory left on board for the code to execute properly. Happens to me all the time.