Serial Communications (SoftwareSerial Class)

Serial Communications (SoftwareSerial Class)

SoftwareSerial is a library that enables serial communication with a digital pin other than the serial port. It is possible to have multiple software serial ports with speeds up to 115200bps. However, be careful when the baud rate is 115200bps because the reception latch timing is not so accurate. The receive buffer is fixed at 256 bytes.

To use it, specify #include <SoftwareSerial.h>. You will need to create an instance of SoftwareSerial class.

.

Constructor for SoftwareSerial

Description
This is a constructor for creating an instance of the SoftwareSerial class.
Syntax
SoftwareSerial(receivePin, transmitPin)
SoftwareSerial(receivePin, transmitPin, inverse_logic)
Parameters
receivePin: Receive pin
transmitPin: Transmit pin
inverse_logic: Inverse logic (default false)
Returns
None

begin

Description
Sets the data rate in bits per second (baud).
Syntax
serial.begin(int speed)
Parameters
speed: In bits per second (baud)
Returns
None

end

Description
Disables serial communication port.
Syntax
serial.end()
Parameters
None
Returns
None

available

Description
Gets the number of bytes (characters) available for reading from the serial port.
Syntax
int serial.available()
Parameters
None
Returns
The number of bytes available in the serial buffer. Returns 0 when there is no data.

read

Description
Reads 1 byte of data from the receive buffer in the the serial communications port.
Syntax
int serial.read()
Parameters
None
Returns
Data. If no data available, returns -1.

peek

Description
Returns the next byte (character) of incoming serial data in the serial communications port without removing it from the receive buffer. CRLF conversion is not carried out.
Syntax
int serial.peek()
Parameters
None
Returns
The first byte of incoming serial data available (or -1 if no data is available).

flush

Description
Waits until the send buffer of the serial communications port is empty.
Syntax
serial.flush()
Parameters
None
Returns
None

write

Description
Writes a character string or data to the serial communications port.
Syntax
serial.write(const char* str)
serial.write(const unsigned char* buf, int len)
Parameters
str: A string to send as a series of bytes
buf: An array (pointer) to send data
len: Length of the output (written) data
Returns
The number of bytes of output (written) data.

print

Description
Prints a character string to the serial communications port.
Syntax
serial.print(val)
serial.print(val, format)
Parameters
val: The value or character string to print
format: Specifies the number base for values (BIN: binary or base 2, OCT: octal or base 8, DEC: decimal or base 10, HEX: hexadecimal or base 16)
Returns
The number of bytes printed.

println

Description
Prints a character string to the serial communications port followed by a carriage return.
Syntax
serial.println(val)
serial.println(val, format)
Parameters
val: The value or character string to print
format: Specifies the number base for values (BIN: binary or base 2, OCT: octal or base 8, DEC: decimal or base 10, HEX: hexadecimal or base 16)
Returns
The number of bytes printed.

listen

Description
Enables the selected software serial port to listen. Only one software serial port 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() (unless the given instance is already listening).
Syntax
bool serial.listen()
Parameters
None
Returns
True or False

isListening

Description
Tests to see if requested software serial port is actively listening.
Syntax
bool serial.isListening()
Parameters
None
Returns
True or False

overflow

Description
Tests to see if a software serial 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.
Syntax
bool serial.overflow()
Parameters
None
Returns
True or False

Example

This is an example that specifies Pin 10 as receive and Pin 11 as transmit and outputs the value of A0.


#include <Arduino.h>
#include <SoftwareSerial.h>
 
SoftwareSerial serial(10, 11); // RX: 10, TX:11
int analogValue;
 
void setup()
{
  serial.begin(9600);
}
 
void loop()
{
  analogValue = analogRead(A0);
  delay(100);
}