Go Down

Topic: Problem reading large data (Read 734 times) previous topic - next topic

technofection

I am trying to interface with a SIM900A modem (Not a Arduino Shield). I am facing a strange problem. It seems I cannot read more than 63 bytes from the modem at a time. When I issue a command like AT+CMGR=1 (to read a message). I get a truncated message (upto 63 bytes). Below is my code. Can someone please suggest what wrong I am doing here?

Code: [Select]

char gsmbuff[400];
char cmdBuff[50];
char *tmpBuff;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  int i = readGSMSerial();
  if(i > 0){
    Serial.print(i);
    Serial.write("Bytes Data From SIMCOM: ");
    Serial.write(gsmbuff);
    Serial.println(); 
  }

  if(Serial.available()){
    delay(100); //Delay to allow the buffer to be filled
    memset(cmdBuff, '\0', 50);
    int i = 0;
    while(Serial.available()){
      cmdBuff[i] = Serial.read();
      i++;
    }

    Serial.write("Received Command: ");
    Serial.write(cmdBuff, i);
    Serial.println();

    sendGSMCommand(i);
  }
}

void sendGSMCommand(int cmdLength){
  if(strstr(cmdBuff, "SMS") != NULL){
      sendSMS();
  }else{
    //Add \r\n for AT commands
    cmdBuff[cmdLength++] = '\r';
    cmdBuff[cmdLength++] = '\n';
    //Send the command to GSM module
    if(cmdLength >= 2){
      Serial1.write(cmdBuff, cmdLength);
      delay(100); //Wait a little before proceeding and reading the response back
    }
  }
}

int readGSMSerial(){
  int i=0;
  memset(gsmbuff, '\0', 400);
  if(Serial1.available()){
    delay(100); //Delay to allow the buffer to be filled
    while(Serial1.available()){
      gsmbuff[i++] = Serial1.read();
      if(i == 400){
        break;
      }
      retryCount = 5;
    }
  }
  return i;
}

int sendSMS(){
  Serial1.write("AT+CMGS=\"9874637890\"\r\n");
  //Wait for the message promot response ">"
  while(readGSMSerial()==0){
    delay(100);
  }
  Serial1.write("Hello there! I am from SIM900A");
  delay(100);
  Serial1.write((char)26);
  delay(100);
}

patdevel

The internal size of the RX buffer is limited to 16 or 64 bytes depending the board (16 for an uno 64 for a mega)

Just change the line in HardwareSerial.h (located in ../Arduino/hardware/arduino/avr/cores/arduino for the arduino 1.7..)
from #define SERIAL_RX_BUFFER_SIZE 64
to    #define SERIAL_RX_BUFFER_SIZE 256

and recompile .

Go Up