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

RawSerial

Note: This API has been deprecated in favor of UnbufferedSerial.

RawSerial class hierarchy

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 RX pin as Not Connected (NC) to get Simplex communication, or specify both to get Full Duplex communication.
  • 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 baud rate for a serial connection on the microcontroller is 9600 baud. This setting may also be configured at run time.

The following parameters can be configured at run time 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 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 microcontroller is 8 bits.
  • Parity - You can add an optional parity bit. The RawSerial 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 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 microcontroller is one stop bit.

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

RawSerial class reference

Public Member Functions
 RawSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a RawSerial port, connected to the specified transmit and receive pins, with the specified baud. More...
int putc (int c)
 Write a char to the serial port. More...
int getc ()
 Read a char from the serial port. More...
int puts (const char *str)
 Write a string to the serial port. More...
void baud (int baudrate)
 Set the baud rate of the serial port. More...
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
 Set the transmission format used by the serial port. More...
int readable ()
 Determine if there is a character available to read. More...
int writeable ()
 Determine if there is space available to write a character. More...
void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a serial interrupt is generated. More...
template<typename T >
void attach (T *obj, void(T::*method)(), IrqType type=RxIrq)
 Attach a member function to call whenever a serial interrupt is generated. More...
template<typename T >
void attach (T *obj, void(*method)(T *), IrqType type=RxIrq)
 Attach a member function to call whenever a serial interrupt is generated. More...
void set_break ()
 Generate a break condition on the serial line NOTE: Clear break needs to run at least one frame after set_break is called. More...
void clear_break ()
 Clear a break condition on the serial line NOTE: Should be run at least one frame after set_break is called. More...
void send_break ()
 Generate a break condition on the serial line. More...
void enable_input (bool enable=true)
 Enable serial input. More...
void enable_output (bool enable=true)
 Enable serial output. More...
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port. More...
void set_flow_control (Flow type, const serial_fc_pinmap_t &static_pinmap)
 Set the flow control type on the serial port. More...
int write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 8bit buffer. More...
int write (const uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 16bit buffer. More...
void abort_write ()
 Abort the on-going write transfer. More...
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)
 Begin asynchronous reading using 8bit buffer. More...
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)
 Begin asynchronous reading using 16bit buffer. More...
void abort_read ()
 Abort the on-going read transfer. More...
int set_dma_usage_tx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking TX transfers. More...
int set_dma_usage_rx (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking RX transfers. More...

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");
}

Example two

Attach a function to call during the generation of serial interrupts. This function defaults to interrupt on an RX pin.

#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);

RawSerial pc(USBTX, USBRX);

void callback_ex() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    pc.putc(pc.getc());
    led2 = !led2;
}

int main() {
    pc.attach(&callback_ex);

    while (1) {
        led1 = !led1;
        wait(0.5);
    }
}

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.