пример для двух портов работает
This commit is contained in:
1 parent
9efc6c5adf
commit
6a67d9fd61
1 file changed
+54
-39
@@ -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 <SoftwareSerial.h>
|
||||
|
||||
// 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);
|
||||
// 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());
|
||||
|
||||
// // 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);
|
||||
// blank line to separate data from the two ports
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user