How can I know USB is connected or not?

While (! Serial){};

Seeeduino XIAO could not determine if there is a USB connection using the above syntax.
Do you know how to determine USB is connected or not?

On the Xiao BLE you can read the status of the Charge LED to determine if USB is connected

Thank you for your suggestion.

But This method just gives information of “USB power is connected” .
I want to know Serial line is dead or alive.



To see if the serial line is alive there must be a program on the PC side that has opened the serial port. Then
if (! Serial) will be true.

Just a USB connection without opening the port is just powering the XIAO.

Thank you rbehm

I already tried it.

Once it is true, it remains true even if the serial port is closed.
I also want to know that the serial port is closed.