Go Down

Topic: Nextion Baud Rate help please (Read 168 times) previous topic - next topic

g0thy

I am using a Nextion display with an Arduino Mega (a genuine one).
They are communicating with each other fine, but I would like to increase the baud rate.
I can change the Nextion baud rate with the Nextion editor but not with the Arduino.

I have tried
Serial2.begin(38400);
&
nexSerial.begin(38400);
 and it still communicates at 9600.

Using this it still communicates at 9600
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);

the  only way I can change the baud rate of the display is with the Nextion editor

If i put Serial2.end(); the serial connection still works.

It's like none of the serial commands are working.
I have tried 2 different Megas and the result is the same.
I think the problem with the Mega not the display

Martyn

PerryBebbington

I am using a Nextion display with an Arduino Mega (a genuine one).
They are communicating with each other fine, but I would like to increase the baud rate.
I can change the Nextion baud rate with the Nextion editor but not with the Arduino.

I have tried
Code: [Select]
Serial2.begin(38400);
&
nexSerial.begin(38400);

And it still communicates at 9600.

Using this it still communicates at 9600
Code: [Select]
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);


The  only way I can change the baud rate of the display is with the Nextion editor

If I put Serial2.end(); the serial connection still works.

It's like none of the serial commands are working.
I have tried 2 different Megas and the result is the same.
I think the problem with the Mega not the display

Martyn
Martyn,
I've never tried changing the baud rate 'on the fly' but in the first of your examples you are changing it at the Arduino end but not at the Nextion end, and in the second you are changing it at the Nextion not the Arduino. You have to change both and you have to change the Nextion first then the Arduino (Because if you change the Arduino first it will be sending to the Nextion at a different rate to what it expects, so it won't understand the instruction).

I've not checked this but I'd think this or something similar would do it:

Code: [Select]

Serial2.begin(9600);
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.begin(38400);



PerryBebbington

Code: [Select]
Serial2.begin(9600);
Serial2.print("baud=38400");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.begin(38400);


Sorry I thought of something after I posted that. Not sure but maybe need a delay before
Code: [Select]
Serial2.begin(38400);
Reason is that without a delay you are trying to change the baud rate of the serial port while it is still sending the previous data. I don't know how it behaves when you do this but I suspect it's not good.

g0thy

Thanks Perry

I had tried changing the Nextion baud rate using serial print. That worked but I couldn't change the Arduino Mega baud rate so they wouldn't communicate - the Mega still at 9600.
I could also change the Nextion Baud using the nextion editor and that worked.

My problem is I don;t seem to be able to change the Mega baud rate. I have monitored the serial connection with an oscilloscope and there is no change from the Mega.

oddly, commands like Serial2.end() doesn't work either.

Is like the Mega serial commands don't work.

I will try putting a delay in next week.

Martyn

PerryBebbington

Thanks Perry

I had tried changing the Nextion baud rate using serial print. That worked but I couldn't change the Arduino Mega baud rate so they wouldn't communicate - the Mega still at 9600.
I could also change the Nextion Baud using the nextion editor and that worked.

My problem is I don;t seem to be able to change the Mega baud rate. I have monitored the serial connection with an oscilloscope and there is no change from the Mega.

oddly, commands like Serial2.end() doesn't work either.

Is like the Mega serial commands don't work.

I will try putting a delay in next week.

Martyn
Hi Martyn,
I can't help with the Mega baud rate, I have never had one, sorry.

I am a bit curious as to why you don't just set the baud rate at the start and leave it at that.

Perry

PerryBebbington

#5
Dec 15, 2018, 11:43 am Last Edit: Dec 15, 2018, 11:47 am by PerryBebbington
Hello Martyn

I've been playing to try to get an answer for you. I have a Nextion with 2 text boxes with objname as T0 and T1. The code below sends odd numbers to T0 at 9600 baud and even number to T1 at 19200 baud, I have checked this on an oscilloscope and it does keep changing baud rate. Ignore the mess of delays and millis, I put this together in a hurry from some other code used to test something else, I just wanted something that proved it worked, not something neat and tidy. Play around with this, change the delays, see if they are needed at all.

This is on a Wemos SAMD21

Code: [Select]

unsigned long lastmillis0 = millis();
unsigned long lastmillis1 = millis();
uint8_t testout = 0;
void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  SerialUSB.begin(9600);
  Serial.begin(9600);
}

void loop() {

  if (lastmillis0 + 500 < millis()) {
    lastmillis0 = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
  if (lastmillis1 + 2000 < millis()) {
    lastmillis1 = millis();

    ++testout;
    Serial.print("baud=9600");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    delay(50);
    Serial.begin(9600);

    Serial.print("T0.txt=\"");
    Serial.print(testout);
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);

    delay(1000);

    ++testout;
    Serial.print("baud=19200");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    delay(50);
    Serial.begin(19200);

    Serial.print("T1.txt=\"");
    Serial.print(testout);
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}

Go Up