|
| 1 | +# BufferedSerial |
| 2 | + |
| 3 | +<span class="images"><span>`BufferedSerial` class hierarchy</span></span> |
| 4 | + |
| 5 | +The `BufferedSerial` class provides UART functionality. We recommend you use this class for serial data transfers. You can use it to send and receive bytes of data in a sequence using separate transmit (TX) and receive pins (RX). A device can interface with another device (such as sensors, printers or another board) to exchange data or to send text to be displayed on a text-based computer interface. |
| 6 | + |
| 7 | +Serial channels have the following characteristics: |
| 8 | + |
| 9 | +- TX and RX pins - you can specify either pin as Not Connected (NC) for simplex (unidirectional) communication or both as valid pins for full duplex (bidirectional) communication. |
| 10 | +- Baud rate - predefined speed at which data is sent and received on the UART interface. Standard baud rates include 9600, 119200 and 115200. |
| 11 | + |
| 12 | +Data is transmitted using packets of configurable sizes divided in different sections, which include: |
| 13 | + |
| 14 | +- Start bit: indicates the start of UART data transmission. |
| 15 | +- Data frame: can be 5 to 8 (or 9 if a parity bit is not used) bits for the actual data being transferred. |
| 16 | +- Parity bit: optional bit, used for data error detection. |
| 17 | +- Stop bits: 1-2 bits to signal the end of a data packet. |
| 18 | + |
| 19 | +The `BufferedSerial` calls the underlying HAL API functions. Please see the [porting guide](../porting/serial-port.html) for target serial support. |
| 20 | + |
| 21 | +When the receive interrupt is triggered when receiving data from a device, the `BufferedSerial` class stores the byte(s) available to read from the hardware buffer to an internal intermediary buffer. When a read request is made, the `BufferedSerial` class uses a mutex lock and enters a critical section to read out the number of bytes requested if as many are available in the intermediary buffer. |
| 22 | + |
| 23 | +To transmit multiple bytes, the class uses an intermediary buffer to store the bytes to send and monitors the serial interface to transfer them to the hardware buffer as soon as it is available. However, all bytes are written unbuffered if in a critical section. |
| 24 | +Using intermediary buffers allows the UART interface to be used reliably for input from noninterrupt context while avoiding excess spinning waiting for transmission buffer space. |
| 25 | + |
| 26 | +The RX and TX buffers are circular buffers with preallocated sizes configurable using the configuration parameters `uart-serial-rxbuf-size` and `uart-serial-txbuf-size`. You can find both configuration parameters in `drivers/mbed_lib.json`. |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +The following parameters can be configured at object instantiation: |
| 31 | + |
| 32 | + - _TX_. |
| 33 | + - _RX_. |
| 34 | + - _Baud rate_. |
| 35 | + |
| 36 | +The default baud rate value is configured in `mbed-os/platform/mbed_lib.json`. |
| 37 | + |
| 38 | +The following parameters can be configured after an `BufferedSerial` object instantiation. |
| 39 | + |
| 40 | + - _Baud rate_. |
| 41 | + - _Data frame length_. |
| 42 | + - _Parity bit_. |
| 43 | + - _Stop bits_. |
| 44 | + |
| 45 | +The default settings for a microcontroller are set as _9600-8-N-1_, a common notation for serial port settings. This denotes a baud rate of 9,600 (9600), eight data frame length (8), no parity bit (N) and one stop bit (1). |
| 46 | + |
| 47 | +You can also configure hardware flow control if necessary. |
| 48 | + |
| 49 | +You can view more information about the configurable settings and functions in the class reference. |
| 50 | + |
| 51 | +## BufferedSerial class reference |
| 52 | + |
| 53 | +[](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/classmbed_1_1_buffered_serial.html) |
| 54 | + |
| 55 | +<span class="notes">**Note**: On a Windows machine, you need to install a USB serial driver. Please see the [Windows serial configuration](../tutorials/serial-communication.html#windows-serial-driver) document for more information.</span> |
| 56 | + |
| 57 | +## BufferedSerial examples |
| 58 | + |
| 59 | +This example toggles an LED and echoes input to a terminal: |
| 60 | + |
| 61 | +[](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_Drivers/BufferedSerial_echo/main.cpp) |
| 62 | + |
| 63 | +### Printing to the console |
| 64 | + |
| 65 | +Mbed OS redefines target-dependent I/O functions in the C library to allow you to use the C standard I/O library functions (`s\v\f\n\printf`, `scanf` and so on) in your application for printing to the console. |
| 66 | + |
| 67 | +You can configure the system I/O retarget code to be buffered or unbuffered, depending on the configuration of the parameter `stdio-buffered-serial` in `platform/mbed_lib.json`. When it is buffered, the retarget code uses an instance of a `BufferedSerial` class to perform the actual printing. This is where `BufferedSerial`'s `Filehandle` inheritance is used. |
| 68 | + |
| 69 | +Alternatively, if you need more configuration of the serial interface, you can pass an instance of the `BufferedSerial` class to the system I/O retarget code at run time for printing to the console: |
| 70 | + |
| 71 | +[](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_Drivers/BufferedSerial_printf/main.cpp) |
| 72 | + |
| 73 | +We recommend you use the standard C I/O library directly to print to the console in Mbed OS. |
0 commit comments