Mistake on this page?
Report an issue in GitHub or email us

Serial

The Serial interface provides UART functionality. The serial link has two unidirection channels, one for sending and one for receiving. The link is asynchronous, and so both ends of the serial link must be configured to use the same settings.

One of the serial connections uses the Arm Mbed USB port, allowing you to easily communicate with your host PC.

Serial class reference

Public Member Functions
 Serial (PinName tx, PinName rx, const char *name=NULL, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Serial (PinName tx, PinName rx, int baud)
bool readable ()
bool writable ()
bool writeable ()
 Public Member Functions inherited from mbed::SerialBase
void baud (int baudrate)
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
int readable ()
int writeable ()
void attach (Callback< void()> func, IrqType type=RxIrq)
template<typename T >
void attach (T *obj, void(T::*method)(), IrqType type=RxIrq)
template<typename T >
void attach (T *obj, void(*method)(T *), IrqType type=RxIrq)
void send_break ()
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
int write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
int write (const uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
void abort_write ()
int read (uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
int read (uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
void abort_read ()
int set_dma_usage_tx (DMAUsage usage)
int set_dma_usage_rx (DMAUsage usage)
 Public Member Functions inherited from mbed::Stream
 Stream (const char *name=NULL)
int putc (int c)
int puts (const char *s)
int getc ()
char * gets (char *s, int size)
int printf (const char *format,...)
int scanf (const char *format,...)
int vprintf (const char *format, std::va_list args)
int vscanf (const char *format, std::va_list args)
 operator std::FILE * ()
 Public Member Functions inherited from mbed::FileLike
 FileLike (const char *name=NULL)
 Public Member Functions inherited from mbed::FileHandle
virtual off_t lseek (off_t offset, int whence)
virtual int fsync ()
virtual off_t flen ()
virtual int set_blocking (bool blocking)
virtual short poll (short events) const
bool writable () const
bool readable () const
virtual void sigio (Callback< void()> func)
 Public Member Functions inherited from mbed::FileBase
 FileBase (const char *name, PathType t)
const char * getName (void)
PathType getPathType (void)
Protected Member Functions
virtual int _getc ()
virtual int _putc (int c)
virtual void lock ()
virtual void unlock ()
 Protected Member Functions inherited from mbed::SerialBase
void start_read (void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match)
void start_write (const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event)
void interrupt_handler_asynch (void)
 SerialBase (PinName tx, PinName rx, int baud)
int _base_getc ()
int _base_putc (int c)
 Protected Member Functions inherited from mbed::Stream
virtual int close ()
virtual ssize_t write (const void *buffer, size_t length)
virtual ssize_t read (void *buffer, size_t length)
virtual off_t seek (off_t offset, int whence)
virtual off_t tell ()
virtual void rewind ()
virtual int isatty ()
virtual int sync ()
virtual off_t size ()
Protected Attributes
PlatformMutex _mutex
 Protected Attributes inherited from mbed::SerialBase
CThunk< SerialBase_thunk_irq
DMAUsage _tx_usage
DMAUsage _rx_usage
event_callback_t _tx_callback
event_callback_t _rx_callback
serial_t _serial
Callback< void()> _irq [IrqCnt]
int _baud
 Protected Attributes inherited from mbed::Stream
std::FILE * _file
Additional Inherited Members
 Public Types inherited from mbed::SerialBase
enum  Parity {
  None = 0, Odd, Even, Forced1,
  Forced0
}
enum  IrqType { RxIrq = 0, TxIrq, IrqCnt }
enum  Flow { Disabled = 0, RTS, CTS, RTSCTS }
 Static Public Member Functions inherited from mbed::SerialBase
static void _irq_handler (uint32_t id, SerialIrq irq_type)
 Static Public Member Functions inherited from mbed::FileBase
static FileBaselookup (const char *name, unsigned int len)
static FileBaseget (int n)

Note: On a Windows machine, you will need to install a USB serial driver. See Windows serial configuration.

Serial channels have a number of configurable parameters:

  • Baud Rate - There are a number of standard baud rates ranging from a few hundred bits per second, to megabits per second. The default setting for a serial connection on the Mbed microcontroller is 9600 baud.
  • Data length - Data transferred can be either 7 or 8 bits long. The default setting for a serial connection on the Mbed microcontroller is 8 bits.
  • Parity - You can add an optional parity bit. The parity bit will be automatically set to make the number of 1s in the data either odd or even. Parity settings are Odd, Even or None. The default setting for a serial connection on the Arm Mbed microcontroller is None.
  • Stop Bits - After data and parity bits have been transmitted, one or two stop bits are inserted to "frame" the data. The default setting for a serial connection on the Mbed microcontroller is one stop bit.

The default settings for the Mbed microcontroller are described as 9600-8-N-1, a common notation for serial port settings.

Serial hello, world

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
 
Serial pc(USBTX, USBRX); // tx, rx
 
int main() {
    pc.printf("Hello World!\n\r");
    while(1) {
        pc.putc(pc.getc() + 1); // echo input back to terminal
    }
}

Serial examples

Example one

Write a message to a device at a baud rate of 19200.

#include "mbed.h"

Serial device(USBTX, USBRX);  // tx, rx

int main() {
    device.baud(19200);
    device.printf("Hello World\n");
}

Example two

Provide a serial pass-through between the PC and an external UART.

#include "mbed.h"
//K64F
Serial pc(USBTX, USBRX); // tx, rx
Serial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX);  // tx, rx

int main() {
    while(1) {
        if(pc.readable()) {
            device.putc(pc.getc());
        }
        if(device.readable()) {
            pc.putc(device.getc());
        }
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.