6 years, 8 months ago.

Serial vs USBSerial and blocking

Can someone clarify the difference between using Serial or RawSerial through a USB port versus using the USBserial?

I was reading another post about USBserial blocking when the USB was not connected. Does this same problem occur with Serial? If using RTOS, can I assume it only blocks the thread that is trying to use the USB port?



2 Answers

6 years, 8 months ago.

Serial is a regular serial which goes via the interface chip most mbed devices have (not all of them, some NXP ones have an on-chip USB bootloader and don't have a seperate interface chip), which translates it into USB.

USBSerial uses directly the USB of the target chip. For the LPC1768 you have these are the D+ and D- pins, the USB connector is regular Serial.

USBSerial can these days also connect non-blocking. To be fair imo it is more a workaround, but it works. Blocking wise Serial blocks a bit when transmitting (depends on number of bytes, BufferedSerial is an alternative which does not block). USBSerial by default blocks until connected. When you use RTOS it depends on the priority of the thread. If that thread has higher priority it will block. With lower it won't, with equal I think it also won't block completely.

Accepted Answer
6 years, 8 months ago.

Do you have an example of using non blocking ??

One of my projects would really benifit From having the ability to randomly connect and disconnect USB.

...board powered from batteries.



The last argument of the USBSerial constructor defines if it blocks or not, so you then also got to copy VID and similar arguments into the constructor. I believe you can check the isConfigured() function to see if it is done connecting, but I don't know how it behaves with connects/disconnects.

I have only done it with a USBMSD class myself, and there all of it was interrupt driven, so I didn't have to worry if it was connected or not, interrupts were only generated if it was connected.

posted by Erik - 20 Jun 2014

Are you using USB Tumb drive ? If so can you share code. So far I have to reset my MBED (own PCB) to read the new file. Cheers Ceri

posted by ceri clatworthy 21 Jun 2014