Start a new topic

Do not receive 0XFE from Nextion 3.2 after addt command.

Can someone help me with a working example?

Or someone can point me in the right direction.

Thanks in advance!

The example that I am trying to get working:


#include "Nextion.h"

NexWaveform s0 = NexWaveform(0, 1, "s0");

uint8_t my_serial_bytes[11]={0x61, 0x64, 0x64, 0x74, 0x20, 0x31, 0x2c, 0x30, 0x2c, 0x64};// addt 1,0,100
void setup(void)
{
  Serial.begin (19200);
  Serial2.begin (115200);

  nexInit();

}

void loop(void)
{

uint8_t  Nex_stat; uint8_t i; uint8_t stat; String nexString;
char tmpString[50]; uint8_t x_read1, x_read2, x_read3, x_read4, x_read5;
uint8_t indeks;
  Serial.write("addt 1,1,100");//where we @?
  Serial.println("");
  while (!Serial.available());
  stat = Serial.read();
  if (stat ==  'A')
  {
  for (indeks = 0; indeks < 10; indeks++)
  Serial.write(my_serial_bytes[indeks]);
  Serial.write(0xFF);
  Serial.write(0xFF);  //Send to terminal first.
  Serial.write(0xFF);
  Serial.println("");
  for (indeks = 0; indeks < 10; indeks++)
  Serial2.write(my_serial_bytes[indeks]);
  Serial2.write(0xFF);
  Serial2.write(0xFF);  //Send to Nex
  Serial2.write(0xFF);
   
/*
  wait 10 ms for NEXTION response 1 byte and the byte is 0xFE (see addt instruction)
*/

  delayMicroseconds(10000);
  while(Serial2.available() > 0)
  x_read1 = Serial.read();
  x_read2 = Serial.read();
  Serial.println(x_read1);
  Serial.println(x_read2);
   
  if (x_read1 ==  0xFE)  
  {
  //put a block of 100byte to NEXTION
  for (i=1;i<100;i++)
  {
  Serial2.write(i);  //put 1 byte
  }
  }
  }
}
  

I write ("addt 1,1,100") to serial monitor to see where the program is.


"addt 1,1,100" is in a uint8_t array   my_serial_bytes{};


I have a serial monitor read to pause program waiting for "A".


First I write command addt with arguments followed by ff ff ff to serial monitor

then write to the Nextion 3.2


Waiting for and reading return status I get 0 and 255.


This is the serial monitor display:

addt 1,1,100  
addt 1,0,dÿÿÿ
0
255
addt 1,1,100





addt 1,1,100  

addt 1,0,dÿÿÿ  <=  The third parameter is a d, this should be 100 as text

0

255

addt 1,1,100


addt 1,1,100ÿÿÿ

will return 0xFE 0xFF 0xFF 0xFF

important this will need to read in 4 bytes

this is a uint32_t type returned

when complete and the 100 bytes are sent

this too is a uint32_t type return 4 bytes

and will return 0xFD 0xFF 0xFF 0xFF


logic is flawed in loop.

 - on every byte received you want to send out 100 bytes on each bytes.

 - but first you must receive the ready to send before sending.

 

 

#include "Nextion.h"

byte ter[3] = {255,255,255}
byte buf[10] = {0};

void setup(void) {
  Serial.begin(19200);
  Serial2.begin(115200);
}

void loop (void) {
   int i, x = 0;
   Serial2.print("addt 0,1,100");
   Serial2.write(ter,3);
   i = 0;
   while (Serial2.available < 4) { };
   if (Serial2.available > 3) { // wait for 4 bytes or more
     while (Serial2.available > 0) {
        buf[i] = Serial2.read();
        i++;
     }
     buf[i] = 0x00;
     if (buf[0]==0xFE && buf[1]==0xFF && buf[2]==0xFF && buf[3]==0xFF) {
       // now that the ready to send has been received send 100 bytes
       for(x=0;x<100;x++) {
          Serial2.write((byte)x);
       }
       // now that requested 100 bytes sent wait for Nextion 0xFD
       while (Serial2.available < 4) { };
       while (Serial2.available > 0) {
          buf[i] = Serial2.read();
          i++;
       }
       buf[i] = 0x00;
       if (buf[0]==0xFD && buf[1]==0xFF && buf[2]==0xFF && buf[3]==0xFF) {
          // transmission is finished
       }
     }
   }
}

 

 

Code not tested - but something in this manner


Thanks for the great help! I did not say, but I am using arduino mega. In the Nextion editor I created a

simple waveform horizontal mode. Loaded the hmi to the Nextion.

If anyone else needs help I am placing the code that I was able to get working with your help.

I hope the comments help. upload to mega. open a serial monitor, you will see "addt 1,1,100"

type in A in monitor, and a single ramp will show up on display. type A again, and another ramp


#include "Nextion.h"

NexWaveform s0 = NexWaveform(0, 1, "s0");
byte buf[10] = {0};

void setup(void)
{
  Serial.begin (19200);
  Serial2.begin (115200);

  nexInit();
}

void loop(void)
{
int buff_idx = 0;

uint8_t i; uint8_t stat;   
  Serial.write("addt 1,1,100");//print to serial monitor to see where we @?
  Serial.println("");//newline on serial monitor
  while (!Serial.available());//this is a just pause through each loop
  stat = Serial.read();//on serial monitor type A to continue
  if (stat ==  'A')  //draw a ramp each iteration and creat a sawtooth
  {
  Serial.print("addt 1,0,100");
  Serial.write(0xFF);  //
  Serial.write(0xFF);  //Send to terminal first.
  Serial.write(0xFF);  //
  Serial.println("");  // newline
   
  Serial2.print("addt 1,0,100");
  Serial2.write(0xFF);  //
  Serial2.write(0xFF);  //Send to Nextion
  Serial2.write(0xFF);  //
/*
  wait 10 ms for NEXTION response 0xFE followed by 0xFF 0xFF 0xFF read all 4 from Nextion
*/
  delayMicroseconds(10000);

  while(Serial2.available() < 4)//if less than 4 bytes
  {};
  if(Serial2.available() > 3)//wait for 4 bytes or more
  while(Serial2.available() > 0)//start reading from Nextion
  {
  buf[buff_idx] = Serial2.read();//read 0xFE 0xFF 0xFF 0xFF
  buff_idx++;
  }
  buf[buff_idx] = 0x00;
  if (buf[0]== 0xFE && buf[1]==0xFF && buf[2]==0xFF && buf[3]==0xFF)//Did we get them?  
  {  //Data transparent transmit ready
  //put a block of 100byte to NEXTION if we did!
  for (i=0;i<100;i++)
  {
  Serial2.write((byte)i);  //put 1 byte at a time and draw a ramp
   
  }//100 bytes sent
  buff_idx = 0;//set buf index back to zero
  while (Serial2.available() < 4)//waiting for 4 bytes
  { };
  while (Serial2.available() > 0)
  {  //
  buf[buff_idx] = Serial2.read();//getting 0xFD 0xFF 0xFF 0xFF
  buff_idx++;
  }
  buf[buff_idx] = 0x00;
  if (buf[0]==0xFD && buf[1]==0xFF && buf[2]==0xFF && buf[3]==0xFF)
  {  //Data transparent transmit finished
  // transmission is complete
  }
   
  }
   
  }
}




Login or Signup to post a comment