As far as I can tell the only place I access the usb if through the console (Serial) port in my log function. So I added this to no avail:
bool LogManager::checkSerialConnection() {
if (Serial) {
if (!m_serialConnected) {
m_serialConnected = true;
LOG("SERIAL CONNECTED");
}
} else {
if (m_serialConnected) {
m_serialConnected = false;
LOG("SERIAL NOT CONNECTED");
}
}
return m_serialConnected;
}
Is there something we can do to properly shut down the Serial port on cable removed? I feel like I should be calling Serial.end() but I have to check for the Serial first and it hangs? Its disconcerning to think I have to keep my Arduino cabled to be stable.
I'm absolutely not familiar with the Portenta. If the board uses native USB for communication with the PC, the following is my experience with the 32U4.
Once is connection with the PC is established and you remove the USB cable, the board can't get rid of data that you print, the output buffer will fill up and your code will come to a (near) grinding halt.
To work around that you need to check if there is still space in the buffer to print.
For 32U4 based boards, you can use Serial.availableForWrite() to check how much space is still available and only print if there is sufficient space.
void setup()
{
Serial.begin(115200);
// wait till connection with PC is established
while(!Serial);
// print the initial size of the buffer so you have some guidance
Serial.println(Serial.availableForWrite());
}
void loop()
{
if(Serial.avaliableForWrite() >60)
{
Serial.println("Hello World !!");
delay(1000);
}
}
As said, I do not know anything about the Portenta so can't even say if it compiles. The above code is intended as a guide to possibly solve your problem.