Start a new topic

Arduino sketch to type commands for the display in the Arduino serial monitor.

Sometimes I would like to try a few commands, but the Nextion display is in the enclosure and I can't get to the connector easily.


Therefor I made a sketch that passes commands from the Arduino serial monitor via the Arduino board to the display, and also shows the returned data from the Nextion display.


I don't know what to call this sketch. I called it "Nextion Pass Through", but that could mean many different things.


//
// Nextion Pass Through
// --------------------
//
// Arduino sketch for typing commands for the display
// in the Arduino serial monitor.
// The returned data is displayed as hexadecimal and text.
//
//
// Version 1.0, 12 June 2017
//   by Koepel
//   Public Domain.
//   No timeout implemented yet, I think it is not needed.
//
//
// Double quotes can be used without problem.
// Command examples:
//   sendme                              (shows the current page)
//   connect                             (shows information)
//   fill 60,60,200,120,BLUE             (fill blue area)
//   xstr 60,86,200,64,0,WHITE,RED,1,1,1,"Hello"  (show text with font 0)


// Remove the comment for the right serial port to the Nextion display.
// The yellow wire of the Nextion display is the Nextion RX.
// The blue   wire of the Nextion display is the Nextion TX.
// Connect the Nextion RX to the Arduino TX.
// Connect the Nextion TX to the Arduino RX.


// ----------------------------------------------------------------
// When using Arduino Mega 2560 and the Serial1 port
// to the Nextion display, then a #define for "nexSerial" enough.
// Mega Serial1 is pin 19 (Arduino RX) and 18 (Arduino TX)
// Mega Serial2 is pin 17 (Arduino RX) and 16 (Arduino TX)
// Mega Serial3 is pin 15 (Arduino RX) and 14 (Arduino TX)
// ----------------------------------------------------------------
// #define nexSerial Serial1


// ----------------------------------------------------------------
// The SoftwareSerial can be used on most pins.
// The SoftwareSerial parameters are: Arduino RX, Arduino TX.
// For example SoftwareSerial( 10, 11) sets 
// pin 10 (Arduino RX) and 11 (Arduino TX).
// ----------------------------------------------------------------
#include <SoftwareSerial.h>
SoftwareSerial nexSerial = SoftwareSerial( 10, 11);


// ----------------------------------------------------------------
// When using AltSoftSerial the pin numbers are specified by the library.
// Arduino Uno: pin 8 (Arduino RX) and 9 (Arduino TX).
// ----------------------------------------------------------------
// #include <AltSoftSerial.h>
// AltSoftSerial nexSerial;



boolean emptyline = true;
int countFF = 0;
byte buffer[80];
int index = 0;


void setup() 
{
  Serial.begin( 9600);        // to the Arduino serial monitor
  nexSerial.begin( 9600);     // to the display

  Serial.println();
  Serial.println(F( "Nextion Pass Through"));
  Serial.println(F( "Set LineFeed at the end of the line from the serial monitor"));
  Serial.println(F( "The LineFeed will be translated to 0xFF,0xFF,0xFF"));
  Serial.println(F( "Type commands for the display"));


  // Send empty command to display.
  delay(100);
  nexSerial.print(F( "\xFF\xFF\xFF"));

  // Clear serial buffer from display.
  delay(100);
  while( nexSerial.available())
    nexSerial.read();
}


void loop() 
{
  if( Serial.available())   // something received from the serial monitor ?
  {
    int inChar = Serial.read();
    
    if( inChar == '\n' || inChar == '\r')
    {
      if( !emptyline)  // was a command transmitted ?
      {
        nexSerial.print(F( "\xFF\xFF\xFF"));
        emptyline = true;   // start with new line
      }
    }
    else if( isprint( inChar))  // only printable and readable text is accepted
    {
      emptyline = false;
      nexSerial.write( inChar);  // pass it on to the display
    }
  }  

  // The data from the display can be the return value of a command.
  // It can also be a touch event.
  if( nexSerial.available())  // check data from display
  {
    int returnChar = nexSerial.read();  // read data from display
    
    Serial.print(F( "0x"));
    if( returnChar < 10)
      Serial.print(F( "0"));
    Serial.print( returnChar, HEX);  // print data as hex byte
    Serial.print(F( " "));

    if( isprint( returnChar))
    {
      buffer[index] = (byte) returnChar;
    }
    else
    {
      buffer[index] = '.';
    }
    
    index++;                        // advance index
    if( index >= sizeof( buffer))   // clip index to maximum
      index = sizeof( buffer) - 1;

    if( returnChar == 0xFF)
    {
      countFF++;           // count the 0xFF
      if( countFF >= 3)    // three consequtive 0xFF ?
      {
        Serial.println();  // new line to serial monitor

        // Don't print the trailing 0xFF
        if( index > 3)
          index -= 3;
        buffer[index] = '\0'; // zero-terminate the text.
        Serial.println( (char *) buffer); // print received data as text
        index = 0;         // reset index of buffer.
        countFF = 0;       // reset counting the 0xFF
      }
    }
    else
    {
      countFF = 0;      // the data is not 0xFF, reset the counter.
    }
  }
}

 


1 person likes this idea
Login or Signup to post a comment