Serial data getting cut off

I have my arduino talking to an electric imp over software serial and am having an issue with the serial buffer aparently being empty after the 3rd character and the communication being terminated before the arduino recieves all of it's instructions half the time. It ends up picking up the rest on the next time through the loop.

The instructions it's recieving are two parameters that are comma delimited and need to be parsed. See my code below. Anyone know what I can do to fix this?

void loop() // run over and over
{  
  String string1 = "";
  String string2 = "";
  
  int index = 0;
  
  // Send data from the software serial
  if (impSerial.available()){
    while(impSerial.available()>0)
    {
      char c = impSerial.read();
    
      if(c == ',')
      {
        index +=1;
      }
      else
      {
        if(index == 0)
        {
          string1 += c;
        }
        else
        {
          string2 += c;
        }
      }}
    Serial.println("String1 = " + string1 );
    Serial.println("String2 = " + string2);
}
}

smahoo:
I have my arduino talking to an electric imp over software serial and am having an issue with the serial buffer aparently being empty after the 3rd character and the communication being terminated before the arduino recieves all of it's instructions half the time. It ends up picking up the rest on the next time through the loop.

The instructions it's recieving are two parameters that are comma delimited and need to be parsed. See my code below. Anyone know what I can do to fix this?

Yes. You can read all your characters instead of assuming they are all available just because more than 0 of them are.
Serial data comes in a a very slow pace, compared to the cycle time of the Arduino. Even at 115200 bps, a character takes a LONG time to arrive (658 microseconds or so), while the Arduino merrily rambles along at 62.5 nanoseconds , so the Arduino can do over 10 thousand single-cycle instructions while that character comes in.

Have a look at Nick Gammon's excellent tutorial at Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

That ought to do the trick, thanks!

Avoid using the String class, it uses too much memory and may have bugs.