From 6a67d9fd610a2269a0fbd62fcc1538efb73f22f2 Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 14:06:16 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B4=D0=B2=D1=83=D1=85=20=D0=BF=D0=BE=D1=80?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TwoPortReceive/TwoPortReceive.ino | 95 +++++++++++-------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino index ad9c143..62a57cc 100644 --- a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino +++ b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino @@ -20,65 +20,80 @@ Not all pins on the Elbear Ace-Uno support interrupts, so only the following can be used for RX: 2, 3, 4, 5, 8, 9 - - created 18 Apr. 2011 - modified 19 March 2016 - by Tom Igoe - based on Mikal Hart's twoPortRXExample - - This example code is in the public domain. - */ #include + // software serial #1: RX = digital pin 2, TX = digital pin 6 SoftwareSerial portOne(2, 6); - // software serial #2: RX = digital pin 8, TX = digital pin 10 SoftwareSerial portTwo(8, 10); -void setup() { - // Open serial communications and wait for port to open: +// active port is changed by user button pressing +volatile bool isPortOneActive = true; +void btn_pressed_callback(void) +{ + // change active port + if (isPortOneActive) + { + portTwo.listen(); + Serial.println("Port two is listening"); + isPortOneActive = false; + } + else + { + portOne.listen(); + Serial.println("Port one is listening"); + isPortOneActive = true; + } +} + +void setup() +{ + // Open serial communications and wait for port to open Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } + // attach interrupt to user button + attachInterrupt(digitalPinToInterrupt(BTN_BUILTIN), btn_pressed_callback, FALLING); + // Start each software serial port portOne.begin(57600); portTwo.begin(57600); - portTwo.listen(); - delay(2000); + // First listen to port one + portOne.listen(); + isPortOneActive = true; + Serial.println("Port one is listening"); } -void loop() { - // By default, the last initialized port is listening. - // when you want to listen on a port, explicitly select it: - portOne.listen(); - Serial.println("Data from port one:"); - // while there is data coming in, read it - // and send to the hardware serial port: - while (portOne.available() > 0) { - char inByte = portOne.read(); - Serial.write(inByte); +void loop() +{ + // check data from port one + if (portOne.available()) + { + Serial.println("Data from port one:"); + // while there is data coming in, read it + // and send to the hardware serial port + while (portOne.available() > 0) + Serial.write((char)portOne.read()); + + // blank line to separate data from the two ports + Serial.println(); } - // blank line to separate data from the two ports: - Serial.println(); - delay(2000); - - // // Now listen on the second port - // portTwo.listen(); - // // while there is data coming in, read it - // // and send to the hardware serial port: - // Serial.println("Data from port two:"); - // while (portTwo.available() > 0) { - // char inByte = portTwo.read(); - // Serial.write(inByte); - // } - - // // blank line to separate data from the two ports: - // Serial.println(); - // delay(2000); + // check data from port two + if (portTwo.available()) + { + Serial.println("Data from port two:"); + // while there is data coming in, read it + // and send to the hardware serial port + while (portTwo.available() > 0) + Serial.write((char)portTwo.read()); + + // blank line to separate data from the two ports + Serial.println(); + } }