Serial Comunnication

i amy writing this code to get uart data from solar controller and i want to to form a single string from the incoming data bits. i get one stream of data per minute. i am having trouble in forming one single string which then i can manipulate and extract certain portions to use.

This is the code used.

"void loop()
{ String data;

if(Serial1.available() >0){
for(int n=0; n<96; n++){
data += char(Serial1.read());
}
Serial.print(data);

}"

I got it to work with software serial but when i use Serial1 of Mega. I get a timing issue. the string formed has lots of empty strings. Why is that? How can i fix it?

Why you don’t something like:

  if(Serial1.available() >=96){
 for(int n=0; n<96; n++){
  data += char(Serial1.read());
 }
if(Serial1.available() >0){
 for(int n=0; n<96; n++){
  data += char(Serial1.read());

You check to see that there is at least one character to read, then you go right ahead and read all 96 of them.
Nope.

Does the device send an end of data delimiter/marker? If not, below is a very simple serial receiving setup.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}

Have a look at the 3 different examples in serial input basics. I suspect one of them will be suitable, or very nearly.

...R

luisilva:
Why you don’t something like:

  if(Serial1.available() >=96){

for(int n=0; n<96; n++){
  data += char(Serial1.read());
}

Because the input buffer is 32 bytes too small to allow you to do this.