Page 1 of 1

Serial.read speed

Posted: Mon Dec 13, 2021 7:28 pm
by TomyLimon
Hello.
Maybe I'm asking a stupid question, sorry.

I try to communicate, using serial.read with nextion display.
I have a function to transform strings into values.
As result, as I identified, the process does not wait for the function to end and go forward.
As result, I have the first variable but doesn't have a second.
this is a string from Nextion display: &SET_TEMP|22|&

Code: Select all

String screen_action = "";
uint8_t screen_value = 0;
void setup() {
    Serial.begin(9600);
    Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop(){
    readDisplay();
    if(screen_action == "SET_TEMP"){     /// at this moment I do have screen_action = "SET_TEMP" and screen_value
        action_temp = screen_value;
        Serial.print("SET action_temp to ");
        Serial.print(action_temp);
        screen_action = "";
    }
}

void readDisplay(){
  if (Serial2.available()) {
      inc = Serial2.read();
      if(inc == '|'){
          if(position==1){
              screen_action = incStr;
              incStr = "";
              debug_println("Screen action: "+x);
          }
          if(position==2){
              screen_value = incStr.toInt();
              incStr = "";
              debug_print("Screen value: ");
              debug_println(y);
          }
          position += 1; 
      }else{
          incStr += inc;
      }
      if(inc == '&'){
          incStr = "";
          position = 1;
      }
  }
}
Is it a right understanding of the process?
To solve the problem, I replace screen_action and screen_value with temporary variables val1 and val2 and do

Code: Select all

     if(inc == '&'){
          screen_action=val1;
          screen_value = val2; 
          incStr = "";
          position = 1;
      }

Re: Serial.read speed

Posted: Tue Dec 14, 2021 2:04 am
by ESP_Sprite
The thing is, every time you call readDisplay, it tries to parse one character from the serial port. That works great up until the first '|' sign. At that point, it recognizes it received screen_action and sets that variable. At that point, your main loop immediately sees screen_action is set to something and uses that plus action_temp to send a string.... but readDisplay hasn't even gotten up to parsing actionTemp yet, so it's not set. Your solution correctly solves this issue.