Last active
November 5, 2023 07:18
-
-
Save maxpromer/1742289fc3175df454c99230d3597830 to your computer and use it in GitHub Desktop.
Serial1 example code on ESP32 (Arduino platform)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define RXD1 (25) // กำหนดขา RX ของ Serial1 เป็นขา 25 | |
#define TXD1 (26) // กำหนดขา TX ของ Serial1 เป็นขา 26 | |
void setup() { | |
Serial.begin(115200); // เริ่มต้นใช้งาน Serial0 ที่ความเร็ว (baud rate) 115200 | |
Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1); // เริ่มต้นใช้งาน Serial1 ที่ความเร็ว (baud rate) 9600 พร้อมกำหนดขา RX, TX ใหม่ | |
} | |
void loop() { | |
while (Serial.available()) { // วนลูปหากมีข้อมูลเข้ามาจาก Serial0 (จาก Serial Monitor) | |
Serial1.write(Serial.read()); // อ่านค่าจาก Serial0 แล้วเอาค่าที่อ่านได้เขียนไปที่ Serial1 | |
} | |
while (Serial1.available()) { // วนลูปหากมีข้อมูลเข้ามาจาก Serial1 (จาก Serial Monitor) | |
Serial.write(Serial1.read()); // อ่านค่าจาก Serial1 แล้วเอาค่าที่อ่านได้เขียนไปที่ Serial0 | |
} | |
delay(10); // หน่วงเวลา เพื่อป้องกัน WDT timeout | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment