[Solved] SerialUSB - checking if connection is (still) present

I found a solution while looking through the USB stack files. There's a method in the SerialUSB class that returns true whenever the host (my C++ app, a serial monitor etc.) is connected:

bool Serial_::dtr() {
	return _usbLineInfo.lineState & 0x1;
}

That information seems to be contained in the first bit of _usbLineInfo.lineState as you can see. For those who did not know (like me :slight_smile: ), the SerialUSB class is declared in USBAPI.h and defined in CDC.cpp.

I also found an alternative to checking the electrical connection of the data lines, as (beautifully) explained by ard_newbie. The method below:

//	VBUS or counting frames
//	Any frame counting?
uint32_t USBD_Connected(void)
{
	uint8_t f = UDD_GetFrameNumber();

	delay(3);

	return f != UDD_GetFrameNumber();
}

Is declared in USBAPI.h, defined in USBCore.cpp, and can be called from the sketch directly as it is not part of any class or structure. The downside to this alternative is the 3 ms delay embedded to the method.

I have version 1.6.11 (the latest, to this date) of the Arduino Due libraries, where the files mentioned above are present.

Thanks!