Serial Buffer and Nextion Display

Hi There
I am Using a Esp32-C3 in combination with a Nextion Inelligent 7".
I get a really slow response when i touch something on my Nextion(something around 1second)
I think it has something to do with the serial input from the Nextion onto my Esp.

void checkNextionTouch(bool &kSicht,bool &kUnSicht,bool &kProg,bool &kSyn,bool &TSich,bool &TUns,bool &Wieder) //Auslesen der Touch events vom Nextion Display
{
  if (Serial0.available()>0)
    {
    String str = Serial0.readString();  
      
    if (str == "kSicht")
      {
        kSicht = true;
      } else 
      {
        kSicht = false;
      }
    
    if (str == "kUnSicht")
      {
        kUnSicht = true;
      } else 
      {
        kUnSicht = false;
      }
    
    if (str == "kProg")
      {
        kProg = true;
      } else 
      {
        kProg = false;
      }

    if (str == "kSyn")
      {
        kSyn = true;
      } else 
      {
        kSyn = false;
      }

    if (str == "TSich")
      {
        TSich = true;
      } else 
      {
        TSich = false;
      }

    if (str == "TUns")
      {
        TUns = true;
      } else 
      {
        TUns = false;
      }

    if (str == "Wieder")
      {
        Wieder = true;
      } else 
      {
        Wieder = false;
      }

    str = "";

    while (Serial0.available()>0) //empty Serial input Buffer
    {
      Serial0.read();
    }
    delay(10);

    }
}


  bool kSicht;
  bool kUnSicht;
  bool kProg;
  bool kSyn;
  bool TSich;
  bool TUns;
  bool Wieder;


void setup() 
{
  Serial0.begin(115200);                                                                                                                                                                                                                                                                                                                                                                                                                       
  Serial.begin(9600);
}


void loop() 
{

  checkNextionTouch(kSicht, kUnSicht,kProg,kSyn,TSich,TUns,Wieder);
  
    if (kSicht) {
    // Handle the kSicht event
    Serial.println("kSicht was touched");
  }

  if (kUnSicht) {
    // Handle the kUnSicht event
    Serial.println("kUnSicht was touched");
  }
 

}

If i run this code and touch one of the fields on the Nextion i get a continous output on my Serial Monitor (in this cas kSicht and kUnSicht)

anyone got an idea how to fix this?

thanks in advance

readString() does not know when the String is really fully received, so there is a builtin default timeout (that you can change) which is 1s ➜ if there is no further incoming data for 1 second on Serial0 then it will consider the String is received and return. That's likely the lag you see.

you could send an end marker and use that to help the readString know when it's done by using readstringuntil()

A typical end marker is '\n' to denote a new line.

Thanks that fixed it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.