CSerialPort tutorial (10) - DTR and RTS functions of CSerialPort

CSerialPort tutorial (10) - DTR and RTS functions of CSerialPort

foreword

The CSerialPort project is a lightweight open source cross-platform serial port class library based on C++, which is used to realize serial port reading and writing across platforms and multiple operating systems.

The open source protocol of the CSerialPort project has adopted the GNU Lesser General Public License v3.0 since version V3.0.0.171216

In order to allow developers to better use CSerialPort for development, a CSerialPort tutorial series based on version 4.x is specially written.

This article will introduce how to use the DTR and RTS functions of CSerialPort.

Note: The DTR and RTS functions of CSerialPort require v4.3.0 and above.

CSerialPort project address:

1. Hardware preparation

Development board: Hezhou ESP32C3 classic version
IDE: Arduino IDE 2.1.0 ([email protected])
USB driver: CH343SER

2. DTR and RTS test program of ESP32C3

Function:

  • When DTR is high, D4 or D5 lights up and prints the string DTR 1 on the serial port
  • When DTR is low, D4 and D5 light up alternately every 1 second, and print the string DTR 0 on the serial port
  • When RTS is low, ESP32C3 restarts and prints the string INIT on the serial port

code:

  • CSerialPort_DTR_RTS.ino
// test DTR and RTS for CSerialPort(https://github.com/itas109/CSerialPort)
#define LED_D4_PIN 12   // D4
#define LED_D5_PIN 13   // D5
#define BTN_BOOT_PIN 9  // BOOT Button

// BOOT <---> DTR
// RST  <---> RTS

// DTR(1) => D4 or D5 on
// DTR(0) => D4 D5 alternate flashing per 1 second
// RTS(1) => nothing
// RTS(0) => ESP32 restart, then serial print "INIT"

void setup() {
  Serial.begin(9600);
  Serial.println("INIT");

  pinMode(BTN_BOOT_PIN, INPUT);
  pinMode(LED_D4_PIN, OUTPUT);
  pinMode(LED_D5_PIN, OUTPUT);

  digitalWrite(LED_D4_PIN, HIGH);
  digitalWrite(LED_D5_PIN, LOW);
}

void loop() {
  if (LOW == digitalRead(BTN_BOOT_PIN)) {
    digitalWrite(LED_D4_PIN, !digitalRead(LED_D4_PIN));
    digitalWrite(LED_D5_PIN, !digitalRead(LED_D5_PIN));
    Serial.println("DTR 0");
    delay(1000);
  } else {
    Serial.println("DTR 1");
    delay(1000);
  }
}

3. CSerialPort test program

Here use the example code in CSerialPort (CSerialPort/examples/CommQT)

Note: The DTR and RTS functions of CSerialPort require v4.3.0 and above.

Test steps:

  • Compile and run CommQT
  • Select the serial port of ESP32C3, the baud rate is 9600
  • Select DTR, D4 and D5 light up alternately every 1 second, and print the string DTR 0 on the serial port
  • Uncheck DTR, D4 or D5 lights up, and print the string DTR 1 on the serial port
  • Select RTS, ESP32C3 restarts, and prints the string INIT on the serial port

Reference:

  1. https://github.com/itas109/CSerialPort
  2. https://gitee.com/itas109/CSerialPort
  3. https://blog.csdn.net/itas109

Guess you like

Origin blog.csdn.net/itas109/article/details/130927514