Why all the information in serial is not read?

I'm using a GPRS shield, communication between Arduino and the shield is by serial commands via my question is this when I make a request to a web server and to read what is sending me the shield I do it this way:

while(gsm.available()>0) { ** Serial.write(gsm.read());** ** contador++;** }

And shows me the result, which is the http header and page content, accountant gives me a while value after about 300 approx. Now what is on the page I want it processed in arduino but when you get the result and store it, do not read everything the shield:

byte primero[400]; while(gsm.available()>0) { ** primero[contador]=(gsm.read());** ** contador++;** } Sometimes counter has a value of 4, other than 10, other than 1. What's going on? Because not read all the information (?)

start counter at 0

Post your complete code, or at least an example that demonstrates the problem.

When you get to the code that reads the data from Serial, how do you know all of the data has arrived? Arduino can read serial data a lot faster than it is transmitted. So if you call that code in the middle of the transmission, it will read everything received so far and assume that it is done.

It is better to include start of packet and end of packet markers with your serial transmission. Just any character that you can use to let you know where the start and end of the data packet is. For example, I like to use the < and > characters. Then I keep reading from serial until I see that > character and I know I have the complete transmission.

The 3rd example in serial input basics illustrates how to use start- and end-markers. It uses <> but you can easily change them.


The GSM device is pushing out data, perhaps sporadically, perhaps all the time,
if your code runs ahead of this stream available() will return zero - all that means
is you are reading faster than the data is coming in, which in general will happen

You need to decide a logical point to break out of listening, perhaps at a natural
line break or other separator character. So you code would be:

void loop ()
  if (gsm.available () > 0)
    char ch = gsm.read () ;
    if (ch == '\n')
      primero[contador] = 0 ;  // NUL terminator
      process () ;   // handle the data so far
      contador = 0 ; // reset for next string.
    else if (contador < 400-1)
      primero[contador++] = ch ;

When I use this code works and read all


But when using this other code not read all

byte primero[400];

This is my code

#include “SIM900.h”
#include <SoftwareSerial.h>
#include “inetGSM.h”

//To change pins for Software Serial, use the two lines in GSM.cpp.
//GSM Shield for Arduino
//this code is based on the example of Arduino Labs.
//Simple sketch to start a connection as client.
InetGSM inet;

char msg[300];
char respuesta[300];
int numdata;
char inSerial[50];
int i=0;
String json="";
int index=0;
char caracter;
boolean started=false;
byte primero[1000];
int contador=0;

void setup()
//Serial connection.
Serial.println(“GSM Shield testing.”);
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(4800)) {
} else Serial.println("\nstatus=IDLE");
if(started) {
//GPRS attach, put in order APN, username and password.
//If no needed auth let them blank.
if (inet.attachGPRS(“internet.itelcel.com”,“webgprs”,“webgprs2003”))

//Read IP address.
//Read until serial buffer is empty.

//TCP Client GET, send a GET request to the server and
//save the reply.
numdata=inet.httpGET(“www.transporte-organos.16mb.com”, 80, “/_ctrl/ctrl.arduino.php?comando=obtener&id_contenedor=1”, msg,300);

//Print the results.
Serial.println("\nNumber of data received:");
Serial.println("\nData received:");
Serial.println(“Leyendo del serial gsm =>”);
String cadena="";
unsigned char arreglo[500];
int contador=0;
byte primero[400];


Serial.println(“Valor de primero=>”);
for(int i=0;i<contador;i++)

  • }*
    void loop()

(1) You don't check for more than 400 characters, so you overwrite the end of your array.

(2) The while( ) loop near the end of your setup( ) function will run faster than the GSM device will supply serial data. When your program catches up to all the data already received, there will be no data available, the while loop will end, and the setup( ) function will complete running once, and your program will do no more.

(3) If your setup( ) function runs before the data is available, it will get no data, and setup( ) will end. setup( ) only runs ONCE.

Suggested solution:

Move all of the serial reading activity to loop( ). If you have processed all the data available at any particular instant in time, loop( ) will run again to get the rest of the data later, and keep running until you have all of the data.