/*
  Test sketch for RS485 interface
*/

#include <SPI.h>
#include <TFT_eSPI.h>      // Graphics library

#define mp(x) tft.print(F(x))
#define mpl(x) tft.println(F(x))

#define sp(x) tft.print(x)
#define spl(x) tft.println(x)

TFT_eSPI tft = TFT_eSPI(); // Invoke library
/* DJ Settings
  #define LOAD_GLCD   // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
  #define LOAD_FONT2  // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
  #define LOAD_FONT4  // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
  #define LOAD_FONT6  // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
  #define LOAD_FONT7  // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:.
  #define LOAD_FONT8  // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
  #define LOAD_GFXFF  // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts
*/

setup_t user; // The library defines the type "setup_t" as a struct
// Calling tft.getSetup(user) populates it with the settings
//------------------------------------------------------------------------------------------

#define timer(p1) sp(p1);spl(millis())

void setup() {
  // Use serial port
  //
  
  Serial2.begin(9600);
  delay(300);
  // Set the font colour to be green with black background
  tft.setTextColor(TFT_BLUE, TFT_WHITE);
  mpl("\n\n=== RS485_1 Software Created " __DATE__" " __TIME__ );
  timer("Starting:");
  spl("Initialising Screen ");
  screenInit();
  timer("Initialised:");
  clearScreen();
  timer("Cleared screen:");

}

void loop() { 
  static int offset = 0;

  // Set the font colour to be green with black background
  tft.setTextColor(TFT_BLUE, TFT_WHITE);
  /*
  tft.setCursor(offset, 0, 2);
  for (int i = 0; i < 10; i++) {
    tft.setCursor(50, i * 15);
    tft.print("Line ");  tft.println(i,HEX);
    //             x   y

  }
  tft.println("Last line");
  offset += 10;
  */
  test();
  delay(300);
  

  clearScreen();
  tft.setTextSize(2);

}

//------------------------------------------------------------------------------------------
void clearScreen() {

  timer("Clear screen:");
  tft.setCursor(0, 0);
  tft.setTextSize(1);
  tft.fillScreen(TFT_BLACK);
}

void screenInit() {
  // Initialise the TFT screen
  tft.init();
  tft.getSetup(user); //
  tft.setRotation(0);

  tft.fillScreen(TFT_BLACK);
  // Set "cursor" at top left corner of display (0,0)
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(0, 0);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  //tft.setTextColor(TFT_WHITE, TFT_BLACK);
  uint16_t fonts = tft.fontsLoaded();
    tft.setTextFont(2);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE, TFT_BLACK);

  // We can now plot text on screen using the "print" class

  tft.setCursor(0, 0);
  tft.fillScreen(TFT_BLACK);
  tft.setTextSize(2);
  tft.println("Screen initialised");


}


// Function to calculate Modbus RTU CRC
int calculateModbusCRC(const char* data, int len) {
  int crc = 0xFFFF; // Initial value

  for (int i = 0; i < len; i++) {
    crc ^= static_cast<uint8_t>(data[i]); // XOR with current byte

    for (int j = 0; j < 8; j++) {
      if (crc & 0x0001) {
        crc = (crc >> 1) ^ 0xA001; // Right shift and XOR with polynomial
      } else {
        crc = crc >> 1; // Right shift
      }
    }
  }
  // Display the calculated CRC
  tft.print ("Modbus RTU CRC: " );
  tft.println (crc, HEX);
  return crc;
}


// Function to send byte array with Modbus RTU CRC to Serial2
void sendToSerial2(const char* data, int len) {
  // Calculate Modbus CRC
  uint16_t crc = calculateModbusCRC(data, len);

  // Send data bytes
  for (int i = 0; i < len; i++) {
    byte b = data[i];
    tft.print(i); tft.print("=");
    tft.println(b, HEX) ; //This omits leading zeros so hex 00 shows as 0 etc
    Serial2.print(b);
  }

  // Send CRC low byte
  tft.print(crc & 0xFF, HEX);
Serial2.print(crc & 0xFF);
  // Send CRC high byte
  tft.print((crc >> 8) & 0xFF, HEX);
  Serial2.print((crc >> 8) & 0xFF);
}

void test() {
  // Example usage

  //:01 04 80 E8 00 0C 59 FB
  /*000218-Tx:01 04 80 E8 00 0C 59 FB
    000219-Rx:01 04 18 00 F8 00 0C 00 0E 00 01 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 00
    85 A4
    000230-Tx:01 04 81 5B 00 0A 29 E2
    000231-Rx:01 04 14 00 00 00 04 00 00 00 01 00 00 00 04 06 08 00 00 00 00 00 00 EE EB
    000242-Tx:01 04 81 05 00 0A 48 30
    000243-Rx:01 04 14 00 00 00 71 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 71 21 EE
  */

  const char data[] = {0x01, 0x04, 0x80, 0xE8, 0x00, 0x0C};
  //expected Checksum :  0x59, 0xFB
  sendToSerial2( data, 6 );
}





Loading
ili9341-cap-touch