You are viewing an older revision! See the latest version

Serial

/media/uploads/mbedofficial/serial_interfaces.png

Serial is a generic protocol used by computers and electronic modules to send and receive control information and data. 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 goes via the mbed USB port, allowing you to easily communicate with your host PC.

Hello World!

Print to the PC, then pass back characters (slightly modified!)

#include "mbed.h"

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

int main() {
    pc.printf("Hello World!");
    while(1) {
        pc.putc(pc.getc() + 1);
    }
}

API

API summary

[error loading api]

Details

The Serial Interface can be used on mbed pins p9/p10, p13/p14, p28/p27 and USBTX/USBRX

Note that USBTX/USBRX are not DIP pins; they represent the pins that route to the interface USB Serial port so you can communicate with a host PC.

Note

If you want to send data to a host PC, take a look at:

  • SerialPC - Communicating between mbed and a host PC

Note that on a windows machine, you will need to install a USB Serial driver, see:

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 seconds, 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 - An optional parity bit can be added. The parity bit will be automatically set to make the number of 1's in the data either odd or even. Parity settings are Odd, Even or None. The default setting for a Serial connection on the mbed microcontroller is for the parity to be set to None.
  • Stop Bits - After data and parity bits have been transmitted, 1 or 2 stop bit is inserted to "frame" the data. The default setting for a Serial connection on the mbed microcontroller is for one stop bit to be added.

The default settings for the mbed microcontroller are described as 9600 8N1, and this is common notation for Serial port settings.

See Also

Reference

Examples

Write a message to a device at a 19200 baud

#include "mbed.h"

Serial device(p9, p10);  // tx, rx

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

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

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial device(p9, p10);  // tx, rx

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

Attach to RX Interrupt

#include "mbed.h"

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

Serial pc(USBTX, USBRX);

void callback() {
    led2 = !led2;
}

int main() {
    pc.attach(&callback);
    
    while (1) {
        led1 = !led1;
        wait(0.5);
    }
}

All wikipages