9 years, 9 months ago.

Using Serial and USBSerial in the same program?

I am using LPC11U35/501. I has both A USB interface and a serial (UART) interface. I want to use both in a program that communicates with a computer over USB (using virtual serial port) on one hand and with a ESP8266 wifi module via the UART on the other.

I can make the USBSerial working and I can make the UART working but when I am using both, the UART part works well but the virtual serial port on the computer (Mac OSX) gets only 0x00 bytes.

Is there a way to use both the USB serial and UART in the same program?

My Test Program

#include "mbed.h"
#include "USBSerial.h"
 
DigitalOut led(P0_7);  // LED pin on my board.
 
//Virtual serial port over USB
USBSerial usb_serial;

// Hardware serial to wifi device.
Serial wifi_serial(P0_19, P0_18);  // tx, rx

Timer timer;

void setup() {
  timer.start();
  wifi_serial.baud(19200);
}

void loop() {
  if (timer.read_ms() < 500) {
      return;
  }
  led = !led;    // Invert LED state
  timer.reset();
  usb_serial.printf("usb\r\n");
  wifi_serial.printf("wifi\r\n");
}

int main(void) {
  setup();
  for(;;) {
    loop();
  }
}

1 Answer

9 years, 9 months ago.

I tried your program on TG-LPC11U35-501, it works fine :)

/media/uploads/okano/---------_2015-02-17_08.03.54.png

If you are using CoolTerm or some other GUI terminal application, please try re-establishing the connection after the program start. (This can be done by pressing "Disconnect" and "Connect" buttons.)
Since the program initializes the USB at every time of the program start, the previous connection on the PC is aborted.

reconnect

Accepted Answer

Thanks Tedd, this was very useful. I tried it on a TG-LPC11U35 and my binary run well. I tried it on another board of mine (different design than my first board) and it also worked well. The original board I used is a very minimal design where the 1.5k usb pullup is hardwired to 3.3V (instead of switching with a transistor). For some reason it worked well without the Serial. Possibly the Serial initialization increased the initialization time which resulted in premature enumeration. Lesson learnt. ;-)

posted by Zapta Z. 16 Feb 2015