Sending long string via harware serial.

Hello,

I am trying to send the following string:

HardwareSerial Uart = HardwareSerial();

...

Uart.println("AT+HTTPPARA=\"URL\",\"http://123.456.123.456/system/ping.php?id=1&data=1;1000;2000;1;0;1;2;0;0;0;1;1000;2000;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;1;1;0;1;2;0;0;0;1;1000;2000;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;");

But the Uart doesnt pass all the data and truncate the string. however the Serial.println does work perfectly. the TX buffer is very big (I changed it to 512 or 1024), anybody has a solution to this problem?

thanks

Don't keep the rest of your program a secret, post it here.

How did you change the size of the TX buffer ? Which Arduino board are you using ?

anybody has a solution to this problem?

Put stupidly long constant strings in program memory

the TX buffer is very big (I changed it to 512 or 1024),

If you are using 1.0+, the receive buffer is the same size. At 1024, on a 328 based machine, your buffers are occupying all the memory, not allowing for any function calls, any local variable, or any literal constants, like the string you are trying to send.

I want to send lots of variables that all of them are changing, I am using Teensy 2++ (and before I used mega where that also happened) so I dont actually clog the memory.

I changed the buffer size at HardwareSerial.cpp (I should have mentioned it is compiling the teensy lib) at arduino-1.0.4\hardware\teensy\cores\teensy\ where i use #define TX_BUFFER_SIZE 1024.

do you have other ideas for sending lots of data (I have about 30 integers) over sim900 gprs? I am sending them every 10 seconds or so.

But the Uart doesnt pass all the data and truncate the string.

How do you know this? What are you passing the data TO? Perhaps the issue is on that end.

That is possible, I will connect it to another arduino and do some testings, anyway the device is sim900.

srusha: the TX buffer is very big (I changed it to 512 or 1024)

Why did you change it?

I checked that and the arduino is actually sending the variables properly, so I now have to find a different approach..

I am using sim900, How can I send a long command to it? maybe separate it to two commands? I tried print("string"); delay(500); print("second part of string"); and it didn't work well..

any ideas?

I tried print("string"); delay(500); print("second part of string"); and it didn't work well..

any ideas?

Well, posting your code and defining what actually happened come to mind.

I dont see how posting the entire code would help in this particular case but…

int8_t sendData(){
  char* expected_answer1="OK";
unsigned int  timeout=5000;
int i;
  uint8_t x=0,  answer=0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while( Uart.available() > 0) Uart.read();    // Clean the input buffer





Uart.print("AT+HTTPPARA=\"URL\",\"http://212.1.208.94/system/ping.php?id=1&data");
delay(500);
Uart.print("=1;1000;2000;1;0;1;2;0;0;0;1;1000;2000;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;1;1;0;1;2;0;0;0;1;1000;2000;1;0");
delay(500);
Uart.println(";1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;\"");


    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Uart.available() != 0){    
      response[x] = Uart.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)    
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));    
  Serial.println(response);
  return answer;
}

I dont see how posting the entire code would help in this particular case

... so you're not going to?

sorry :blush:, here is the rest

#include <Time.h>
#include <Streaming.h>

time_t t;


int8_t answer;
int onModulePin= 9;
HardwareSerial Uart = HardwareSerial();
char data[512];
int data_size;

char aux_str[100];
char aux;
int x = 0;


void setup(){
  Uart.begin(19200); 
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);   

  Serial.println("Starting...");
  power_on();

  delay(3000);
sendATcommand("AT+IPR=19200","ok",2000);

  while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);

  //    answer = sendATcommand("AT+IPR=?", "OK", 10000);
  sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
  sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet\"", "OK", 2000);
  //    sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
  //    sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
  answer= sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,1", 2000);
  if(answer!=1)
    while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
    {
      delay(5000);
    }


}
void loop(){
  // Initializes HTTP service

  answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
  if (answer == 1)
  {
    // Sets CID parameter
    answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
    if (answer == 1)
    {
      // Sets url 
      answer = sendData();
      if (answer == 1)
      {
        // Starts GET action
        answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:1,200", 10000);

        if (answer == 1)
        {
          x=0;

          sprintf(aux_str, "AT+HTTPREAD=%d,100", x);
          if (sendATcommand2(aux_str, "+HTTPREAD:", "ERROR", 30000) == 1)
          {
            data_size = 0;
            while(Uart.available()==0);
            aux = Uart.read();
            do{
              data_size *= 10;
              data_size += (aux-0x30);
              while(Uart.available()==0);
              aux = Uart.read();        
              Serial.print(aux);
            }
            while(aux != 0x0D);


            if (data_size > 0)
            {
              while(Uart.available() <= data_size);
              Uart.read();

              for (int y = 0; y < data_size; y++)
              {
                data[x] = Uart.read();
                x++;
              }
              data[x] = '\0';
            }
            else
            {
              Serial.println("Download finished");    
            }
          }
          else if (answer == 2)
          {
            Serial.println("Error from HTTP");
          }
          else
          {
            Serial.println("Error getting the url");
            data_size = 0;
          }

          Serial.print("Data received: ");
          Serial.println(data);
        }
        else
        {
          Serial.println("Error getting the url");
        }
      }
      else
      {
        Serial.println("Error setting the url");
      }
    }
    else
    {
      Serial.println("Error setting the CID");
    }    
  }
  else
  {
    Serial.println("Error initializating");
  }

  sendATcommand("AT+HTTPTERM", "OK", 5000);



}

void power_on(){

  uint8_t answer=0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin,HIGH);
    delay(3000);
    digitalWrite(onModulePin,LOW);

    // waits for an answer from the module
    while(answer == 0){  
      // Send AT every two seconds and wait for the answer   
      answer = sendATcommand("AT", "OK", 2000);    
    }
  }

}


int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
int i;
  uint8_t x=0,  answer=0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while( Uart.available() > 0) Uart.read();    // Clean the input buffer

    Uart.println(ATcommand);


    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Uart.available() != 0){    
      response[x] = Uart.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)    
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));    
  Serial.println(response);
  return answer;
}



int8_t sendATcommand2(char* ATcommand, char* expected_answer1, 
char* expected_answer2, unsigned int timeout){

  uint8_t x=0,  answer=0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while( Uart.available() > 0) Uart.read();    // Clean the input buffer

  Uart.println(ATcommand);    // Send the AT command 


    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{        
    if(Uart.available() != 0){    
      response[x] = Uart.read();
      x++;
      // check if the desired answer 1 is in the response of the module
      if (strstr(response, expected_answer1) != NULL)    
      {
        answer = 1;
      }
      // check if the desired answer 2 is in the response of the module
      if (strstr(response, expected_answer2) != NULL)    
      {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));    
  Serial.println(response);
  return answer;
}





int8_t getClock(){
  int  second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  int timeout=1000;
  uint8_t x=0,y=0,  answer=0;
  char response[100];
  unsigned long previous;
  answer=0;
  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while( Uart.available() > 0) Uart.read();    // Clean the input buffer

  Uart.println("AT+CCLK?");    // Send the AT command 

  y=0;
  x = 0;
  previous = millis();

  // this loop waits for the answer

  x=0;
  do{
    if(Uart.available() != 0){    
      response[x] = Uart.read();
      x++;
      // check if the desired answer is in the response of the module
      if(x >37)answer=1;
    }
    // Waits for the asnwer with time out
  }
  while((answer== 0) && ((millis() - previous) < timeout));    



  year=(response[20]-48)*10+(response[21]-48);
  month=(response[23]-48)*10+(response[24]-48);
  dayOfMonth=(response[26]-48)*10+(response[27]-48);
  hour=(response[29]-48)*10+(response[30]-48);
  minute=(response[32]-48)*10+(response[33]-48);
  second=(response[35]-48)*10+(response[36]-48);


  setTime(hour,minute,second, dayOfMonth,month,year);
  Serial.println(response);
  return answer;
}


int8_t sendData(){
  char* expected_answer1="OK";
unsigned int  timeout=5000;
int i;
  uint8_t x=0,  answer=0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100);    // Initialize the string

  delay(100);

  while( Uart.available() > 0) Uart.read();    // Clean the input buffer





Uart.print("AT+HTTPPARA=\"URL\",\"http://212.1.208.94/system/ping.php?id=1&data");
delay(500);
Uart.print("=1;1000;2000;1;0;1;2;0;0;0;1;1000;2000;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;1;1;0;1;2;0;0;0;1;1000;2000;1;0");
delay(500);
Uart.println(";1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;0;1;0;10;1;0;1;2;0;0;\"");


    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Uart.available() != 0){    
      response[x] = Uart.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)    
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));    
  Serial.println(response);
  return answer;
}
HardwareSerial Uart = HardwareSerial();

Which pins is this instance associated with?

Isn't there another instance (Serial, most likely) that ALREADY uses those pins?