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

RawSerial

The RawSerial class provides UART functionality without the use of Stream's print and scan functions the way the Serial class does. RawSerial does not retarget the standard library print and scan functions. Instead, RawSerial reimplements the print and scan functions to use each target's underlying serial communication functions. See the porting guide for target serial support. This makes RawSerial suitable for use in interrupt handlers with the RTOS.

Serial channels have the following configurable parameters in the constructor:

  • Tx and Rx Pin - The physical serial transmit and receive pins. You can specify a Tx or Tx pin as Not Connected (NC) to get Simplex communication, or specify both to get full duplex.
  • Baud Rate - This setting is an optional constructor parameter. Standard baud rates range 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. This setting may also be configured at run time.

The following parameters can be configured at runtime in the RawSerial object. You can view more information about the configurable settings and functions in the class reference.

  • Baud Rate - Standard baud rates range 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 - Transferred data 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 object automatically sets the parity bit 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 the transmission of data and parity bits, the RawSerial object inserts one or two stop bits to "frame" the data. The default setting for a serial connection on the Mbed microcontroller is one stop bit.

9600-8-N-1, a common notation for serial port settings, describes the default settings for the Mbed microcontroller.

RawSerial class reference

Public Member Functions
 RawSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
int putc (int c)
int getc ()
int puts (const char *str)
int printf (const char *format,...)
 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)
Protected Member Functions
virtual void lock (void)
virtual void unlock (void)
 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)
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)
 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

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

RawSerial 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"
 
RawSerial pc(USBTX, USBRX); // tx, rx
 
int main() {
    pc.printf("Hello World!\n\r");
    while(1) {
        pc.putc(pc.getc()); // echo input back to terminal
    }
}

RawSerial examples

Example one

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

#include "mbed.h"
 
RawSerial device(USBTX, USBRX);  // tx, rx
 
int main() {
    device.baud(19200);
    device.printf("Hello World\n");
}

Mbed OS example

Common use cases for RawSerial are IRQ heavy UART operations, such as the ATParser in the ESP8266 driver. This driver uses UART on user supplied pins to communicate with the offchip ESP8266 module.

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.