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..