Receiving a large line by Serial port. Buffer problem

Hello everyone, let see if anyone can help me cause after a week trying different ideas I cant do it by myself :roll_eyes:

I have prepared 2 test Sketches to prepare my code before applying it to my real project.

I need Arduino1 to send a large string or chars, to an Arduino2 by Serial port.
Arduino2 needs to store all the line in a char cArray[200]; and will be later saving it to a SD card.

Here is both codes.

Problem is I believe I am filling in the buffer and not receiving the information fast enough, and in some cases, too fast. So either Arduino2Receiver is busy doing other stuff, or its waiting for Serial information that takes time to arraive.
.
Any help? Thanks a lot everyone !!

Arduino1Sender

//**********************************
    // Send Serial MYSQL
      unsigned long previousmillis_Send_Serial_MYSQL;
      unsigned long interval_Send_Serial_MYSQL = 10000;
//**********************************

    const int Status_LED = 13;
    
void setup()
{
  delay(5000);
  
  // start serial port at 9600 bps:
  Serial.begin(9600);     while (!Serial)  {    ; /* wait for serial port to connect. Needed for Leonardo only */  }
  Serial2.begin(9600);    while (!Serial2) {    ; /* wait for serial port to connect. Needed for Leonardo only */  }
  
  Serial.println();Serial.println();Serial.println(F("STARTING MASTER SYSTEM TESTBENCH SIMULATOR"));
  Serial.print(F("MYSQL data send every: "));  Serial.println(interval_Send_Serial_MYSQL);
  //*****************************
  
  // Visual indication that the Arduino MASTER SYSTEM is resetting...
  for(int i=0; i < 100; i++) { digitalWrite(Status_LED, !digitalRead(Status_LED)); delay(100); }  //delay(10000);
  
  delay(5000);
}





void loop()
{
  digitalWrite(Status_LED, HIGH);   // set the LED on
  
  Send_Serial_MYSQL();
  
  delay(500);                        // Delay to simulate other tasks performed by the main system (SD card saving mainly)

  digitalWrite(Status_LED, LOW);    // set the LED off
  delay(100);                        // Delay to show system is active
}



void Send_Serial_MYSQL()
{
  //************************************************************************************************
  if (millis() - previousmillis_Send_Serial_MYSQL > interval_Send_Serial_MYSQL)
  {
      previousmillis_Send_Serial_MYSQL = millis();
      
    // Using an Arduino MEGA for test. Send it using Serial2
      Serial2.print(F("!INSERT INTO data (B,date_uploaded,temp_box,"));
      delay(100);
      Serial2.print(F("temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      delay(100);
      Serial2.print(F("VALUES ("));
      delay(100);
      Serial2.print(millis());
      Serial2.print(F(",2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.00,458.19,1.99,133.9)"));

    // Show it also in the Serial monitor
      Serial.print(F("!INSERT INTO data (B,date_uploaded,temp_box,"));
      delay(100);
      Serial.print(F("temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      delay(100);
      Serial.print(F("VALUES ("));
      delay(100);
      Serial.print(millis());
      Serial.print(F(",2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.00,458.19,1.99,133.9)#"));
      Serial.println();
  }
  //************************************************************************************************
}

Arduino2Receiver

  char foo;    // http://arduino.cc/forum/index.php?topic=84412.0

#include <SoftwareSerial.h>
    SoftwareSerial mySerial(7, 8); // RX, TX

//**************************** SERIAL *******************************************
    boolean stringComplete    = false;   // whether the string is complete
     //Character array used to convert recieved serial characters to floats.
          char cArray[200];
          int ic=0;
//**************************** SERIAL *******************************************


    const int Status_LED = 8;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);   while (!Serial) {    ; /* wait for serial port to connect. Needed for Leonardo only  */  }
  
  delay(5000);
  Serial.println();Serial.println(F("STARTING LOGGING"));
  delay(5000);

  pinMode(Status_LED,        OUTPUT);

  //********************
  mySerial.begin(9600);
}



void loop()
{
  //do other stuff
  delay(500);
  
  serialEvent();

  digitalWrite(Status_LED, !digitalRead(Status_LED));
}

 
void serialEvent() 
{
   char inChar;
   
   if (mySerial.available() > 0)
   {
     Serial.println();Serial.println(F("Receiving..."));
     
                   while (mySerial.available()) {
                  
                   // get the new byte:
                   inChar = (char)mySerial.read(); 
                    
                    Serial.print(inChar);

                   //add to character array
                   cArray[ic] = inChar; ic++;
              
                  // if the incoming character is a newline, set a flag
                  // so the main loop can do something about it:
                  if (inChar == '#')  {      stringComplete = true;  } // if (inChar == '\n') {      stringComplete = true;  } 
                  
                  // once all data is collected...
                  if (stringComplete == true)
                    {
                      Serial.println(F("******** String Complete ********"));
                       ic=0;
                       // Print the string
                       for(int i=0; i<200; i++) cArray[i] = 0;
                       Serial.println();
                    }
                 }
  }
}

Receiver output

STARTING LOGGING and WATCHDOG PROGRAM

Receiving...
!INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,hu
Receiving...
VALUES (593790,2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.00
Receiving...
!INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,hu
Receiving...
!INSERT INTO d

I attach the files also, for easy view.
I dont know how to send and receive such a large text.

Sender.ino (2.75 KB)

Receiver.ino (2.4 KB)

In your half second delay, your 64 byte buffer will overflow seven times.
Get rid of the delay.

    String Serial_Data_String = "";      // a string to hold incoming data    //String inputString = "";

What's this for? You never use it.

The delay() on EVERY pass through loop() isn't helping. What the hell is that there for?

Printing every single character received is going to cut the effective read rate in half.

hello AWOL, thanks.

I left that half a second delay to "simulate" when the Arduino2Receiver is saving data to the SD card and therefore not being able to do anything else. I just made this two test sketches to simplify everything before adding other code (SD saving).

I guess my objective is to come up with a SerialEvent() which handles better the information that arrives through the serial port. The problem, I dont know how to coupe with overflow.
while (serial.available()) sometimes does not kick in and misses the rest of the serial i think

PaulS:

    String Serial_Data_String = "";      // a string to hold incoming data    //String inputString = "";

What's this for? You never use it.

The delay() on EVERY pass through loop() isn't helping. What the hell is that there for?

Printing every single character received is going to cut the effective read rate in half.

Sorry that was removed now, it was from previous tests. sorry for the misunderstanding.

I need to consider the ArduinoReceiver will be busy every 10 seconds writing information to the SD card. I dont know how to code the Serial function to receive the full line. it always get cut at different places.

is it a good idea to break the line i am sending in different parts? like i did with the 100 ms delay? if not, how to send such a large line?

The problem, I dont know how to coupe with overflow.

The way to cope with overflow is to not let it happen. You need to have the receiver tell the sender that it is ready to receive. Obviously, you won't do that in the middle of a write to the SD card.

When the sender gets the send message, it can stream out all 200 characters. The receiver can receive them faster than they can be sent. Only after all 200 have (or the end of packet marker has) arrived should you write to the SD card.

Thanks Paul sounds very good. My brain got blocked after so many tries and needed new ideas.

Would it be doable, that the Sender sends whenever it wants? I really didnt want the Sender to have to be checking for authorization. any ideas? This would allow me to use only 2 wires (ground + Tx).

Would it be doable, that the Sender sends whenever it wants?

If you don't mind missing the occasional packet while writing to the SD card, yes.

Using start and end markers on the packets makes it easy to skip partial packets, if an overflow occurs while you are writing.

For now I prefer receiving a string correctely, saving it, and then receive the next one.

(I delete my previous message as I found a big error I was making)
Its starting to improve, but cant get full perfect reception yet…

//**********************************
    // Send Serial MYSQL
      unsigned long previousmillis_Send_Serial_MYSQL;
      unsigned long interval_Send_Serial_MYSQL = 5000;
//**********************************

    const int Status_LED = 13;
    
void setup()
{
  delay(5000);
  
  // start serial port at 9600 bps:
  Serial.begin(9600);     while (!Serial)  {    ; /* wait for serial port to connect. Needed for Leonardo only */  }
  Serial2.begin(9600);    while (!Serial2) {    ; /* wait for serial port to connect. Needed for Leonardo only */  }
  
  Serial.println();Serial.println();Serial.println(F("STARTING MASTER SYSTEM TESTBENCH SIMULATOR"));
  Serial.print(F("MYSQL data send every: "));  Serial.println(interval_Send_Serial_MYSQL);
  //*****************************
  
  // Visual indication that the Arduino MASTER SYSTEM is resetting...
  for(int i=0; i < 100; i++) { digitalWrite(Status_LED, !digitalRead(Status_LED)); delay(100); }  //delay(10000);
  
  delay(5000);
}





void loop()
{
  digitalWrite(Status_LED, HIGH);   // set the LED on
  
  Send_Serial_MYSQL();
  
  delay(500);                        // Delay to simulate other tasks performed by the main system

  digitalWrite(Status_LED, LOW);    // set the LED off
  delay(100);                        // Delay to show system is active
}



void Send_Serial_MYSQL()
{
  //************************************************************************************************
  if (millis() - previousmillis_Send_Serial_MYSQL > interval_Send_Serial_MYSQL)
  {
      previousmillis_Send_Serial_MYSQL = millis();
      
    // Using an Arduino MEGA for test. Send it using Serial2
      Serial2.print(F("!INSERT INTO data (B,date_uploaded,temp_box,"));
      Serial2.print(F("temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      Serial2.print(F("VALUES ("));
      Serial2.print(millis());
      Serial2.print(F(",2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.00,458.19,1.99,133.9)$"));

    // Show it also in the Serial monitor
      Serial.print(F("!INSERT INTO data (B,date_uploaded,temp_box,"));
      Serial.print(F("temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      Serial.print(F("VALUES ("));
      Serial.print(millis());
      Serial.print(F(",2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.00,458.19,1.99,133.9)$"));
      Serial.println();
  }
  //************************************************************************************************
}

Receiver

  char foo;    // http://arduino.cc/forum/index.php?topic=84412.0

#include <SoftwareSerial.h>
    SoftwareSerial mySerial(7, 8); // RX, TX

//**************************** SERIAL *******************************************
    boolean stringComplete    = false;   // whether the string is complete
     //Character array used to convert recieved serial characters to floats.
       const int ARRAYSIZE = 300;
          char cArray[ARRAYSIZE];
          int ic=0;
//**************************** SERIAL *******************************************

  unsigned long previousmillis_lastSDwrite;
  unsigned long interval_SDwrite = 20000;
  
    const int Status_LED = 8;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);   while (!Serial) {    ; /* wait for serial port to connect. Needed for Leonardo only  */  }
  
  delay(5000);
  Serial.println();Serial.println(F("STARTING LOGGING and WATCHDOG PROGRAM"));
  delay(5000);

  pinMode(Status_LED,        OUTPUT);

  //********************
  mySerial.begin(9600);
}



void loop()
{
  //do other stuff
  if (millis() - previousmillis_lastSDwrite > interval_SDwrite)
  {
    // Write data so SD
    delay(500);
  }
  
  serialEvent();

  digitalWrite(Status_LED, !digitalRead(Status_LED));
}

 
void serialEvent() 
{
   char inChar;
   
   if (mySerial.available() > 0)
   {
      while (mySerial.available()) {
                  
                   // get the new byte:
                   inChar = (char)mySerial.read(); 
                    
                   // Serial.print(inChar);

                  
                  // Start
                   if (inChar == '!')
                         {
                          Serial.println();Serial.println(F("Receiving..."));
                          ic=0;
                         }
                         
                    //add to character array
                   cArray[ic] = inChar; ic++;
              
              
                  // if the incoming character is a newline, set a flag
                  // so the main loop can do something about it:
                  if (inChar == '

Result:

STARTING LOGGING and WATCHDOG PROGRAM

Receiving...
####
******** String Complete ********
!INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) VALUES (1290972,2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.         00,458.19,1.99,133.9)$

Receiving...

Receiving...
####
******** String Complete ********
!INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) VALUES (1301142,2013-09-28 00:17:22,27.69,25.94,25.00,41.00,3.00,458.19,1.99,133.9)$

Receiving...

Receiving...

Receiving...

Receiving...
####
******** String Complete ********
!INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,hu02.6925.94,25.00,41.00,3.00,458.19,1.99,133.9)$

Sender.ino (2.43 KB)

Receiver.ino (2.76 KB))  {  Serial.println("####");    stringComplete = true;  }
                 
                 // once all data is collected…
                 if (stringComplete == true)
                   {
                     Serial.println(F("******** String Complete ********"));
                      ic=0;
                      // Print the string
                      for(int i=0; i<ARRAYSIZE; i++) Serial.print(cArray[i]);
                      Serial.println();
                      stringComplete = false;
                      // Clear the Array
                      for(int i=0; i<ARRAYSIZE; i++) cArray[i] = 0;
                   }
                }
     }
   }


Result:

§DISCOURSE_HOISTED_CODE_2§


[Sender.ino|attachment](upload://s2UqyCi0DTrUPJe0JCFK8aAvXJ7.ino) (2.43 KB)

[Receiver.ino|attachment](upload://tWzEBnJDYc73DM0VRl00xErVLKU.ino) (2.76 KB)

Why are you using only 9600 baud? You could go at least 6 times that fast.

Get rid of the artificial delay(). Don’t even think about reintroducing that until you understand the communications problem.

You send some data. The receiver reads the first character. Let’s suppose that that IS a !. You read all the data that has arrived so far. That might be 2 or 3 more characters, but is far more likely to be none. Then, the function ends.

It gets called again, and reads one character. That character isn’t a !, so the function ends. And repeats.

Try some code like this:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[200];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Thanks a lot Paul for your time !

I guess I need to modify this line? why do this check? with this check nothing happens.

  if(index < 79)

I really like the idea of the ‚Äústarted‚ÄĚ and ‚Äúended‚ÄĚ flags. as well as the chained if condition. makes so much sense now to have it.

regarding the 9600 speed, I just thought it would be better to keep it slower than faster, but I understand I was wrong.

any idea why it might not be liking 57600 speed?

<¬©¬ß¬©!*"√äJy"?√°√ā@?¬°?¬≤X¬Ĺ¬¨U√Ā√Ď√ě√ā¬ī¬≤¬≤?:¬Ļ-¬ģ%¬Ĺ√°X√®e¬Ĺ¬ł¬Į¬®-,??√ē√†?¬†¬™?¬ī]+()?√©?√Ö√®√ź¬ĶWw?√©√ęb?	)

Sender. Arduino MEGA. using Serial2 at 57600
Receiver. Arduino NANO. using softwareSerial at 57600 (basic example with arduino ide)

I guess I need to modify this line?

Yes. That should be 199 for an array size of 200.

No idea about the baud rate issue.

Ive added this delay at the middle of the Sending and seems to help on not loosing data when using the SoftwareSerial basic example

      Serial2.print(F("<INSERT INTO data (B,date_uploaded,temp_box,"));
      Serial2.print(F("temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      delay(100);
      Serial2.print(F("VALUES ("));
      Serial2.print(millis());
      Serial2.print(F(",2013-09-28 11:22:33,44.55,66.77,88.99,00.11,2.33,444.55,6.77,888.9)>"));

At 57600 is still showing wierd characters. I have changed the speed at both arduinos, I even put the Serial debug all at 57600 including the serial monitor window

edit…
thanks Paul. I left it as…

      if(index < ARRAYSIZE-1)

The baudrate still getting me. until i dont fix this I presume its impossible to get the code working.

douh

 while(Serial.available() > 0)
 while(mySerial.available() > 0)

I cant get to receive a perfect line, therefore the code does not return anything. I see it always gets stuck around index 70 or 74 when receiving data. overflow???

The longer I make the delay, the few more characters I can receive. obviously not the solution but maybe helps to find the solution?
Running at 38400

  int delay_t = 300;
    // Using an Arduino MEGA for test. Send it using Serial2
      Serial2.print(F("<INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT"));
      delay(delay_t);
      Serial2.print(F(",humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      delay(delay_t);
      Serial2.print(F("VALUES ("));
      Serial2.print(millis());
      Serial2.print(F(",2013-09-28 11:22:33,44.55,66.77,88.99,00.11,2.33,444.55,6.77,888.9)>"));
void serialEvent() 
{
  // Read all serial data available, as fast as possible
  while(mySerial.available() > 0)
  {
    char inChar = mySerial.read();
    //Serial.write(inChar);
    if(inChar == SOP)
    {
      Serial.println("Starting");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
      Serial.println("Ending");
       ended = true;
       break;
    }
    else
    {
     // Serial.println("Data");
      if(index < ARRAYSIZE-1)
      {
          // Ive tried with the printing disabled, but it only keeps writting "Starting".
        Serial.print("Saving: ");Serial.print(inChar); Serial.print(" index ");Serial.println(index);
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
      else { Serial.println("array overflow");}
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
              // Print the string
              for(int i=0; i<index; i++) Serial.print(inData[i]);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Starting
Saving: I index 0
Saving: N index 1
Saving: S index 2
Saving: E index 3
Saving: R index 4
Saving: T index 5
Saving: index 6
Saving: I index 7
Saving: N index 8
Saving: T index 9
Saving: O index 10
Saving: index 11
Saving: d index 12
Saving: a index 13
Saving: t index 14
Saving: a index 15
Saving: index 16
Saving: ( index 17
Saving: B index 18
Saving: , index 19
Saving: d index 20
Saving: a index 21
Saving: t index 22
Saving: e index 23
Saving: _ index 24
Saving: u index 25
Saving: p index 26
Saving: l index 27
Saving: o index 28
Saving: a index 29
Saving: d index 30
Saving: e index 31
Saving: d index 32
Saving: , index 33
Saving: t index 34
Saving: e index 35
Saving: m index 36
Saving: p index 37
Saving: _ index 38
Saving: b index 39
Saving: o index 40
Saving: x index 41
Saving: , index 42
Saving: t index 43
Saving: e index 44
Saving: m index 45
Saving: p index 46
Saving: _ index 47
Saving: a index 48
Saving: m index 49
Saving: b index 50
Saving: , index 51
Saving: t index 52
Saving: e index 53
Saving: m index 54
Saving: p index 55
Saving: D index 56
Saving: H index 57
Saving: T index 58
Saving: , index 59
Saving: h index 60
Saving: u index 61
Saving: m index 62
Saving: D index 63
Saving: H index 64
Saving: T index 65
Saving: , index 66
Saving: w index 67
Saving: e index 68
Saving: a index 69
Saving: t index 70
Saving: h index 71
Saving: e index 72
Saving: r index 73
Saving: _ index 74
Saving: s index 75
Saving: t index 76
Saving: a index 77
Saving: t index 78
Saving: u index 79
Saving: V index 80
Saving: A index 81
Saving: L index 82
Saving: U index 83
Saving: E index 84
Saving: S index 85
Saving: index 86
Saving: ( index 87
Saving: 1 index 88
Saving: 5 index 89
Saving: 4 index 90
Saving: 1 index 91
Saving: 4 index 92
Saving: 5 index 93

Sending 19 characters back for every one received is killing you. Stop it. At most print one character.

PaulS:
Sending 19 characters back for every one received is killing you. Stop it. At most print one character.

I just did the printing to see if i could figure out what was happening. if I dont print I only get...

STARTING LOGGING and WATCHDOG PROGRAM
Starting
Starting
Starting
Starting

Thank you for keep trying to help me, I know we are close, but cant make it fully work.

IT’S WORKING !!

I splitted the string into much shorter ones, and added a delay after each of them. Seems to be working now. I will update the post with final results later…

void Send_Serial_MYSQL()
{
  //************************************************************************************************
  if (millis() - previousmillis_Send_Serial_MYSQL > interval_Send_Serial_MYSQL)
  {
      previousmillis_Send_Serial_MYSQL = millis();
      int delay_t = 100;
    // Using an Arduino MEGA for test. Send it using Serial2
      Serial2.print(F("<INSERT INTO data (B,date_uploaded,"));
      Serial2.print(F("temp_box,temp_amb,tempDHT"));
      delay(delay_t);
      Serial2.print(F(",humDHT,weather_status,Apparent_power,"));
      Serial2.print(F("Irms,ArduinoMeter) "));
      delay(delay_t);
      Serial2.print(F("VALUES ("));
      delay(delay_t);
      Serial2.print(millis());
      delay(delay_t);
      Serial2.print(F(",2013-09-28 11:22:33,44.55,66.77,"));
      delay(delay_t);
      Serial2.print(F("88.99,00.11,2.33,444.55,6.77,888.9)>"));
      
    // Show it also in the Serial monitor
      Serial.print(F("<INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT"));
      Serial.print(F(",humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) "));
      Serial.print(F("VALUES ("));
      Serial.print(millis());
      Serial.print(F(",2013-09-28 11:22:33,44.55,66.77,88.99,00.11,2.33,444.55,6.77,888.9)>"));
      Serial.println();
  }
  //************************************************************************************************
}

Receiver:

void serialEvent() 
{
  // Read all serial data available, as fast as possible
  while(mySerial.available() > 0)
  {
    char inChar = mySerial.read();
    //Serial.write(inChar);
    if(inChar == SOP)
    {
      Serial.println("Starting");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
      Serial.println("Ending");
       ended = true;
       break;
    }
    else
    {
     // Serial.println("Data");
      if(index < ARRAYSIZE-1)
      {
       // Serial.print("Saving: ");Serial.print(inChar); Serial.print(" index ");Serial.println(index);
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
      else { Serial.println("array overflow");}
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
              // Print the string
              for(int i=0; i<index; i++) Serial.print(inData[i]);
              Serial.print(" Index: ");Serial.println(index);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Starting
Ending
INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) VALUES (127083,2013-09-28 11:22:33,44.55,66.77,88.99,00.11,2.33,444.55,6.77,888.9) Index: 198
Starting
Ending
INSERT INTO data (B,date_uploaded,temp_box,temp_amb,tempDHT,humDHT,weather_status,Apparent_power,Irms,ArduinoMeter) VALUES (132529,2013-09-28 11:22:33,44.55,66.77,88.99,00.11,2.33,444.55,6.77,888.9) Index: 198

Thank you so much everyone.
If I confirm its all working good I will post the full sketch with comments for future reference. Then I will try to add the SD card saving function.
THANKS !!