USBSerial

mbed OS 2

This is the handbook for mbed OS 2.

The USBSerial interface is used to emulate a serial port over USB. You can use this serial port as an extra serial port or as a debug solution. It's also a great solution to easily communicate between your mbed and a computer.

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Driver required on Windows!

On Windows, you need a configuration file. You can download this archive containing a .inf file. Extract it.
When you plug your USBSerial serial device, Windows will try to find an existing driver for it without success. After this step, go into the device manager to find the unknown device:

  • Right click on the device
  • Update driver software
  • Click on "Browse my computer for driver software"
  • Indicate the path of serial.inf extracted previously and click next.
  • Accept the warning and you should have a virtual port (called Mbed Virtual Serial Port in device manager) over USB!

As product_id and vendor_id are hardcoded in the .inf file, if you don't want to use default values, you will have to change them in your program AND in the .inf file.

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBSerial.h"
00003  
00004 //Virtual serial port over USB
00005 USBSerial serial;
00006  
00007 int main(void) {
00008  
00009     while(1)
00010     {
00011         serial.printf("I am a virtual serial port\r\n");
00012         wait(1);
00013     }
00014 }

API

[Not found]

More example

In this example, the program waits a line on the virtual serial port. When it receives a line, it sends it to the usual mbed serial port (the one used to flash a new program) and to the virtual one.

USBSerial echo

#include "mbed.h"
#include "USBSerial.h"

//Virtual serial port over USB
USBSerial serial;
Serial pc(USBTX, USBRX);

int main(void) {
    uint8_t buf[128];
    while(1)
    {
        serial.scanf("%s", buf);
        serial.printf("recv: %s", buf);
        pc.printf("recv: %s\r\n", buf);
    }
}