Sequential overflow in Arduino

When reading SMS using the Arduino program, only senders are displayed on the serial monitor (no date or time) and the message is truncated. This could be due to sequential overflow, a common problem in Arduino.

Code:

#include <SoftwareSerial.h> #include <String.h> SoftwareSerial mySerial(7, 8); void setup() { mySerial.begin(9600); Serial.begin(9600); } void loop() { mySerial.print("AT+CMGR=1\r"); delay(100); while(mySerial.available()) Serial.write(mySerial.read()); delay(1000); } 

Output

 AT+CMGR=1 +CMGR: "REC READ","+XXXXX","A Silky Soni","1AT+CMGR=1 +CMGR: "REC READ","+XXXXX","A Silky Soni","1AT+CMGR=1 +CMGR: "REC READ","+XXXXX","A Silky Soni","1AT+CMGR=1 
+4
source share
2 answers

You may need to adjust the bit rate in the code according to the GSM screen.

+1
source

This is not a baud rate because the string is legible, so all bits are in the correct position, however the string is truncated.

This is because the output buffer in mySerial takes more time to fill up with the GSM screen than the time to be depleted by the mySerial.read() command. What it is: when mySerial.available() checked, and the GSM screen does not have time to insert anything into the output buffer, the result is a failure of the while . There are several ways to solve this problem:

Put a delay() with a specific time inside while :

 void loop() { mySerial.print("AT+CMGR=1\r"); delay(100); while(mySerial.available()){ Serial.write(mySerial.read()); delay(100); //fix the time according to how fast the GSM shield //wrote the data in the serial port. } delay(1000); } 

Or use a timeout:

 unsigned long init_time, timeout=500;//choose the correct timeout value void loop() { mySerial.print("AT+CMGR=1\r"); //delay(100); //this delay can be omitted init_time=millis(); do{ while(mySerial.available()){ Serial.write(mySerial.read()); init_time=millis(); } }while(millis()-init_time < timeout); delay(1000); } 

millis()-init_time indicate the time elapsed since the last return mySerial.available() true or to check if it was not available. The code will still check availability until it reaches the wait limit.

I suggest the last approach. Happy coding!

0
source

All Articles