communication errors with software serial

I have an Arduino Ethernet board with an Arduino GSM Shield on top. It appears that communication errors occur in using this combination. For testing I used the Send SMS example from the library page.

/*
SMS sender

 This sketch, for the Arduino GSM shield,sends an SMS message 
 that you send it through the serial monitor. To make it work, 
 open the serial monitor, and when you see the READY message, 
 type a message to send. Make sure the serial monitor is set
 to send a newline when you press return.

 Circuit:
 * GSM shield 

 created 25 Feb 2012
 by Tom Igoe

 This example is in the public domain.
 */

// libraries
#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

void setup()
{
  // initialize serial communications
  Serial.begin(9600);

  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
}

void loop()
{

  Serial.print("Enter a mobile number: ");
  char remoteNumber[20];  // telephone number to send sms
  readSerial(remoteNumber);
  Serial.println(remoteNumber);

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");
}

/*
  Read input serial
 */
int readSerial(char result[])
{
  int i = 0;
  while(1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if(inChar!='\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}

A few tries only

SMS Messages Sender

was displayed, probably because communication failed right away.

Sometimes I was able to send a message, but only once. This was one of the results (I crossed out my phone number):

SMS Messages Sender
GSM initialized
Enter a mobile number: 06********
Now, enter SMS content: SENDING

Message:
goedemiddag

CO
ª?UT$¡H¨Ô?W?.¤XHé?þ

and another one:

SMS Messages Sender
GSM initialized
Enter a mobile number: 06********
Now, enter SMS content: SENDING

Message:
goedemiddag

COMPLETE!

Enter a mobP?«¹Õµ??Éé?þ

Especially the last parts demonstrates the communication failure:

CO
ª?UT$¡H¨Ô?W?.¤XHé?þ

and

Enter a mobP?«¹Õµ??Éé?þ

The messages where received ok.
What can be the cause and how to solve this?

Why do you think the errors are related to software serial? Why are you mounting a Wireless shield on a wired device?

How much memory do you have available?
http://playground.arduino.cc/Code/AvailableMemory

(Not enough would be my guess.)

      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }

Why are you calling Serial.flush()? Do you have a clue what it does?

Why do you think the errors are related to software serial?

It's a guess, that is why I put the question here in the hope to get an answer and may be a solution!

Why are you mounting a Wireless shield on a wired device

I am not. It is a GSM shield.

How much memory do you have available?

I will look into that. But I cannot explain why not enough memory causes failure sometimes after a few lines of code and sometimes after one loop. You would expect when running out of memory failure occurs at the same point.
More over I am using a standard board and a standard shield (both Arduino), with an example (unaltered) from the library page. May I expect that these three things work together?

Why are you calling Serial.flush()? Do you have a clue what it does?

Yes, I do have a clue. As mentioned I used the example written by Tom Igoe from the Arduino website.

I am not. It is a GSM shield.

You have a GSM shield that has wires? WTF are they for?

You would expect when running out of memory failure occurs at the same point.

There you are wrong. I expect undefined behavior when running out of memory. YOU expect the failure to occur consistently.

Yes, I do have a clue.

OK, Mr. SmartyPants. What does it do? Just because it was in some old code doesn't mean it is right. And, I can assure you that neither the old meaning of flush() or the new meaning of flush() is useful at that point. Mr. Igoe may be a pioneer in the Arduino field, but he is NOT a programmer, and some of his examples are crap.

You have a GSM shield that has wires?

I think it is beyond the scope of the subject to comment on my choice of boards unless you can point me to the page where it is stated that they are not compatible. But to make things clear: the project I am working on, monitors a burglar alarm, sends out (wired) once a day a status report by mail and when an alarm condition occurs sends out a SMS. That's the goal anyway and that is why I selected these boards.
To test the hardware in combination with the GSM library, I used an example from the website. I can not imagine that I am the only one out here with this combination of boards and the only one facing the reported problem.

There you are wrong. I expect undefined behavior when running out of memory.

I don't agree. I also expect undefined behavior when running out of memory, but that should occur after the same number of program lines. And that is not wat is happening.

Mr. Igoe may be a pioneer in the Arduino field, but he is NOT a programmer, and some of his examples are crap.

So when can the Arduino community expect your proposal for an improved example?????

but that should occur after the same number of program lines.

Dream on.

I did some further troubleshooting, I posted a new thread in the GSM Shield part of the forum.

A helpfull response to that is appreciated!