Software Serial Library

1. Purpose

SoftSerial library emulates Serial Interface on any I/O pin of Ansteron Board. All commonly used baurates, up to 115.2 Kbps are supported. The library is purely software implemented and no additional hardware resource is used.

The operation of this library is basically sending data on an I/O pin with Serial-like signals that other devices can understand. The library also supports polling an I/O pin to receive data at all supported baurates.

2. Functions


Set the baurate that other functions of the library will operate on. The default baurate is 115200 bps and can be changed using this function. If multiple devices with different baurates are used, call this function to set the appropriate baurate for each device before sending or receiving data. Input value for baurate_id must be one of the identifiers in table below; other values may cause undefined baurate and data errors.

IdentifierBaurateActual value
DEFAULT115200 bps, 1 bit stop, no parity0
SOFTSERIAL_DEFAULT115200 bps, 1 bit stop, no parity6
SOFTSERIAL_BAU_48004800 bps, 1 bit stop, no parity234
SOFTSERIAL_BAU_96009600 bps, 1 bit stop, no parity115
SOFTSERIAL_BAU_1440014400 bps, 1 bit stop, no parity76
SOFTSERIAL_BAU_1920019200 bps, 1 bit stop, no parity55
SOFTSERIAL_BAU_3840038400 bps, 1 bit stop, no parity26
SOFTSERIAL_BAU_5600056000 bps, 1 bit stop, no parity16
SOFTSERIAL_BAU_5760057600 bps, 1 bit stop, no parity16
SOFTSERIAL_BAU_115200115200 bps, 1 bit stop, no parity6


Send one byte using given I/O pin. Since the IDLE state of Serial transmit line is high, the I/O pin should be make output and set high at start-up. This function will also make the pin high after data has been sent. Input value for pin is the same with inputs for I/O functions (PIN_Cx, PIN_Dx, PIN_Bx). The function always succeeds and no value turned.


Receive a byte on given I/O pin. The function will monitor given I/O pin and receive one byte when it comes. Note that this function will not return until a byte was received. If a byte came before this function was called, the byte will be lost. If the function is call in the middle of a byte coming, it will return invalid data.

When data is expected to come, call this function as soon as possible. If multiple bytes are coming in a message, set up a buffer large enough and only process data after the whole message has been received.

softserial_get_byte_ex(pin, timeout);

This function is the same with softserial_get_byte() except it will return when timeout. Timeout value is in millisecond. The function returns an int value (16 bit) instead of one byte. The high byte of returned value indicate status (0 is timeout, non-zero is success), the low byte is data if any. For example:

The function will return if there was no data came for 1 second. Simply check returned value, if it is zero, there is no data. Otherwise, some data must be in the lower byte and it can be extracted byte casting to a char variable (directly assigned to a char variable will work as well). The value can also be used directly by making the high byte zero, like this rvalue=rvalue & 0x00FF;

softserial_message(pin, message);

Send a message on given I/O pin. The message can be 1 or more characters (bytes), ending with value 0. This is similar to serial_message();.

softserial_print_int(pin, value);

softserial_print_long(pin, value);

softserial_print_hex8(pin, value);

softserial_print_hex16(pin, value);

softserial_print_hex32(pin, value);

Print an integer/hex value. Translate a value into a string of characters and send them out on given I/O pin.

3. Use note

The library only supports Serial signal with 8 bit data, 1 bit stop and no parity bit (8N1).

Due to timing requirements, all interrupts will be temporally disabled while SoftSerial library sending or receiving data and re-enabled if they were on. When the library is used with other libraries that run background tasks, send and receive data functions should only be used when other tasks can be temporally off.

In the case where a message with multiple bytes are being received and background task(s) is running at the same time, the program should temporally disable all interrupts (system_disable_interrupt();) to avoid interrupts between bytes. When the message has finished receiving, interrupts can be re-enabled by calling function system_enable_interrupt();.