Two Port Receive

Work with multiple software serial ports on an Arduino board.

Arduino boards have built in support for serial communication on pins 0 and 1, but what if you need more serial ports? The SoftwareSerial Library has been developed to allow serial communication to take place on the other digital pins of your boards, using software to replicate the functionality of the hardwired RX and TX lines. This can be extremely helpful when the need arises to communicate with two serial enabled devices, or to talk with just one device while leaving the main serial port open for debugging purpose.

In the example below, digital pins 8 and 10 on your Arduino board are used as virtual RX serial lines. Pins 9 and 11 are virtual TX lines. The board listens on one virtual port (portOne) until it has read all available data. After that, it does the same on the second virtual port (portTwo).

Hardware Required

  • Arduino Board

Circuit

There is no circuit for this example. Make sure that your Arduino board is attached to your computer via USB to enable serial communication through the serial monitor window of the Arduino Software (IDE).

Code

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

Last revision 2018/05/17 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.