SoftwareSerial Library
| |||
SoftwareSerial | available | begin | islistening |
overflow | peek | read | |
println | listen | write |
The SoftwareSerial library allows serial communication on other digital pins of an Arduino board, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.
The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by 'Mikal Hart'.
To use this library:
#include <SoftwareSerial.h>
SoftwareSerial library has the following known limitations:
If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library .
SoftwareSerial()
Create an instance of a SoftwareSerial object. Multiple SoftwareSerial objects may be created, however only one can be active at a given moment.
SoftwareSerial( rxPin, txPin, inverse_logic)
None.
#include <SoftwareSerial.h> const byte rxPin = 2; const byte txPin = 3; SoftwareSerial mySerial( rxPin, txPin ); // Set up new SoftwareSerial object
available()
Get the number of bytes (characters) available for reading from a software serial port. This is data that has already arrived and stored in the serial receive buffer.
mySerial.available()
None.
The number of bytes available to read.
#include <SoftwareSerial.h> #define rxPin 10 #define txPin 11 /*F******************************************************************** * Set up a new SoftwareSerial object7SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); **********************************************************************/ void setup() { pinMode( rxPin, INPUT ); // Define pin modes for TX and RX11 pinMode( txPin, OUTPUT ); mySerial.begin( 9600 ); } /*F******************************************************************** * Set baud rate for the SoftwareSerial object15 **********************************************************************/ void loop() { if( mySerial.available() > 0) mySerial.read(); }
begin()
Sets the speed (baud rate) for the serial communication. Supported baud rates are: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 bauds.
mySerial.begin( speed )
None.
#include <SoftwareSerial.h>23 #define rxPin 10 #define txPin 11 // Set up a new SoftwareSerial object7SoftwareSerial mySerial = SoftwareSerial( rxPin, txPin); /*F******************************************************************** * **********************************************************************/ void setup() { pinMode( rxPin, INPUT ); // Define pin modes for TX and RX11 pinMode( txPin, OUTPUT ); mySerial.begin( 9600 ); // Set the baud rate for the SoftwareSerial object } /*F******************************************************************** * **********************************************************************/ void loop() { // ... }
isListening()
Tests to see if requested software serial object is actively listening.
mySerial.isListening()
None.
Boolean.
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 1 SoftwareSerial portOne( 10, 11); /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( 9600 ); // Set baud rate for Serial port portOne.begin( 9600 ); // Set baud rate for SerialSoftware object } /*F******************************************************************** * **********************************************************************/ void loop() { if( portOne.isListening()) { Serial.println("portOne is listening!"); }
overflow()
Tests to see if a SoftwareSerial buffer overflow has occurred. Calling this function clears the overflow flag, meaning that subsequent calls will return false unless another byte of data has been received and discarded in the meantime. The SoftwareSerial buffer can hold up to 64 bytes.
mySerial.overflow()
None.
Boolean.
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11 SoftwareSerial portOne( 10, 11); /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( 9600 ); // SET BAUD RATE FOR sERIAL PORT portOne.begin( 9600 ); // SET BAUD RATE FOR SerialSoftware OBJECT } /*F******************************************************************** * **********************************************************************/ void loop() { if( portOne.overflow()) { Serial.println("portOne overflow!"); } // ... }
peek()
Return a character that was received on the RX pin of the software serial port. Unlike read(), however, subsequent calls to this function will return the same character. Note that only one SoftwareSerial object can receive incoming data at a time (select which one with the listen() function).
mySerial.peek()
None.
The character read or -1 if none is available.
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11 SoftwareSerial mySerial( 10, 11 ); /*F******************************************************************** * **********************************************************************/ void setup() { mySerial.begin( 9600 ); // Set baud rate for SerialSoftware object } /*F******************************************************************** * **********************************************************************/ void loop() { char c = mySerial.peek(); }
read()
Return a character that was received on the RX pin of the SoftwareSerial objecto. Note that only one SoftwareSerial object can receive incoming data at a time (select which one with the listen() function).
mySerial.read()
None.
The character read or -1 if none is available.
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 // and TX in digital pin 11 SoftwareSerial mySerial( 10, 11 ); /*F******************************************************************** * **********************************************************************/ void setup() { mySerial.begin( 9600 ); // Set baud rate for SerialSoftware object } /*F******************************************************************** * **********************************************************************/ void loop() { char c = mySerial.read(); }
print()
Prints data to the transmit pin of the SoftwareSerial object. Works the same as the Serial.print() function.
mySerial.print( val )
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin SoftwareSerial mySerial( 10, 11 ); analogValue; /*F******************************************************************** * **********************************************************************/ void setup() { mySerial.begin( 9600 ); // Set baud rate for SerialSoftware object } /*F******************************************************************** * **********************************************************************/ void loop() { analogValue = analogRead( A0 ); // Read analog value on pin A0 // Print analogValue in the Serial Monitor in many formats: mySerial.print( analogValue ); // Print as an ASCII-encoded decimal mySerial.print( "\t"); // Print a tab character mySerial.print( analogValue, DEC); // Print as an ASCII-encoded decimal mySerial.print("\t"); // Print a tab character mySerial.print(analogValue, HEX); // Print an ASCII-encoded hexadecimal mySerial.print("\t"); // Print a tab character mySerial.print( analogValue, OCT); // Print as an ASCII-encoded octal mySerial.print("\t"); // Print a tab character mySerial.print( analogValue, BIN); // Print as an ASCII-encoded binary mySerial.print("\t"); // Print a tab character mySerial.print( analogValue/4, BYTE); // Print as a raw byte value (divide // value in 4 because analogRead() function returns numbers // from 0 to 1023, but a byte can only hold values up to 255) mySerial.print("\t"); // Print a tab character mySerial.println(); // Print a line feed character // Pause for 10 milliseconds before the next reading delay(10); }
println()
Prints data to the transmit pin of the SoftwareSerial object followed by a carriage return and line feed. Works the same as the Serial.println() function.
mySerial.println( val )
The number of bytes written (reading this number is optional).
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11 SoftwareSerial mySerial( 10, 11); int analogValue; /*F******************************************************************** * **********************************************************************/ void setup() { // SET BAUD RATE FOR SerialSoftware OBJECT mySerial.begin( 9600 ); } /*F******************************************************************** * **********************************************************************/ void loop() { // Read the analog value on pin A0 analogValue = analogRead(A0); // Print analogValue in the Serial Monitor in many formats: mySerial.print( analogValue ); // Print as an ASCII-encoded decimal mySerial.print( "\t" ); // Print a tab character mySerial.print( analogValue, DEC); // Print as an ASCII-encoded decimal mySerial.print("\t"); // Print a tab character mySerial.print( analogValue, HEX); // Print as an ASCII-encoded hexadecimal mySerial.print("\t"); // Print a tab character mySerial.print( analogValue, OCT); // Print as an ASCII-encoded octal mySerial.print("\t"); // Print a tab character mySerial.print( analogValue, BIN); // Print as an ASCII-encoded binary mySerial.print("\t"); // Print a tab character mySerial.print( analogValue/4, BYTE); // Print as a raw byte value (divide the // value in 4 because analogRead() function returns numbers // from 0 to 1023, but a byte can only hold values up to 255) mySerial.print("\t"); // Print a tab character mySerial.println(); // Print a line feed character // Pause for 10 milliseconds before the next reading36 delay(10); }
listen()
Enables the selected SoftwareSerial object to listen. Only one SoftwareSerial object can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() function (unless the given instance is already listening).
mySerial.listen()
None.
Returns true if it replaces another.
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin SoftwareSerial portOne( 10, 11);56// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 9 SoftwareSerial portTwo( 8, 9); /*F******************************************************************** * **********************************************************************/ void setup() { Serial.begin( 9600 ); // SET BAUD RATE FOR THE SERIAL OBJECTS portOne.begin( 9600 ); // SET BAUD RATE FOR THE SERIALSOFTWARE OBJECTS portTwo.begin( 9600 ); } /*F******************************************************************** * **********************************************************************/ void loop() { portOne.listen(); // Enable SoftwareSerial object to listen if( portOne.isListening()) { Serial.println("portOne is listening!"); } else { Serial.println("portOne is not listening!"); } if( portTwo.isListening()) { Serial.println("portTwo is listening!"); } else { Serial.println("portTwo is not listening!"); } }
write()
Prints data to the transmit pin of the SoftwareSerial object as raw bytes. Works the same as the Serial.write()function.
mySerial.write( val )
The number of bytes written (reading this number is optional).
#include <SoftwareSerial.h> // Set up a new SoftwareSerial object with RX in digital pin 10 and TX // in digital pin 11 SoftwareSerial mySerial( 10, 11); /*F******************************************************************** * **********************************************************************/ void setup() { mySerial.begin( 9600 ); // Set baud rate for SerialSoftware object8 } /*F******************************************************************** * **********************************************************************/ void loop() { mySerial.write( 45 ); // Send a byte with the value 4513 // Send the string “hello” and return the length of the string int bytesSent = mySerial.write( “hello” ); }