Problem with reading serial data from A6 gsm module

Hello. I have a problem which I can't figure out... In the datasheet of A6 module it says that every response from module will begin with \r\n and finish with \r\n also. So I decided to put \n as end marker for storing response from module to char array... The problem is that when I make a call to module the response from module should be like this

I know that because when i write the function update serial in this kind of way it works ok

void updateSerial()
{
  
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

But I decided to go with Robin's serial tutorial, so I wrote the function like this and this is where i am missing some characters after my phone number... The complete code is here... And this comes to me serial monitor when I make a call to A6, and as you can see, the characters behind my phone number are missing and I can't figure out why... I would appreciate any help for better understanding

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);



const char number[] = {'+', 'C', 'L', 'I', 'P', ':', ' ', '"', '3', '8', '5', '9', '8', '5', '6', '0', '9', '4', '5', '\0'};
boolean message = false;
const byte CharNumber = 20; // maximum numbers of bytes
char incomingChar[CharNumber]; // array for incoming message
char CharReceived = 0; //for storing the lenght of index/bytenumber


void receive() { //function for receiving data
  static byte index = 0;
  const char endflag = '\n'; // "n" indicates end of data
  static byte endmarker = 0;



  // while reciveing
  while (mySerial.available() > 0 && message == false) // while we receiving
  {

    char in = mySerial.read();
    if (in != endflag) { //if incoming char is not \n keep storing
      incomingChar[index] = in;
      index++;
      if (index >= CharNumber) {
        index = CharNumber - 1;
      } //end if
    } // end if

    else if (in == endflag) {     
        message = true;
        incomingChar[index] = '\0'; //terminate
        CharReceived = index;
        index = 0; 
    }
  }
}
void showNewData() {
   if (message == true) {
    Serial.print("This just in ... ");
    Serial.println(incomingChar);
    message = false;

  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {

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

  memset(response, '\0', 100); // Clean response buffer
  while ( mySerial.available() > 0) mySerial.read();   // Wait for clean input buffer
  mySerial.println(ATcommand);    // Send the AT command
  previous = millis();

  // this loop waits for the answer
  do {
    // if there are data in the UART input buffer, reads it and checks for the asnwer
    if (mySerial.available() != 0) {
      response[x] = mySerial.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;
      }
    }
    // Waits for the answer with time out
  } while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}

void updateSerial()
{ 
  while (Serial.available())
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  receive();
  showNewData();




}

void setup()
{

  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);

  //Begin serial communication with Arduino and A6
  mySerial.begin(9600);

  Serial.println("Initializing...");
  delay(3000);

  while (sendATcommand("AT", "OK", 1000) == 0) {
    delay(500);
    mySerial.println("AT");
    updateSerial();

  }

  mySerial.println("AT+CLIP=1"); // show the calling number
  updateSerial();

}



void loop() {

  updateSerial();



}

Ooops

void updateSerial()
{ ...}

EDIT: that part is ok, misread

Your image:

void updateSerial()
{ 
  while (Serial.available()>0)
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  receive();
  showNewData();
}

Tried with this also, i have been struggling with this for days, ooops is unlikely going to help me... if I could figure it out by myself I wouldn't bother posting in the forum... I don't ask for code but more specific directions would be nice

Sorry misread it - actually thought you were reading from Serial and I did not see your image... fixed

Your issue is that your buffer is too small, so the receive function drops whatever is above 19 chars (19 chars plus trailing ‘\0’ = 20 which is the incomingChar buffer size)

Change 20 in

const byte CharNumber = 20; // maximum numbers of bytes
char incomingChar[CharNumber]; // array for incoming message

and use something larger like 50 or whatever you think the longest ever meaningful line will be

VTI_16V:
But I decided to go with Robin's serial tutorial,

Based on your description of the data "begin with \r\n and finish with \r\n" I suspect the second example in Serial Input Basics would work without any changes. Every second message would be very short (just the starting \r) and could be discarded.

What is the maximum number of bytes in an incoming message?

...R

Solved with bigger buffer size, thank you guys... Feel like an idiot for not seeing that :confused:

:slight_smile:

We have all been there, don’t feel bad!