USBSerial issue

16 Apr 2012

I was playing with USBserial library and found something really annoying. The main loop woudn't be executed if USB isn't connected. So, while the usb is plugged, LED1 blinks, when unplugged, LED1 stops. Also, if connected for some time, it would stop anyway if hyperterminal or some other pc aplication isn't using that virtual port.

How to detect USB state? How to ignore USB not presented and execute the rest of the code anyway?

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

USBSerial pc;

DigitalOut myled1(LED1);

int main() {
    while(1) {
        myled1 = 1;
        wait(0.2);
        myled1 = 0;
        pc.printf("I am a virtual serial port on LPC\r\n");
    }
}
17 Jun 2014

I'm having same problem.

USBSerial constructor is blocking. Is there any way to detect if there is serial connection before hand?

17 Jun 2014

Current version of USBSerial can connect non-blocking, see: http://mbed.org/users/mbed_official/code/USBDevice/docs/8ef73dd868a0/classUSBSerial.html. Dunno how to check if it is connected. Maybe using .configured() function works for that.

17 Jun 2014

Here is a hack I did a while back. Looks like between what there and some of this there may be some nice enhancements.

http://mbed.org/users/setcom_001/code/USBDevice/file/a9671b78d24e/USBSerial/USBSerial.cpp

Looks like I was working offline at the time and used a code formatter so the commits aren't easy to decode but you can poll the VBUS line to see when its connected and also detect if the terminal on the host is open or not.

You can do something like

switch ( _terminal_mode ) {
        default:
        case USB_NOT_CONNECTED:
            if( _comport.vbusDetected() ) {
                _comport.connect();
                _terminal_mode = TERMINAL_CLOSED;
            }
            break;
 
        case TERMINAL_CLOSED:
            // if the terminal is closed we cant be under PC control
            _status.pc_mode = 0;
 
            if ( _comport.vbusDetected() ) {
                if( _comport.terminalIsOpen() ) {
                    // change the state
                    _terminal_mode = TERMINAL_OPEN;
                    // service used vars
                    loc = 0;
                    _comport.flush();
                    // indicate that the terminal is open in code (UI)
                    _status_led_state = STATUS_TERMINAL;
                    // clear the console each time it's openend
                    _comport.printf( "\033[2J" );
                    _comport.printf( "\033[1;1H" );
                }
            } else {
                _comport.disconnect();
                _terminal_mode = USB_NOT_CONNECTED;
            }
 
            break;
 
        case TERMINAL_OPEN:
            if ( _comport.vbusDetected() && _comport.terminalIsOpen() ) {
                // make sure their is something to read
                if( _comport.available() ) {
                    // read data
                }
            } else {
                _terminal_mode = TERMINAL_CLOSED;
            }
 
            break;
    }
18 Jun 2014

Is VBUS not also detected when using the Mbed USB port? I need to detect if the USBSerial serial port is connected or not.