how about for the Serial.read() get data

So, I have a question.
when I get the data from the GPS, I get the infromation data like this by Serial assistant(betwen --------------------)

AT++UGNSINF
+UGNSINF: 1,1,20180731140456.000,39.846848,116.381652,231.900,0.00,268.8,1,,0.8,1.1,0.8,,13,5,,,45,,,

OK

it is long String and has ‘’\n“and”\r"

I set the softserial in the Ariduino Uno for receive them.

How can get data by .read() ?
Before, I used the .readString get the data, but .readString has 1 second dealy by the system. it is problem for the Program Result.

When I use the Program like below for test.

String comdata="";
void setup() {
Serial.begin(19200);
while (!Serial) {}
}
void loop() {
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
}
if (comdata.length() > 0)
{
Serial.println(comdata);
comdata = "";
}
}

Example: I put in the data like this

AT+UGNSINF
+UGNSINF: 1,1,20180731140456.000,39.846848,116.381652,231.900,0.00,268.8,1,,0.8,1.1,0.8,,13,5,,,45,,,

OK

the windows show receive like this:

AT+UGNSINF +UGNSINF: 1,1,20180731140456.000,39.846848,116.381652,231.900,0.00,268.8,1,,,,,,,O

it do not receive all of the data.
So,Lost many data.

Everybody, Do you know how to soulation the quesion?

You can also use the NORMAL Serial.read() and made a CHAR ARRAY, rhat on Arsuino is better than a String, and put the single data into it. The classic

while (Serial.available())
{
stringinarray [i++]=Serial.read();
}

Read Serial Input Basics - updated for ideas; make sure the buffer is big enough to hold the maximum number of characters plus a terminating nul character; the examples use 32 and you can easily change it.

Silente:
You can also use the NORMAL Serial.read() and made a CHAR ARRAY, rhat on Arsuino is better than a String, and put the single data into it. The classic

while (Serial.available())

{
stringinarray [i++]=Serial.read();
}

That suffers from the same problem; if the transmission of a character is in progress and the buffer is empty, the available() method will return 0.

Ok, so you can do a timing solution, like this

unsigned long tp=millis();
byte i=0;
char string [???]={'\0'};
while (millis()-tp<interval && i<???)
{
if (Serialmavailable())
{
string [i++]=Serialmread();
}
}

Silente:
Ok, so you can do a timing solution, like this

The first thing would be to read till a known character (e.g. '\n'). Next you can add a timeout if needed.

sterretje:
The first thing would be to read till a known character (e.g. '\n'). Next you can add a timeout if needed.

Thanks a Lot!

the program I used like below. drop more some information by GPSMsg += char(GPSSerial.read()); keep the time part. ( the string can not keep all infrmation)
So, I can get the UTC_date_Time only now. ^_^.

but one more question, running this part will be use 330ms time about(before get date for GPS),will be used 360ms (after get data and do.) I think it is used so long time for this part.
So, when I input time to Clock, I will lost 360ms about.

void GPS_Data_Get() {
String GPSMsg = "";
int commaPosition = 1;
GPSSerial.println("AT+CGNSPWR=1"); delay(20);
GPSSerial.println("AT+CGNSINF"); delay(20);
while (GPSSerial.available() > 0)
{
GPSMsg += char(GPSSerial.read());
delay(5);
}
if (GPSMsg.length() > 40)
{ //Serial.println(GPSMsg.length());
do
{ commaPosition = GPSMsg.indexOf(': ');
if (commaPosition != -1)
{ GPSMsg = GPSMsg.substring(commaPosition + 1, GPSMsg.length());
UTC_date_Time = GPSMsg.substring(4, 22);
//Latitude = GPSMsg.substring(23, 32);
//Longitude = GPSMsg.substring(33, 43);
//Serial.println(GPSMsg);
Serial.println("GUTC_date_Time:" + UTC_date_Time);
//Serial.println("Latitude:" + Latitude);
//Serial.println("Longitude:" + Longitude);
}
}
while (commaPosition >= 0);
}
else {
Serial.println("Waiting Get Data From GPS ");
}
}

while (GPSSerial.available() > 0)
{
GPSMsg += char(GPSSerial.read());
delay(5);
}
if (GPSMsg.length() > 40)

40 x 5 ms equals at least 200 ms wasted.

Read the thread that I linked; it will not waste any time. It also does not use the String class (capital S) so less chance that you end up with code that starts showing undefined behaviour due to inefficient memory usage.

sterretje:
40 x 5 ms equals at least 200 ms wasted.

Read the thread that I linked; it will not waste any time. It also does not use the String class (capital S) so less chance that you end up with code that starts showing undefined behaviour due to inefficient memory usage.

Thanks a lot, you are the best!
I change the CODE like below , The runing time 53ms more or less.

sterretje:
while (GPSSerial.available())
{
GPSMsg += char(GPSSerial.read());
}
if (GPSMsg.length() > 130)

So, is this your way ?

sterretje:
while (Serial.available())
{
stringinarray [i++]=Serial.read();
}

Taiyi_zhan:
So, is this your way ?

It is not how it is done in Serial Input Basics - simple reliable ways to receive data.

...R

Robin2:
It is not how it is done in Serial Input Basics - simple reliable ways to receive data.

...R

Thanks a lot! I will study it.