Delaying USBSerial connect

Now that Zoltan has helped me get USB Serial working in general, I’ve another issue with USBSerial.

What I’m trying to do is have the device optionally connect to a port. In my “big” program, when I use a USBSerial, it blocks the device indefinitely until I scan my serial ports, then it frees up the device.

I’ve some sample code here that demonstrates what I’m trying to do, but it doesn’t quite work. The serial device never shows up on my computer.

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

//Virtual serial port over USB
USBSerial serial(false);

int main(void) 
{

	wait(10.0);
	serial.init();
	wait(5.0);
	serial.connect();
    while(1)
    {
        serial.printf("I am a virtual serial port\r\n");
        wait(1.0);
    }
}

If I remove the waits and change the initializer to just USBSerial serial, it works as expected, though I suspect that it hangs until the device is connected.

My “big” program uses an LCD screen, and I’ve made the USBSerial work with it but only if I have it connect to the computer immediately, and even then it only starts when I scan for USB devices on my terminal program. I’d really like to have it allow for an optional connection to USBSerial for optional debugging without it delaying the startup of the device.

Thanks!

Hello Glyn,

I have added an LED to your code, built with the latest mbed-os and tested it on Linux. It seemed to work correctly:

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

//Virtual serial port over USB
USBSerial serial(false);
DigitalOut  led(LED1);

int main(void)
{

    wait(10.0);
    serial.init();
    wait(5.0);
    serial.connect();
    while(1)
    {
        serial.printf("I am a virtual serial port\r\n");
        led = !led;
        wait(1.0);
    }
}
  • Reset the board without having the USB plugged to the computer.
  • After 15 sec the LED started to blink.
  • Plugged the USB to the computer.
  • A new ttyACM1 serial device was detected (without having to scan the USB devices on the terminal program).
  • The LED continued to blink.
  • Run a terminal program and it printed the USBSerial messages correctly.
  • Unplugged the USB from the PC.
  • The terminal program stopped to print the serial messages.
  • The LED continued to blink.
  • Tried to pug and unplug the USB several times and it always worked correctly (as described above).

OK - this is just strange.

Your code did not work with my Mac, but when I shortened the 10 second wait to 5 seconds, it worked. Also, I could remove the .init() call and it still worked as long as I had a shorter delay in there. I wonder if it has anything to do with interrupts or the delay function, because I moved the code to my “big” program and with just the connect it worked perfectly.

If I try to call .connected() it doesn’t work (that is, it doesn’t do what it’s supposed to do when that’s called) the main connection still happens.

Thanks for your help!

HI @glyngowing

It has been a while. What is your status on USBSerial?

I was hoping to replace replace CP210X with the USB stack on STM32 but have trouble to get it to work reliably.

Hello Zoltan!
Where is this USBSerial.h library located
Thanks in advance!

Hello Zhiyong,

The USBSerial seems to work reliably on an STM32F407VE also with Mbed OS 6. But I have tested it only on Linux (Ubuntu 18.04). Also notice that the USBSerial wants a DTR signal on the terminal side as explained here by Johannes.

Best regards,

Zoltan

1 Like

Hello Nik,

The USB became a part of Mbed OS. No additional library is needed to be linked. The USBSerial.h is located in the mbed-os/drivers folder and the API documentation is available here. However, you have to check whether the selected target has an “USBDEVICE” entry in the “device_has” section of the mbed-os/targets/targets.json file.

Best regards,

Zoltan