Serial communication Nextion

hello. I'm trying to send and receive data from nextion 2.8-inch display. for now, my communication is a success in sending and receiving. but I also received another unexpected value. i want to block that unidentified serial string.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
float man,s,v1,b, R, a, c, L, M, N, Rrefer = 10, sum=0;

String datanextion = "";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {

for(int i=0; i<6000; i++){
  v1 = analogRead(A0);
  v1= (v1/1023)*3.3;
  R =((3.3-v1)*10)/v1;
 //b = (2.5346*R)-12.622;
 //a = (1.36481*b)+0.30025;
 //c = (0.9453*a)-1.7961;
 L = (0.76781*R) - 4.32278;
 M = (0.7418*L) + 0.60089;
 N = (1.11965*M) + 0.47761;
  
  sum=sum+N;
  //sum=sum+R;
  }

  sum=sum/6000;

 if (mySerial.available() > 0) {
    datanextion = "";
    delay(30);
    while (mySerial.available() > 0) {
      datanextion += char(mySerial.read());
    }
   


    Serial.print("product number : ");
    Serial.print(datanextion);
    Serial.print("  Resistance : ");
    Serial.println(sum);



 }
//Serial.println(a);

mySerial.print("x0.val=");
mySerial.print(b);
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
 sum=0;
//delay(250);
} 

Any kind of help is much appreciated.

save
yes, enter is the one I need. also, I need to divide it to yes and enter because I send them separate times. The other character (in product number) is unwanted. I need a perfect serial print here. thank you

@ravirulz

I've moved your question to the Display section of the forum as that is where Nextion questions usually go.

What exactly is your unexpected string? Nextion displays do not send out 'unexpected' strings, well, OK you might not expect them but they are for a reason. Correctly configured, with appropriate code on your Arduino they only send things you want and need. If you are getting something you don't expect then you need to understand why and correct that.

1 Like

I see nothing unexpected, as far as string printing binary data allows me to see,
maybe the answer of the Nextion is OK (x0.val was changed),
or invalid variable (if x0 does not exist on the current page)?

1 Like

Yes. I tried but failed for hours. in the serial monitor,

⸮⸮⸮

this part is unwanted. I have tried to remove that but failed. nextion debug doesn't show any kind of thing like this. thank you

Ah, I have a numeric keypad and it may cause this. any idea how to fix this

You should study the linked documentation of the Nextion.

1 Like

I think that is the 0xff 0xff 0xff that the Nextion sends with its data. The serial monitor can't display it properly so you get that.

There's nothing to fix, you just have to handle it in your code.

I have a tutorial at the top of the display section, in there you will find my methods and, in case you prefer them, a link so another tutorial by Seithan, he does things in a completely different way to me. Pick whichever suits you best. I am pretty sure @Whandall also has his way of doing things too.

1 Like

I'm trying to but I need to deliver it in a few hours.

I think you have set yourself an impossible target. I don't know exactly what you are doing, but I know I'd allocate myself a lot more than a few hours to a Nextion project. I've got a curry to make for a friend shortly, so my replies are going to stop soon...

1 Like
#include <WhandallSerial.h>  // https://github.com/whandall/WhandallSerial

// simple handler just prints some infos about the received line

void processLine(const char* buf) {
  Serial.print(F("len = "));
  Serial.print((uint8_t)buf[-1]);
  Serial.print(F(", strlen = "));
  Serial.println(strlen(buf));
  Serial.println(buf);
}

SSerial nextionSerial(Serial1, processLine); // used serial and handler

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);
  nextionSerial.begin(64, optTripleFF); // buffer size and options, maybe +optKeepDlm
}

void loop() {
  nextionSerial.loop();    // collect chars and call handler for each line
}

But that does not work with SoftwareSerial.

1 Like

You are late to the game, and I would guess from the quality of your try, too late.

1 Like

ok sir. thank you

But i cannot use tx rx main ports in uno boards sir.

Hmm but I'm not giving up. im really close. i feel it.

If you just want to get rid of the non printable stuff...

change to...

    byte b;
    while (mySerial.available() > 0) 
    {
      b = mySerial.read();
      if (b < 128)
        datanextion += char(b);
    }

It's most likely that you are receiving the product number as binary... so it would be better to decode that.

1 Like

And hope that there is no byte above 127 in that code, if you use the crude filter.

if (b != 0xFF) is only a little better.

The fact that the three 0xFF's can occur inside data, also needs some special consideration.

1 Like

to be honest I'm not sure what they are trying to do... and there is no clear view on what the data expected is. The primary directive seems to be

1 Like

You should absolutely not use String concatenation for the reception on an UNO.

1 Like

Thanks a lot. but I have figured it out on my own. thanks a lot, guys.
This is my final code

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
float man,s,v1,b, R, a, c, L, M, N, Rrefer = 10, sum=0;
String endchar = String(char(0x1A))+String(char(0xff)) + String (char(0xff)) + String(char(0xff));

String datanextion = "";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {

 // if(i==19){i=0;}
 
 for(int i=0; i<6000; i++){
  v1 = analogRead(A0);
  v1= (v1/1023)*3.3;
  R =((3.3-v1)*10)/v1;
 //b = (2.5346*R)-12.622;
 //a = (1.36481*b)+0.30025;
 //c = (0.9453*a)-1.7961;
 L = (0.76781*R) - 4.32278;
 M = (0.7418*L) + 0.60089;
 N = (1.11965*M) + 0.47761;
  
  sum=sum+N;
  //sum=sum+R;
  }

  sum=sum/6000;

 if (mySerial.available() > 0) {
    datanextion = "";
    delay(30);
    while (mySerial.available() > 0) {
      datanextion += char(mySerial.read());
    }
   

if(datanextion!=endchar ){
    Serial.print("product number : ");
    if((datanextion[0]==char(0x1A))&&(datanextion[1]==char(0xFF))&&(datanextion[2]==char(0xFF))&&(datanextion[3]==char(0xFF))){
    datanextion.remove(0,4);}
    Serial.print(datanextion);
    Serial.print("  Resistance : ");
    Serial.println(sum);
}


 }
//Serial.println(a);
int m=sum*100;
mySerial.print("x0.val=");
mySerial.print(m);
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
 sum=0;
delay(250);
}