Very odd behavior of Serial stream

The Arduino is faster than you imagine.

while (!Serial1.find("+CUSD:"));
At this point the ':' of "+CUSD:" has just been taken out of the buffer. At 57600 baud, it will be about 174 microseconds (2777 instruction cycles) until the next character arrives.

The buffer is empty so this loop does nothing:

  while (Serial1.available()) 
    Serial.write(Serial1.read());

This line puts "end\n" in the Serial output buffer.
Serial.println("end");

send_sms() now returns.

Now we are back in loop() and start repeating:

  while ( Serial1.available() ) {
    Serial.write( Serial1.read() );
  }

That will (eventually) show everything that arrived after "end\n" was put in the output buffer.

1 Like