sms recieving problem

Hey guys whats up, so i made progress with recieving sms, one problem though, the sms string is Testing, i am able to get Te using the code below

char character = 0;
char data[255];
int x=0;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  delay(60000);



}

void loop()
{
Serial1.println("AT+CMGF=1");
Serial1.flush();
Serial1.println("AT+CMGR=1"); //Reads the first SMS
Serial1.flush();
for (x=0;x < 255;x++){ 
data[x]='\0'; 
} 
x=0;
do{
while(Serial1.available()==0);
data[x]=Serial1.read(); 
x++; 
if(data[x-1]==0x0D&&data[x-2]=='"'){
x=0;
}
}
while(!(data[x-1]=='K'&&data[x-2]=='O'));

data[x-3]='\0'; //finish the string before the OK

Serial.println(data); //shows the message

delay(5000);


  
   

}

However, if i take out Serial1.println(“AT+CMGF=1”); and put it into void setup() instead, i will get Testing which is what i want, problem is, i need the entire thing to be a function.Any ideas?

Serial1.flush();

Why? What earthly purpose does it serve to block waiting for all characters to be shifted out?

for (x=0;x < 255;x++){ 
data[x]='\0'; 
}

Fix your tab key.

While your at it, fix your understanding of what a NULL-terminated array means. It does not mean that every element in the array needs to be 0.

do{
while(Serial1.available()==0);
data[x]=Serial1.read(); 
x++; 
if(data[x-1]==0x0D&&data[x-2]=='"'){
x=0;
}
}
while(!(data[x-1]=='K'&&data[x-2]=='O'));

More nonsense. Use the space bar once in a while, and the tab key a lot more often.

i need the entire thing to be a function.Any ideas?

http://arduino.cc/en/Hacking/LibraryTutorial