Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SerialBuffered Class Reference
SerialBuffered based on Serial but fully buffered IO. More...
#include <SerialBuffered.h>
Public Member Functions | |
SerialBuffered (PinName tx, PinName rx) | |
Create a SerialBuffered object connected to the specified pins. | |
char | getc (void) |
Get a character from the serial stream. | |
int | getc (bool blocking) |
Gets a character from the serial stream with optional blocking. | |
int | putc (int c) |
Puts a characher to the serial stream. | |
int | putc (int c, bool blocking) |
Puts a characher to the serial stream. | |
int | readable (void) |
Are there characters in the RX buffer we can read? | |
int | writeable (void) |
Is there room in the TX buffer to send a character? | |
void | baud (int baudrate) |
Set's the UART baud rate. | |
void | format (int bits, int parity, int stopbits) |
Sets the serial format. | |
void | set_tx_buffer_size (int buffer_size) |
Change the TX buffer size. | |
void | set_tx_buffer_size (int buffer_size, char *buffer) |
Change the TX buffer size and provide your own allocation. | |
void | set_rx_buffer_size (int buffer_size) |
Change the RX buffer size. | |
void | set_rx_buffer_size (int buffer_size, char *buffer) |
Change the RX buffer size and provide your own allocation. | |
Protected Member Functions | |
uint16_t | calculate_baud (int baud, int uart) |
Calculate the divisors for a UART's baud. |
Detailed Description
SerialBuffered based on Serial but fully buffered IO.
Example:
#include "mbed.h" #include "SerialBuffered.h" SerialBuffered serial1 (p13, p14); SerialBuffered serial2 (p28, p27); int main() { while(1) { if (serial1.readable()) { while (!serial2.writeable()); serial2.putc(serial1.getch()); } if (serial2.readable()) { while (!serial1.writeable()); serial1.putc(serial2.getc()); } } }
Note, because this system "traps" the interrupts for the UART being used do not use the .attach() method, otherwise the buffers will cease functioning. Or worse, behaviour becomes unpredictable.
Definition at line 59 of file SerialBuffered.h.
Constructor & Destructor Documentation
SerialBuffered | ( | PinName | tx, |
PinName | rx | ||
) |
Create a SerialBuffered object connected to the specified pins.
- Parameters:
-
PinName tx The Mbed TX pin for the uart port. PinName rx The Mbed RX pin for the uart port.
Definition at line 38 of file SerialBuffered.cpp.
Member Function Documentation
void baud | ( | int | baudrate ) |
Set's the UART baud rate.
Any allowed baudrate may be passed. However, you should ensure it matches the far end of the serial link.
- Parameters:
-
int The baudrate to set.
Definition at line 30 of file sb_aux.cpp.
uint16_t calculate_baud | ( | int | baud, |
int | uart | ||
) | [protected] |
Calculate the divisors for a UART's baud.
- Parameters:
-
int The desired baud rate int The UART in use to calculate for
Definition at line 38 of file sb_aux.cpp.
void format | ( | int | bits, |
int | parity, | ||
int | stopbits | ||
) |
Sets the serial format.
Valid serial bit lengths are:-
- SerialBuffered::WordLength5
- SerialBuffered::WordLength6
- SerialBuffered::WordLength7
- SerialBuffered::WordLength8
Valid serial parity are:-
- SerialBuffered::NoParity
- SerialBuffered::OddParity
- SerialBuffered::EvenParity
- SerialBuffered::Forced1
- SerialBuffered::Forced0
Valid stop bits are:-
- SerialBuffered::None
- SerialBuffered::One
- SerialBuffered::Two
- Parameters:
-
int bits int parity int stopbits
Definition at line 26 of file sb_aux.cpp.
int getc | ( | bool | blocking ) |
Gets a character from the serial stream with optional blocking.
This method allows for getting a character from the serial stream if one is available. If blocking is true, the method will wait for serial input if the RX buffer is empty. If blocking is false, the method will return immediately if the RX buffer is empty. On return, if not blocking and the buffer was empty, -1 is returned.
- Parameters:
-
blocking true or false, when true will block.
- Returns:
- int An int representation of the 8bit char or -1 on buffer empty.
Definition at line 26 of file sb_getc.cpp.
char getc | ( | void | ) |
Get a character from the serial stream.
- Returns:
- char A char value of the character read.
Definition at line 40 of file sb_getc.cpp.
int putc | ( | int | c, |
bool | blocking | ||
) |
Puts a characher to the serial stream.
As with putc(int c) this function allows for a character to be sent to the uart TX port. However, an extra parameter is added to allow the caller to decide if the method should block or not. If blocking is disabled then this method returns -1 to signal there was no room in the TX FIFO or the TX buffer. The character c passed is has not therefore been sent.
- Parameters:
-
int An int representation of the 8bit character to send. bool true if blocking required, false to disable blocking.
- Returns:
- int Zero on success, -1 if no room in TX FIFO or TX buffer.
Definition at line 26 of file sb_putc.cpp.
int putc | ( | int | c ) |
Puts a characher to the serial stream.
putc
This sends a character out of the uart port or, if no room in the TX FIFO, will place the character into the TX buffer. Note, if the TX buffer is also full, this method will block (wait) until there is room in the buffer.
- Parameters:
-
int An int representation of the 8bit character to send.
- Returns:
- int Always returns zero.
Definition at line 50 of file sb_putc.cpp.
int readable | ( | void | ) |
Are there characters in the RX buffer we can read?
- Returns:
- int 1 if characters are available, 0 otherwise.
Definition at line 44 of file sb_getc.cpp.
void set_rx_buffer_size | ( | int | buffer_size ) |
Change the RX buffer size.
By default, when the SerialBuffer object is created, a default RX buffer of 256 bytes in size is created. If you need a bigger (or smaller) buffer then use this function to change the RX buffer size.
Note, when a buffer is resized, any previous buffer in operation is discarded (destroyed and lost).
- Parameters:
-
int The size of the RX buffer required.
Definition at line 100 of file sb_aux.cpp.
void set_rx_buffer_size | ( | int | buffer_size, |
char * | buffer | ||
) |
Change the RX buffer size and provide your own allocation.
This methos allows for the buffer size to be changed and for the caller to pass a pointer to an area of RAM they have already set aside to hold the buffer. The standard method is to malloc space from the heap. This method allows that to be overriden and use a user supplied buffer.
Note, the buffer you create must be of the size you specify! Note, when a buffer is resized, any previous buffer in operation is discarded (destroyed and lost).
- Parameters:
-
int The size of the RX buffer required. char* A pointer to a buffer area you previously allocated.
Definition at line 79 of file sb_aux.cpp.
void set_tx_buffer_size | ( | int | buffer_size, |
char * | buffer | ||
) |
Change the TX buffer size and provide your own allocation.
This methos allows for the buffer size to be changed and for the caller to pass a pointer to an area of RAM they have already set aside to hold the buffer. The standard method is to malloc space from the heap. This method allows that to be overriden and use a user supplied buffer.
Note, the buffer you create must be of the size you specify! Note, when a buffer is resized, any previous buffer in operation is discarded (destroyed and lost).
- Parameters:
-
int The size of the TX buffer required. char* A pointer to a buffer area you previously allocated.
Definition at line 52 of file sb_aux.cpp.
void set_tx_buffer_size | ( | int | buffer_size ) |
Change the TX buffer size.
By default, when the SerialBuffer object is created, a default TX buffer of 256 bytes in size is created. If you need a bigger (or smaller) buffer then use this function to change the TX buffer size.
Note, when a buffer is resized, any previous buffer in operation is discarded (destroyed and lost).
- Parameters:
-
int The size of the TX buffer required.
Definition at line 73 of file sb_aux.cpp.
int writeable | ( | void | ) |
Is there room in the TX buffer to send a character?
- Returns:
- int 1 if room available, 0 otherwise.
Definition at line 54 of file sb_putc.cpp.
Generated on Wed Jul 13 2022 01:50:20 by
