Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Created November 5, 2023 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxpromer/ca972ee47679a2eaac46932ec66bcd97 to your computer and use it in GitHub Desktop.
Save maxpromer/ca972ee47679a2eaac46932ec66bcd97 to your computer and use it in GitHub Desktop.
Serial2 example code on ESP32 (Arduino platform)
#define RXD2 (16) // กำหนดขา RX ของ Serial2 เป็นขา 16
#define TXD2 (17) // กำหนดขา TX ของ Serial2 เป็นขา 17
void setup() {
Serial.begin(115200); // เริ่มต้นใช้งาน Serial0 ที่ความเร็ว (baud rate) 115200
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); // เริ่มต้นใช้งาน Serial2 ที่ความเร็ว (baud rate) 9600 พร้อมกำหนดขา RX, TX ใหม่
}
void loop() {
while (Serial.available()) { // วนลูปหากมีข้อมูลเข้ามาจาก Serial0 (จาก Serial Monitor)
Serial2.write(Serial.read()); // อ่านค่าจาก Serial0 แล้วเอาค่าที่อ่านได้เขียนไปที่ Serial2
}
while (Serial2.available()) { // วนลูปหากมีข้อมูลเข้ามาจาก Serial2 (จาก Serial Monitor)
Serial.write(Serial2.read()); // อ่านค่าจาก Serial2 แล้วเอาค่าที่อ่านได้เขียนไปที่ Serial0
}
delay(10); // หน่วงเวลา เพื่อป้องกัน WDT timeout
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment