Serial.readString does not give whole string

Hi,

I am trying to basically read string from serial. However it readString does not give me whole the Serial1 response.

when the code is:

Serial1.println("AT+CGNSINF");
it gives me this:

AT+CGNSINF

+CGNSINF: 1,1,20200922094421.000,36.208805,36.1558

I tried Serial.readStringUntil('\n'); but still no change unfortunately.

However, when I tried to do this with Serial monitor itself, like writing to command to serial monitor manually it gives me the true answer like:

AT+CGNSINF

+CGNSINF: 1,1,20200922094719.000,36.208752,36.155862,121.200,0.17,229.9,1,,1.0,1.3,0.9,,10,8,,,43,,

OK

Why this is happening? can someone help me?

ratatosk1:
Why this is happening? can someone help me?

Maybe there's something wrong with the code you didn't post.

So, this is the GPS function

int getGPS()
{

	sendSingleCommand("AT+CGNSINF", 200); //500
  Serial1.flush();
  Serial.flush();
  //delay(500);
  
	while(Serial1.available())
    {
        String tempAns = Serial1.readString();
       // Serial.println(tempAns);

        if(getSplittedValue(tempAns,',', 1).equals("1"))
        {
            String latS = getSplittedValue(tempAns,',',3);
            String longS = getSplittedValue(tempAns,',',4);
            String gnss = getSplittedValue(tempAns,',',15);
            String glonass = getSplittedValue(tempAns,',',16);
            
            latt = latS.toFloat();
            longg = longS.toFloat();
            gnssnum = gnss.toInt();
            glonassnum = glonass.toInt();

            Serial.print("GPS Updated: ");
            Serial.print(latt);
            Serial.print(",");
            Serial.print(longg);
            Serial.print(",");
            Serial.print(gnssnum);
            Serial.print(",");
            Serial.print(glonassnum);
            Serial.println();
            return 1;
        }
        else
        {
            Serial.println("Cannot take GPS Signal");
            return 0;
        }
    }
}

In this function I cant get the number of gnss and glonass satellites seen

AT+CGNSINF

+CGNSINF: 1,1,20200922094719.000,36.208752,36.155862,121.200,0.17,229.9,1,,1.0,1.3,0.9,,10,8,,,43,,

OK

Here these numbers are 10 and 8. Is this indexing fault or something?

Serial.readString() has a timeout - maybe that triggers before the full message arrives.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

What Arduino board are you using?

Note that Serial.flush() is used when sending data - it makes the Arduino wait until all the data has been sent.

...R

TheMemberFormerlyKnownAsAWOL:
Maybe there's something wrong with the code you didn't post.

Still not posted it have you?

http://snippets-r-us.com

Posted it dont you see

ratatosk1:
Posted it dont you see

I see a snippet.

Yes, and that is the part which I need help bro. If another parts have correlation with this part I would post them too. Why am I convincing you? :frowning:

No, you're not convincing me.

Then, help me if you know the answer?

Answer?
I don't even know fully what the question is
Post the code.

ratatosk1:
Yes, and that is the part which I need help bro.

Please post the complete program. Often the reason you can't find the cause of the error is because it is not in the piece of code where you think it is.

...R

Here it is

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include "DFRobot_SHT20.h"



#define SIM_PWR 12




String datee = "";
String timee = "";

float latt = 0;
float longg = 0;
int gnssnum = 0;
int glonassnum = 0;

int counter = 0;
int tempr = 0;



String getSplittedValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}


int sendCommand(String cmd, String answer, int delay_time)
{
    Serial1.println(cmd);
    Serial.flush();
    Serial1.flush();
    delay(delay_time);
    while(Serial1.available())
    {
        String ans = Serial1.readString();
    
        if(ans.equals(answer))
        {
            return 1;
        }
    }
    
    return 0;

}

void sendSingleCommand(String cmd, int delay_time)
{
    Serial1.println(cmd);
    delay(delay_time);
    Serial.flush();
    Serial1.flush();
}


void showMessage()
{
   Serial1.flush();
   Serial.flush();
   while(Serial1.available())
   {
      Serial.println(Serial1.readString());
   }
}

String showMessageS()
{
  String mes = "";
  Serial1.flush();
  Serial.flush();
  while(Serial1.available())
   {
      mes = Serial1.readString();
   }

  return mes;
}

void powerTrigger()
{
    Serial.println("Device is enabling..");
    digitalWrite(SIM_PWR, HIGH);
    delay(3000);
    digitalWrite(SIM_PWR,LOW);
}

void setGPRS()
{
    int r = sendCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 150); //300
    Serial1.flush();
    Serial.flush();
    r = sendCommand("AT+SAPBR=3,1,\"APN\",\"INTERNET\"","OK", 150); //300
    r = sendCommand("AT+SAPBR=1,1", "OK", 150); //300
       
    Serial.println("GPRS Enabled");
}

void closeGPRS()
{
    int r = sendCommand("AT+SAPBR=0,1", "OK", 10); //300
    Serial1.flush();
    Serial.flush();
}


int getGPS()
{

	sendSingleCommand("AT+CGNSINF", 350); //500
  Serial1.flush();
  Serial.flush();
  //delay(500);
  
	while(Serial1.available())
    {
        String tempAns = Serial1.readString();
       // Serial.println(tempAns);

        if(getSplittedValue(tempAns,',', 1).equals("1"))
        {
            String latS = getSplittedValue(tempAns,',',3);
            String longS = getSplittedValue(tempAns,',',4);
            String gnss = getSplittedValue(tempAns,',',15);
            String glonass = getSplittedValue(tempAns,',',16);
            
            latt = latS.toFloat();
            longg = longS.toFloat();
            gnssnum = gnss.toInt();
            glonassnum = glonass.toInt();

            Serial.print("GPS Updated: ");
            Serial.print(latt);
            Serial.print(",");
            Serial.print(longg);
            Serial.print(",");
            Serial.print(gnssnum);
            Serial.print(",");
            Serial.print(glonassnum);
            Serial.println();
            return 1;
        }
        else
        {
            Serial.println("Cannot take GPS Signal");
            return 0;
        }
    }
}

void updateDateTime()
{
    Serial.flush();
    Serial1.flush();
    sendSingleCommand("AT+CCLK?", 150); //500
	  Serial.flush();
    Serial1.flush();
    //delay(500);
    while(Serial1.available())
    {
        String tempAns = Serial1.readString();
	
        datee = getSplittedValue(tempAns,',',0);
        datee = datee.substring(datee.length()-8,datee.length());
        timee = getSplittedValue(tempAns,',',1);
        timee = timee.substring(0,8);

        Serial.print("Date time updated: ");
        Serial.print(timee);
        Serial.print(",");
        Serial.print(datee);
        Serial.println();
    }
	
}



void GPRSConfig()
{
    powerTrigger();
    delay(3000);
    sendSingleCommand("AT", 10);
    //delay(3000);
    showMessage();
    showMessage();
    //delay(2000);
    
    showMessage();
    //delay(1000);
    sendSingleCommand("AT+CLTS=1", 200);
    //delay(1000);
    showMessage();
    sendSingleCommand("AT&W", 200);
    //delay(1000);
    powerTrigger();
    delay(3000);
    powerTrigger();
    Serial.println();
   // delay(5000);
    showMessage();
    //delay(3000);
    sendSingleCommand("AT+CGNSPWR=1", 200);
    //delay(2000);
    showMessage();
    //delay(100);
    sendSingleCommand("AT+CGNSSEQ=\"GSV\"", 200);
    //delay(2000);
    //sendCommand("AT+CGNSINF", 10);
    showMessage();
    //sendCommand("AT+CGNSINF", 10);
    showMessage();
    //sendCommand("AT+CGNSINF", 10);
    showMessage();
    sendSingleCommand("AT+CGNSINF", 10);
    showMessage();
    //delay(5000);
    showMessage();
    //delay(10000);
}

void setup()
{
    Serial.begin(9600);
    Serial1.begin(9600);
    delay(2000);
    Serial.println("Device initializing...");

    pinMode(SIM_PWR, OUTPUT);

    initParts();
    GPRSConfig();
    Serial.println("Device initialized");
    delay(30000);
   
    Serial.println("Ready, Running");
    delay(1000);
    Serial.println("Configuring GPS");
    int resp = getGPS();
    while(resp!= 1)
    {
      resp = getGPS();
      delay(1000);
    }
    setGPRS();
}

void loop()
{
    Serial.println();

    tempr = getGPS(); 
	delay(1000);
}

I reckon it will be easier to get to the bottom of the this program if you write a short 10 or 20 line program that just covers the essentials.

...R

However it readString does not give me whole the Serial1 response.
I cant get the number of gnss and glonass satellites seen

There have been two different problem statements

One, that the whole string is not present in tempAns after

String tempAns = Serial1.readString();

Two, that the entire string is present, but the parsing of two numbers from that String is faulty.

Please clarify. You may need to modify the title of your posting.

I'm still confused by the question after reading through several times.

However, if the OP is trying to read

+CGNSINF: 1,1,20200922094719.000,36.208752,36.155862,121.200,0.17,229.9,1,,1.0,1.3,0.9,,10,8,,,43,,

from the serial port, then there's a good chance that part of the problem is the 64 character receive buffer limit. If my counting is correct, that message is just shy of 100 characters. The OP will most likely have to reassemble the message in their own buffer from multiple reads of the serial port.

I tought that too and tried to read Serial two times but it gave me nothing on second request

ratatosk1:
I tought that too and tried to read Serial two times but it gave me nothing on second request

It could be that once you had read it the first time and processed the contents, you had lost the rest of the message. Better to increase the buffer size.

You can be right but the message it gives me 51 characters it is not full of 64. And how can i increase the buffer size of Arduino Mega? I am using Serial1 to communicate the module.

Am I going to just write

#define SERIAL1_BUFFER_SIZE 100 or something?

And how can i increase the buffer size of Arduino Mega?

Top hit on a search of “ increasing the buffer size on an Arduino Mega” gives :-
https://forum.arduino.cc/index.php?topic=399751.0