Board to PC communication over USB

The mbed microcontroller on your board can communicate with a host PC over the same USB cable that is used for programming.

If you’re working on Windows, you might need to install a serial driver.

This allows you to:

  • Print out messages to a host PC terminal (useful for debugging).

  • Read input from the host PC keyboard.

  • Communicate with applications and programming languages running on the host PC that can communicate with a serial port, like Perl, Python, and Java.

Hello World!

This program prints a “Hello World” message that you can view on a terminal application. Communication over the USB Serial port uses the standard serial interface, specifying the internal (USBTX, USBRX) pins to connect to the serial port routed over USB:

#include "mbed.h"

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

int main() {
    pc.printf("Hello World!\n");
    while(1);
}

Terminal applications

Terminal applications run on your host PC, and provide a window for your mbed board to print to, and a means for you to type characters back to your board.

Serial configuration: The standard setup for the USB Serial Port is 9600 baud, 8 bits, 1 stop bit, no parity (9600-8-N-1)

Using terminal applications on Windows and OS X

Installing an application

There are many terminal applications for Windows, including:

  • CoolTerm - this is the application we use in this example. We use it very often, becuase it usually “just works”.
  • Tera Term.
  • Putty.
  • Some Windows PCs come with Hyperterminal installed.

Configuring the Connection

  1. Plug in your mbed board.
  2. Open CoolTerm.
  3. Click Connect. This opens up an 8-n-1 9600baud connection to the first available serial port. If you have more than one board plugged in, you may need to change the port under Options > Serial Port > Port .

Check your connection parameters:

  1. Select Setup > Serial Port.
  2. You should see 9600 baud, 8 bits, 1 stop bit, no parity (9600-8-N-1).
  3. If you do not see your board, click Re-Scan Peripherals,

Your terminal program is now configured and connected.

Using terminal applications on Linux

CoolTerm should work under Linux. If for some reason it doesn’t, you can try using any of the following:

Additional examples

Use your terminal application to interact with the following examples.

If you’re not sure how to build these examples and run them on your board, please see out build tools section.

Echo back characters you type

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    pc.printf("Echoes back to the screen anything you type\n");
    while(1) {
        pc.putc(pc.getc());
    }
}

Use the ‘u’ and ‘d’ keys to make LED1 brighter or dimmer

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
PwmOut led(LED1);

float brightness = 0.0;

int main() {
    pc.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down\n");

    while(1) {
        char c = pc.getc();
        if((c == 'u') && (brightness < 0.5)) {
            brightness += 0.01;
            led = brightness;
        }
        if((c == 'd') && (brightness > 0.0)) {
            brightness -= 0.01;
            led = brightness;
        }   
    }
}

Pass characters in both directions

#include "mbed.h"

Serial pc(USBTX, USBRX);
Serial uart(p28, p27);

DigitalOut pc_activity(LED1);
DigitalOut uart_activity(LED2);

int main() {
    while(1) {
        if(pc.readable()) {
            uart.putc(pc.getc());
            pc_activity = !pc_activity;
        }
        if(uart.readable()) {
            pc.putc(uart.getc());
            uart_activity = !uart_activity;
        }
    }
}

Using stdin, stdout and stderr

The C stdin, stdout and stderr file handles are also defaulted to the PC serial connection:

#include "mbed.h"

int main() {
    printf("Hello World!\n");
    while(1);
}

Read to a buffer

#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

int main() {
    char c;
    char buffer[128];

    pc.gets(buffer, 4);
    pc.printf("I got '%s'\n", buffer);
    while(1);
}