| 1 |
/* |
| 2 |
Software serial multiple serial test |
| 3 |
|
| 4 |
Receives from the two software serial ports, |
| 5 |
sends to the hardware serial port. |
| 6 |
|
| 7 |
In order to listen on a software port, you call port.listen(). |
| 8 |
When using two software serial ports, you have to switch ports |
| 9 |
by listen()ing on each one in turn. Pick a logical time to switch |
| 10 |
ports, like the end of an expected transmission, or when the |
| 11 |
buffer is empty. This example switches ports when there is nothing |
| 12 |
more to read from a port |
| 13 |
|
| 14 |
The circuit: |
| 15 |
Two devices which communicate serially are needed. |
| 16 |
* First serial device's TX attached to digital pin 10(RX), RX to pin 11(TX) |
| 17 |
* Second serial device's TX attached to digital pin 8(RX), RX to pin 9(TX) |
| 18 |
|
| 19 |
Note: |
| 20 |
Not all pins on the Mega and Mega 2560 support change interrupts, |
| 21 |
so only the following can be used for RX: |
| 22 |
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69 |
| 23 |
|
| 24 |
Not all pins on the Leonardo support change interrupts, |
| 25 |
so only the following can be used for RX: |
| 26 |
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI). |
| 27 |
|
| 28 |
created 18 Apr. 2011 |
| 29 |
modified 19 March 2016 |
| 30 |
by Tom Igoe |
| 31 |
based on Mikal Hart's twoPortRXExample |
| 32 |
|
| 33 |
This example code is in the public domain. |
| 34 |
|
| 35 |
*/ |
| 36 |
|
| 37 |
#include <SoftwareSerial.h> |
| 38 |
// software serial #1: RX = digital pin 10, TX = digital pin 11 |
| 39 |
SoftwareSerial portOne(10, 11); |
| 40 |
|
| 41 |
// software serial #2: RX = digital pin 8, TX = digital pin 9 |
| 42 |
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega |
| 43 |
SoftwareSerial portTwo(8, 9); |
| 44 |
|
| 45 |
void setup() { |
| 46 |
// Open serial communications and wait for port to open: |
| 47 |
Serial.begin(9600); |
| 48 |
while (!Serial) { |
| 49 |
; // wait for serial port to connect. Needed for native USB port only |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
// Start each software serial port |
| 54 |
portOne.begin(9600); |
| 55 |
portTwo.begin(9600); |
| 56 |
} |
| 57 |
|
| 58 |
void loop() { |
| 59 |
// By default, the last initialized port is listening. |
| 60 |
// when you want to listen on a port, explicitly select it: |
| 61 |
portOne.listen(); |
| 62 |
Serial.println("Data from port one:"); |
| 63 |
// while there is data coming in, read it |
| 64 |
// and send to the hardware serial port: |
| 65 |
while (portOne.available() > 0) { |
| 66 |
char inByte = portOne.read(); |
| 67 |
Serial.write(inByte); |
| 68 |
} |
| 69 |
|
| 70 |
// blank line to separate data from the two ports: |
| 71 |
Serial.println(); |
| 72 |
|
| 73 |
// Now listen on the second port |
| 74 |
portTwo.listen(); |
| 75 |
// while there is data coming in, read it |
| 76 |
// and send to the hardware serial port: |
| 77 |
Serial.println("Data from port two:"); |
| 78 |
while (portTwo.available() > 0) { |
| 79 |
char inByte = portTwo.read(); |
| 80 |
Serial.write(inByte); |
| 81 |
} |
| 82 |
|
| 83 |
// blank line to separate data from the two ports: |
| 84 |
Serial.println(); |
| 85 |
} |