пример для двух портов работает
This commit is contained in:
1 parent
9efc6c5adf
commit
6a67d9fd61
1 file changed
+53
-38
@@ -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:");
|
Serial.println("Data from port one:");
|
||||||
// while there is data coming in, read it
|
// while there is data coming in, read it
|
||||||
// and send to the hardware serial port:
|
// and send to the hardware serial port
|
||||||
while (portOne.available() > 0) {
|
while (portOne.available() > 0)
|
||||||
char inByte = portOne.read();
|
Serial.write((char)portOne.read());
|
||||||
Serial.write(inByte);
|
|
||||||
}
|
|
||||||
|
|
||||||
// blank line to separate data from the two ports:
|
// blank line to separate data from the two ports
|
||||||
Serial.println();
|
Serial.println();
|
||||||
delay(2000);
|
}
|
||||||
|
|
||||||
// // Now listen on the second port
|
// check data from port two
|
||||||
// portTwo.listen();
|
if (portTwo.available())
|
||||||
// // while there is data coming in, read it
|
{
|
||||||
// // and send to the hardware serial port:
|
Serial.println("Data from port two:");
|
||||||
// Serial.println("Data from port two:");
|
// while there is data coming in, read it
|
||||||
// while (portTwo.available() > 0) {
|
// and send to the hardware serial port
|
||||||
// char inByte = portTwo.read();
|
while (portTwo.available() > 0)
|
||||||
// Serial.write(inByte);
|
Serial.write((char)portTwo.read());
|
||||||
// }
|
|
||||||
|
// blank line to separate data from the two ports
|
||||||
// // blank line to separate data from the two ports:
|
Serial.println();
|
||||||
// Serial.println();
|
}
|
||||||
// delay(2000);
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user