пример для двух портов работает

This commit is contained in:
klassents committed 2024-10-13 14:06:16 +07:00
1 parent 9efc6c5adf
commit 6a67d9fd61
1 file changed
+54 -39
@@ -20,65 +20,80 @@
Not all pins on the Elbear Ace-Uno support interrupts, Not all pins on the Elbear Ace-Uno support interrupts,
so only the following can be used for RX: so only the following can be used for RX:
2, 3, 4, 5, 8, 9 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 <SoftwareSerial.h> #include <SoftwareSerial.h>
// software serial #1: RX = digital pin 2, TX = digital pin 6 // software serial #1: RX = digital pin 2, TX = digital pin 6
SoftwareSerial portOne(2, 6); SoftwareSerial portOne(2, 6);
// software serial #2: RX = digital pin 8, TX = digital pin 10 // software serial #2: RX = digital pin 8, TX = digital pin 10
SoftwareSerial portTwo(8, 10); SoftwareSerial portTwo(8, 10);
void setup() { // active port is changed by user button pressing
// Open serial communications and wait for port to open: 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); Serial.begin(115200);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only ; // 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 // Start each software serial port
portOne.begin(57600); portOne.begin(57600);
portTwo.begin(57600); portTwo.begin(57600);
portTwo.listen(); // First listen to port one
delay(2000); portOne.listen();
isPortOneActive = true;
Serial.println("Port one is listening");
} }
void loop() { void loop()
// By default, the last initialized port is listening. {
// when you want to listen on a port, explicitly select it: // check data from port one
portOne.listen(); if (portOne.available())
Serial.println("Data from port one:"); {
// while there is data coming in, read it Serial.println("Data from port one:");
// and send to the hardware serial port: // while there is data coming in, read it
while (portOne.available() > 0) { // and send to the hardware serial port
char inByte = portOne.read(); while (portOne.available() > 0)
Serial.write(inByte); 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: // check data from port two
Serial.println(); if (portTwo.available())
delay(2000); {
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());
// // Now listen on the second port // blank line to separate data from the two ports
// portTwo.listen(); Serial.println();
// // 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);
} }