Receiving unwanted characters between LCD and Arduino

Hello guys, I have a problem with my arduino and Nextion comunication.
I have created diferent pages to receive and send comands to arduino.
But in some pages I receive the next information:

Before I enter the page its all normal, but when I enter the page apears the code in arduino "$240d&" and next apears the information I have shown in the previous image.
And the code I run initializing the page is:
image

I see the code many times, but I am new with this lcd.

Are you able to post the code (in code tags please)? No one can help if they don't know the whole story, and the code is the story.

The. Wiring is also part of the story. How do you capture the Serial traffic going to the screen? (And what is the baud rate for this serial monitor)

1 Like

The code was 2000 lines. I can show parts of code how I receive the information from lcd.

The problem it's not on wiring, because in other pages I receive the correct information from lcd.
The baundrate is 38400, and I set the baudrate on first page.
image
I have RX_Buffer, and when I receive data from lcd, I print them.

while (Serial3.available())
  {
    RX_Buffer += char(Serial3.read());
    Serial.println(RX_Buffer);      
  }    

Normally I receive data like this ($70d&):
image
One more example what happen wrong:


I don't know ⸮⸮⸮ where is come from, the interrogation marks!!

the nextion protocol is not pure ASCII.

All instructions over seria are terminated with three bytes of 0xFF 0xFF 0xFF ➜ so that could be your three question marks and there is a trailing null char.

is RX_Buffer a String instance ?
your prints are accumulating input...

try something like this

while (Serial3.available()) {
  int x = Serial3.read();  
  Serial.print(F("0x");
  if (x < 0xF) Serial.write('0');
  Serial.print(x, HEX);
  Serial.write(',');
}

Normally this is whats happened, when I change the page:
image

I have a function who I calls when I send information to LCD:

void EndNextionCom()
{
  Serial3.write(0xff);
  Serial3.write(0xff);
  Serial3.write(0xff);
}
Serial3.print("Flight_Mod.T.val=" + String(T_Cal));
EndNextionCom();

Sorry.

please please please never post a picture of text again... post your code using code tags...

Basically yes. But my RX_Buffer need to be a string!

The code in LCD:

print "$"  // start char
print "5"  // page no
print "0"  // send the ID
print "d"  // dummy char «
print "&"  // end char

The code I have on arduino of the RX buffer:
image
The rest of the code dependes how I receive that information.

I don't know why it would need to be a String but with

while (Serial3.available())
  {
    RX_Buffer += char(Serial3.read());
    Serial.println(RX_Buffer);      
  }

you print the buffer as it grows and you are not even sure you got everything that was sent since the while loop will likely run faster than the 38400 bauds you have on the other end

I change the code, that on below is correct:

while (Serial3.available())
  {      
    RX_Buffer += char(Serial3.read());
    //Serial.println(RX_Buffer);      
  }    
  Serial.println(RX_Buffer);

you still have the issue that the buffer might not be complete as you can read faster than the data is coming in. if you know the end marker of the communication, you should wait for this end marker before you print your RX_Buffer (otherwise the next time you read you'll have the leftover from the previous communication coming in)

The problem continues. The diference is the end marker is printed slower at the time.

how did you fix the potential incomplete read?

In teory this have to run correcty, but in practice no.

String RX_Buffer = "";
String C = "";
if (Serial3.available())
{
  delay(100);
  while (Serial3.available())
  {            
  C = char(Serial3.read());
  if(C != "⸮")
  {
    RX_Buffer += C;
    Serial.println(RX_Buffer);
  }          
} 

When the marker apears the buffer dont grow in teory.

this is trying to second guess the timing of an asynchronous process...

the incoming buffer has only 64 slots, at 38400 you might have received 384 bytes in that time and lost a lot of them...

I would suggest to study Serial Input Basics to understand how the Serial port should be handled


this is not going to do what you want

  if(C != "⸮")

the you see is generated by the Serial monitor, it does not mean this is the character you got (and this is not a standard ASCII one char anyway, more an UTF8 thingy, so you have zero chance of getting it whilst reading only 1 byte)

But the end marker of the communication not allways apear.Its why I could't know when the comunication or the data of the LCD it's over.

What’s the end marker?

When I refer the end marker is the "⸮⸮⸮" simbols!
But only ocurs on some pages, not all of them. And some times when I change between some pages the symbols stop apearing on the arduino serial port.