Setting up Serial USB communication between STM32 and PC with Mbed library

mbed serial
stm32 serial communication
stm32 usb virtual com port example
stm32 usb serial
mbed usb serial driver
mbed serial example
mbed serial port
mbed lpc1768 serial driver

I hava an STM32 f401RE. I am using Mbed library for setting up a conexion from STM32 to PC. I want to send via serial a char sequence to the board. As an answer i expect a blinking LED. E.g: led1 results in LED ON, led2 results in LED OFF.

The problem is that i don't know how to set the port for the connection.

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

//Virtual serial port over USB
USBSerial serial;

int main(void) {

        serial.printf("I am a virtual serial port\r\n");

You can use the USBSerial interface to emulate a serial port over USB. You can use this serial port as an extra serial port or as a debug solution. It also communicates between Mbed and a computer.

I would like to do all the above(even thogh i don't know what does emulate a serial port over USB. What is that Virtual USB?).

I see that USBSerial constructor takes USBSerial (bool connect_blocking=true, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, uint16_t product_release=0x0001). And i think i need to modify some of this adresses. The problem is that on Windows the ports are represented in Device Manager with COMxx and on Linux like ttyACMxx. How would i transform this in hexa adresses - is this what i have to do?

Ignore that 'b'. Your device is not seeing that 'b'. It is just being printed by serial terminal utility. Also I would like to mention what I got from your question is, you want to send some data from PC to board over Serial and if device receives that data, it should start blinking the LED. If that is correct, use the code below:

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
DigitalOut led(LED1);    // If blinking doesn't work with LED1, Check the pin map for your board and pass the LED pin instead of LED1

char token = 'a';        // This is the character that you should send to trigger blinking
bool startBlinking = false;

int main() {

        if (pc.getc() == token) {
            startBlinking = true;
        if (startBlinking) {
            led = 1;
            led = 0;

SerialPC, See Windows-serial-configuration for full details about setting up Windows for serial It is common to use a terminal application on the host PC to communicate with the Communication over the USB Serial port simply uses the standard Serial modify the settings of both the Serial Interface and the Host PC application! Setting up Serial USB communication between STM32 and PC with Mbed library Hot Network Questions Agile: in a sprint retro can I bring up a negative which isn't affecting me personally but I know another team member has a problem with it?

You should not have to transform anything or mess with the USB product_id or vendor_id, an mbed serial port should show as any other serial port so if it doesn't for you it means you are having driver issues.

On most recent Linux distros the device should show something similar to the following kernel messages:

 cdc_acm 5-2:1.1: ttyACM0: USB ACM device
 usbcore: registered new interface driver cdc_acm
 cdc_acm: v0.26:USB Abstract Control Model driver for USB modems and ISDN adapters

On Windows, you will probably need to install drivers. After you do that, the serial port should show as mbed Serial Port (COMx) on your Device Manager. There are many places you can get troubleshooting help, see here, for instance.

The fact that you are getting nothing on both Windows and Linux makes one wonder if you are using the right cable (some USB cables work only for charging and are no good for your purposes, and some others simply fail after a while). I would first make sure your cable works with other devices (obviously not for charging only). There is also the possibility your board went (or came from the factory) bad, but that's quite unlikely.

Serial, Log in or Sign up One of the Serial connections goes via the mbed USB port, allowing you to easily communicate with your host PC. Import librarymbed route to the interface USB Serial port so you can communicate with a host PC. The default setting for a Serial connection on the mbed Microcontroller is 9600 baud. Communication over the USB Serial port simply uses the standard Serial Interface, specifying the internal (USBTX, USBRX) pins to connect to the Serial Port routed over USB. The Serial Interface defaults to a 9600 baud standard serial connection (8 bits, 1 stop bit, no parity), so your host program should be set to the same settings.

I just found this approach and it is working. The thing that i don't understand is why on my pc i get this message: b'Hello World!\n'

#include "mbed.h"

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

int main() {

        pc.printf("Hello World!\n");

STM32F103C8T6_USBSerial - Building a USB serial device , Building a USB serial device with STM32F103C8T6 board. This is a great solution to communicate easily between the microcontroller and a computer. These instructions explain how to setup the mbed Microcontroller to use the USB serial port on mbed-STM32F103C8T6.lib, 67, Revisions Annotate. STM32 micros just like any other micro provide hardware for serial communication. As we all know serial communication is a very important tool for debugging, connecting with external hardware like RFID, GPS, GSM modems, etc. and for performing other communication-related tasks. STM32s have several hardware serial (USART) ports.

USBSerial, 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. If you have multiple Mbed devices but the serial port only appears for one of them: Make sure you run the installer for every device (plug in the device over USB and run the installer again); Windows loads the driver based on the serial number, so it needs to be run for each device individually.

Creating Console Output, Connect your ST Nucleo F411RE board to your PC via USB cable; Determine the STM32 Right click on the STM32 STLink icon and select Properties; Click on the Hardware Tab Install or open a terminal emulator for serial communications Check the serial port settings by selecting the Setup::Serial Port… menu item� It also comprises the STM32CubeWB MCU Package composed of the STM32Cube hardware abstraction layer (HAL) and the low-layer (LL) APIs, a consistent set of middleware components such as USB Device, STMTouch, STM32_WPAN (Bluetooth ® 5 profiles and services, OpenThread and 802.15.4 MAC services), FatFS and FreeRTOS™ kernel, plus Bluetooth ® 5

Windows serial configuration, Industry standard TLS stack and crypto library This content relates to a deprecated version of Mbed The mbed serial port works by default on Mac and Linux, but Windows needs a how to setup the mbed Microcontroller to use the USB serial port on Windows. Download the installer to your PC, e.g. your desktop. Hello and welcome back. Continuing our discussion on RS232 serial communication in this part we will make a RS232 level converter. In the last tutorial we saw that how RS232 level signals differs from normal logic signals. So to interface RS232 level signals to our MCUs we need a "Level converter". And in this tutorial we will make one. What a level converter will do is to convert RS232 level

  • for these boards, I prefer using Arduino since serial-debugging is already implemented. See this link on how to configure the Arduino IDE:
  • Sorry for my question but using arduino on this boards does it affects the performance of stm32? Does it works fully? What is the difference of using stm32 with arduino vs using stm32 using HALL vs using stm32 using Mbed?
  • The stm32 implementation on arduino is fine and won't slow down your performance that much. It's a great starting point for fast prototyping. You can also take a look at "Atollic TrueSTUDIO", it takes a bit more time to set up but "line-by-line"-debugging is a big plus!
  • I made a confusion. I thought i need to setup something on PC, then something on board in order to get it functional. I can see my board as COM3 on windows. I just don't know how to setup a serial communication to my board...
  • I see what you mean. Then you should already have a fully functional setup. Just install any terminal utility you like (TeraTerm, RealTerm, puTTY,...), point it to COM3, boot your board and you should get "I am a virtual serial port on the terminal. Then you'll have to build it up from there, if you want to blink an LED you'd better take a look here
  • well, that's what your code is supposed to be doing: writing the message Hello World in a loop to its serial port. On your computer, you are only reading what you get on the port. If you want to blink an LED when you input a key on your computer you'll have to change your code to read from the port and instruct the board to blink the LED, look at the link I posted on my comment above.
  • How can i get rit of the b in front of my message?
  • That b is coming from the visualization on your terminal utility, you should be able to get rid of it manipulating its settings