Serial.read speed

TomyLimon
Posts: 1
Joined: Mon Dec 13, 2021 6:49 pm

Serial.read speed

Postby TomyLimon » Mon Dec 13, 2021 7:28 pm

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;
      }

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Serial.read speed

Postby ESP_Sprite » Tue Dec 14, 2021 2:04 am

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.

Who is online

Users browsing this forum: No registered users and 56 guests