Nextion Display No Lib: Switching between pages too fast messes up outgoing data

Hello all,

So I am using the Serial3.available() strategy to obtain data from the Nextion display to update such things as page number. I convert integers to text then send that text to my Arduino using this command on the screen for every page:

get t11.txt
get t12.txt
get t13.txt
get t14.txt

Now, I organize the received data in the Arduino IDE using this filtering algorithm:

void VariablesListenUpdate() //Sending Variables from Nextion to µC whenever any page is postinitialized
{

  if (Serial3.available()) { //Is data coming through the serial from the Nextion?
    inByte = Serial3.read();

    Serial.print(inByte); //See the data as it comes in
    Serial.print("  ");

    if (inByte == 112)
    {
      y = 1; //Just for when nextion starts. 112 is first byte we want to start reading from. without this our organized byte data gets unorganized.
    }

    if (inByte > 47 && inByte < 58 && y == 1) { //Is it data that we want to use? This is ASCII where 48 is 0 49 is 1 ... and 57 is 9
      message.concat(char(inByte)); //Cast the decimal number to a character and add it to the message 
    }
    else if (inByte == 255 && y == 1) { //Is it an end byte?
      endBytes = endBytes + 1; //If so, count it as an end byte.
    }

    if (inByte == 255 && endBytes == 3) { //Is it the 3rd (aka last) end byte?
      Received_Value_From_Screen = message.toInt(); //Because we have now received the whole message, we can save it in a variable.
      message = ""; //We received the whole message, so now we can clear the variable to avoid getting mixed messages.
      endBytes = 0; //We received the whole message, we need to clear the variable so that we can identify the next message's end
      y = 0;
      numMessages  = numMessages + 1; //We received the whole message, therefore we increment the number of messages received.
    }
  }

  if (numMessages == 1) { //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
    numMessages = 0; //Now that the entire set of data is received, reset the number of messages received
    variableID = variableID + 1;
  }

  if (variableID == 1)
  {
    target_variable = Received_Value_From_Screen;
  }

  if (variableID == 2)
  {
    gas_variable = Received_Value_From_Screen;
  }

  if (variableID == 3)
  {
    liftmass_variable = Received_Value_From_Screen;
  }

  if (variableID == 4)
  {
    page_ID = Received_Value_From_Screen;
    variableID = 0;

    if (page_ID == 90001 || page_ID == 24465) //the first value could be held by a 32-bit CPU but a 16-bit CPU cant read it so it overflows to the second value
    {
      page_num = 1; //Home Page
    }

    if (page_ID == 90002 || page_ID == 24466) 
    {
      page_num = 2; //Initiate Page
    }

    if (page_ID == 90003 || page_ID == 24467) 
    {
      page_num = 3; //Settings Page
    }

    if (page_ID == 90004 || page_ID == 24468)
    {
      page_num = 4; //Set Weight Page
    }

    if (page_ID == 90005 || page_ID == 24469)
    {
      page_num = 5; //Advanced Options Page
    }

    if (page_ID == 90006 || page_ID == 24470) 
    {
      page_num = 6; //Gas Page
    }

    if (page_ID == 90007 || page_ID == 24471)
    {
      page_num = 7; //Lift Mass Page
    }

    Serial.println(" ");
    Serial.print("target:");
    Serial.print(target_variable);
    Serial.print("  ");
    Serial.print("gas:");
    Serial.print(gas_variable);
    Serial.print("  ");
    Serial.print("lift:");
    Serial.print(liftmass_variable);
    Serial.print("  ");
    Serial.print("page:");
    Serial.print(page_num);
    Serial.println(" ");

  }
}

}

This works great when I switch between pages slowly but when I switch between pages too fast, the "get" sequences on the nextion display appear to be interrupted so the organization is messed up and sent, making received variable values in the Arduino very incorrect. I've attached an example of what the serial monitor shows when switching between pages too fast.

Is there any way around this? I mainly just need the page number updated on every screen to appropriately send data from the Arduino to the Nextion display. Sending data to all pages at the same time from the Arduino seems to creates too much traffic on the Serial3 line so data is also disorganized and buffer overflows in the Nextion simulator too.

Thanks in advance..

Woops here is the attachment

Hello Aero2019,
Support for Nextion on these fora is pretty much as follows:

You can follow the methods I set out in using Nextion displays with Arduino. My methods do not use the Nextion libraries, so I can offer help on using my methods but I cannot offer anything very helpful for any Nextion library.

The original Nextion libraries are full of bugs. There is a link from my tutorial to to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.

There's also a guy on here called Seithan who has also developed his own ways of dealing with Nextion displays, you can find his methods in his tutorial on how to write code with Nextion and Arduino.

Beyond that the odd person occasionally offers a bit of help but not much.

I am unclear if you are using the Nextion libraries or not, your first bit of code suggests you are so I suppose you must be, but as you've not posted your complete code it's hard to tell.

Why have you posted an image of text? Sorry, but that's daft. Post text as, well, err....text. Use code tags for it as well, like you've done with the code.

To answer your question:

Is there any way around this?

Yes, use my methods, you won't have these problems.

Hi Perry,

Thank you for your response. I am not using the Nextion library. The "get" command in the Nextion HMI software is a standard that doesn't require the library. It outputs text in ASCII decimal which I then convert to integers. As you know characters are much easier to deal with when it comes to serial comms. When the number 400 is sent from the nextion for example it sends

112 52 48 48 255 255 255

Where 112 is the typical Nextion start 'signal', 52 is 4 in ASCII, two 48's make two 0's in ASCII, and then the three 255's mark an end 'signal'.

I will look more into your methods! I found this 'get' command to be a simple solution especially for one page but when multiple pages are involved and you quickly go through them a sequence of 'get' commands appear to be interrupted.

Hi Aero,

My apologies, I do not use 'get' and am not familiar with it. I guess I should have checked the Nextion instruction set before replying.

There are a few reasons I can't give you a helpful reply to your question; first I am not very good with other people's code. Second it took me a lot of time and many mistakes to get my methods working properly. To look at some code someone else has written, using their ideas, and try to suggest why it might not work very well is a big ask. I don't know how you think and why you made the decisions you made. You approach is different to mine so I don't know what problems it might present.

If you get stuck with my methods then I can help.

Have fun :slight_smile: