Serial.readString() is extremely slow

Hi there..

So this the problem: Serial.readString(); is tooooo slow (2500 mili seconds).. Is there any way or working around method for speed up the function or use something else?

Everything so good about the strings functions.. They have a lot of good capabilities and much easier to understand and work around with then arrays...

But what I find out is that everything works but readString() function is extremely slow. It takes about 2,5 sec or 2500 mili seconds to execute..... It is like a FOREVER... It is unacceptable.

So pls for those who knows. Don't pass it pls help... I got bulled equipment now with strings I realy need to speed up the process...

Thank you...

I got bulled equipment now with strings I realy need to speed up the process...

?

But what I find out is that everything works but readString() function is extremely slow. It takes about 2,5 sec or 2500 mili seconds to execute..... It is like a FOREVER... It is unacceptable.

The String functions work work fast for me, so it is probably some issue with your code.

1 Like

post your code, it may be affecting how it is taken but you can affect the timeout by setting it as such:

int myTimeout = 250; // milliseconds for Serial.readString

Serial.setTimeout(myTimeout);

default is 1000milliseconds

4 Likes

All functions of Strings working good and fast but the only one is the main one NO... It takes 2,5 sec to execute... Check it out you will be surprise then...

Im not the only one here!!!

BulldogLowell:
post your code, it may be affecting how it is taken but you can affect the timeout by setting it as such:

int myTimeout = 250; // milliseconds for Serial.readString

Serial.setTimeout(myTimeout);

default is 1000milliseconds

Hah Ok for example...

//ATDP DESCRIBE CURRENT PROTOCOL
// ATE(0,1) echo(on,off)
String Str;
String Str2;
int check = 1;
void setup()
{
  Serial.begin(9600);
  Serial.println("Start");    //This serial monitor what's UP
  
  Serial1.begin(9600);
  Serial1.print("ATZ/r");       // On this Serial sitting ELM327
  delay(1000);
  
  
  Serial3.begin(115200);       //This one is LCD
  //Str = Serial1.readString();       // Everything what ELM answers we got in the String Str
             
}

void loop()
{
  
               Str = Serial1.readString(); 
               Serial1.flush();
               Serial1.print("010C\r");         //RPM  PID
  
               
               Serial.println(Str);              //This one for monitor you know

              
               
  
  if (Str.startsWith("SEARCHING"))  //When you first power up the system ELM automaticaly scan for car protocols when its doing that its answers with Searching word
  {
  

     Serial3.write(0xFE);           //While we serching do this
     Serial3.write(0x01);
     Serial3.print("Serching");
     
     Serial1.print("010C\r");
     delay(3500);                     //We need this delay for ELM find the right protocol when ing ON
  }
    
  
  if (Str.startsWith("NO",5))
  {
  
     Serial3.write(0xFE);
     Serial3.write(0x01);            // When ELM actually knows the car protocol but can't obtin data from it because ingitin  is off.
     Serial3.print("NO DATA");
     Serial3.print(" ");
     Serial3.print("ING OFF");
     
     Serial1.print("010C\r");
     delay(3500);
  }
    
    
   if (Str.startsWith("41", 5))    // Finaly when we got the data we ware searching for we doing this:
      
        {
        //  Str != Serial1.readString();
          // Str2 = Serial1.readString(); 
          while(check == 1)
          
                 {
                     //  Str2 = Serial1.readString();
                       
                     // getRPM();
                     
                     Serial1.print("010C\r");
                     Serial.println("While");
                     Serial.println(Str2);
                     String A = Str2.substring(11,13);       //It is was pain to get it right... Cause STRTOL you can specify the begining of string but not the end or I don't get so that's way in to 2 Strings
                     String B = Str2.substring(14,16);      
                     Serial.println(A);
                     Serial.println(B);

                       //((A*256)+B)/4
                      int long Ch = ((strtol(&A[0], 0, 16) * 256) + strtol(&B[0], 0, 16)) / 4;     //The magic formula what gets actual DEC number of HEX answer


                     if (Ch == 640){ check = 0;}                                                  

                     Serial3.write(0xFE);
                     Serial3.write(0x01);              //reset LCD and Put  the cursor to the TOP left position
                     Serial3.write(0x0D);               //Put the cursor to the beginnig of second rol


                     Serial3.print(Ch);
                     Serial3.print(" Mph");
                     
                     
                     
                    // delay(500);
                     
                     Str2 = Serial1.readString();     // This one takes 2.5 sec to execute. I tryed to get rid of it  but you know you got to call it...
                                                                                    YES Im changing Str1  on Str2 for this part... Don't warry
 
                 }
  }
}

Check it out you will be surprise then...

If you can't post your code in this forum you will probably wind up being ignored.

Serial.setTimeout(myTimeout);

default is 1000milliseconds
[/quote]

So what I do:

void setup()
{
Serial1.setTimeout(0);
}

like that?

1 Like

zoomkat:

Check it out you will be surprise then...

If you can't post your code in this forum you will probably wind up being ignored.

I just did.

Look to the last part of it in 3d if

Slavka85:

Serial.setTimeout(myTimeout);

default is 1000milliseconds

So what I do:

void setup()
{
Serial1.setTimeout(0);
}

like that?
[/quote]

well... you probably don't want to set it to zero....

start with 200ms and tweak it as you like.

BulldogLowell:

Slavka85:

Serial.setTimeout(myTimeout);

default is 1000milliseconds

So what I do:

void setup()
{
Serial1.setTimeout(0);
}

like that?

well... you probably don't want to set it to zero....

start with 200ms and tweak it as you like.

[/quote]

Allright.. It is a lid now... Im going to check it out tomorow when I get to the car...

Yep of course you know you will defiantly check any timing and found what fits.. Hope it will work.... But you said 1000 is default.... 1000 is sure a lot... But still it seems I got the rest 1.5 sec....will see tomor im excited.

1 Like

You may want to get rid of the below as it is probably not doing what you think.

Serial1.flush();

zoomkat:
You may want to get rid of the below as it is probably not doing what you think.

Serial1.flush();

rather than being cryptic... and for the OP seeking help...

Serial.flush is benign in that context, and is not at all affecting your serial processing.

it is a tool used block further processing, waiting to clear out any OUTGOING serial data, so you can certainly do without.

zoomkat:
You may want to get rid of the below as it is probably not doing what you think.

Serial1.flush();

it's doesn't matter there... its exuteted once there it's like setupping for ELM waiting for answering and stuff.. But thanks anyway...

Im talking about this part of the code....

     {
                     //  Str2 = Serial1.readString();
                       
                     // getRPM();
                     
                     Serial1.print("010C\r");
                     Serial.println("While");
                     Serial.println(Str2);
                     String A = Str2.substring(11,13);       //It is was pain to get it right... Cause STRTOL you can specify the begining of string but not the end or I don't get so that's way in to 2 Strings
                     String B = Str2.substring(14,16);      
                     Serial.println(A);
                     Serial.println(B);

                       //((A*256)+B)/4
                      int long Ch = ((strtol(&A[0], 0, 16) * 256) + strtol(&B[0], 0, 16)) / 4;     //The magic formula what gets actual DEC number of HEX answer


                     if (Ch == 640){ check = 0;}                                                  

                     Serial3.write(0xFE);
                     Serial3.write(0x01);              //reset LCD and Put  the cursor to the TOP left position
                     Serial3.write(0x0D);               //Put the cursor to the beginnig of second rol


                     Serial3.print(Ch);
                     Serial3.print(" Rpm");
                     
                     
                     
                    // delay(500);
                     
                     Str2 = Serial1.readString();     // This one takes 2.5 sec to execute. I tryed to get rid of it  but you know you got to call it...
                                                                                    YES Im changing Str1  on Str2 for this part... Don't warry
 
                 }

I need to speed up... You see what happans as soon as delite " Str2 = Serial1.readString" everything just flyes... But of course I don't have string now nothing works... we need to call the reading function...

so... String (capital 'S') class basically sucks with Arduino.

You may want to look to parse your incoming data as a string (small 's'/char array) instead, or just parse (what looks to be) bytes and build integers from your transmission.

What does your data look like? You could avoid a lot of machinations if you are merely seeking numbers/integers.

On the other hand, if you are done and happy... well that's that.

BulldogLowell:
so... String (capital 'S') class basically sucks with Arduino.

I figure that...

You may want to look to parse your incoming data as a string (small 's'/char array) instead, or just parse (what looks to be) bytes and build integers from your transmission.

What do you mean?

What does your data look like?

If sending "010C" Respond will look like "010c 41 0C OB 67 >" OB 67 Is what I actually need from the String...

You could avoid a lot of machinations if you are merely seeking numbers/integers.

On the other hand, if you are done and happy... well that's that.

Well not yet.

[/quote]

Where did you find Serial.readString() and what is it supposed to do? Don't remember seeing this String function before.

zoomkat:
Where did you find Serial.readString() and what is it supposed to do? Don't remember seeing this String function before.

What you mean where I found???

right here i guess

ok if you newer seen this function before. Then here the quastion what else can be used as like it???

i got the answering String such as "010c 41 0C 4D 67" How else axcept using arrays with F pointers I can read it all in onse and get from it what i need?

zoomkat:
Where did you find Serial.readString() and what is it supposed to do? Don't remember seeing this String function before.

Stream class...

I looked at the readString() documentation and it seems to circular without any example code. It also says "reads characters from a stream into a string" and not a String. I suggest you capture your own String somewhat like below then parse out the info you desire.

//zoomkat 6-29-14 Simple serial echo test
//type type text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}