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);
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!
source share