Loading...

Learning   Examples | Foundations | Hacking | Links

Examples > Software Serial Library

Two Port Receive

Arduinos 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 Arduino, 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 2 and 4 on your Arduino are used as virtual RX serial lines. Pins 3 and 5 are virtual TX lines. The Arduino listens on one virtual port (portOne) until it receives a "?" character. After that, it listens on the second virtual port (portTwo).

Hardware Required

  • Arduino Board

Circuit

There is no circuit for this example. Make sure that your Arduino is attached to your computer via USB to enable serial communication.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

/*
  Software serial multple serial test
 
 Receives from the two software serial ports,
 sends to the hardware serial port.
 
 In order to listen on a software port, you call port.listen().
 When using two software serial ports, you have to switch ports
 by listen()ing on each one in turn. Pick a logical time to switch
 ports, like the end of an expected transmission, or when the
 buffer is empty. This example switches ports when there is nothing
 more to read from a port
 
 The circuit:
 Two devices which communicate serially are needed.
 * First serial device's TX attached to digital pin 2, RX to pin 3
 * Second serial device's TX attached to digital pin 4, RX to pin 5
 
 created 18 Apr. 2011
 by Tom Igoe
 based on Mikal Hart's twoPortRXExample
 
 This example code is in the public domain.
 
 */


#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 2, RX = digital pin 3
SoftwareSerial portOne(2, 3);

// software serial #2: TX = digital pin 4, RX = digital pin 5
SoftwareSerial portTwo(4, 5);

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);

  // Start each software serial port
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop()
{
  // By default, the last intialized 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);
  }

  // blank line to separate data from the two ports:
  Serial.println();

  // 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();
}

See also