user42225: The following code, debugs SerialUSB Native Port, plugged to a PC, and connected through a Terminal Monitor (such as Termite, with any bps, 8N1, RTS/CTS).
At start, the ports seeks for communication, and you see fast blinks.
If SerialUSB, Plugged and Connected, communication happens, a.k.a. slow blinks.
But if USB is Unplugged after SerialUSB, even if you Disconnect it before, the code turn into an Halt state, a.k.a. no blinks.
If you Plug back the USB, it returns to !SerialUSB, so if you Disconnect (if it were connected) and Connect back the terminal, it recovers the SerialUSB state, not without a reset() line to react.
Seems the same happens for Serial.
Any clue about why the Arduino goes into that Halt state after a USB disconnect?
How should we deal with that? How one should prevent this state?
Most people asserts this is a power source problem. This happens with and without the 5V Barrel Power Source.
// ARDUINO DUE BLINK LED
void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(115200);
  SerialUSB.begin(115200);
  while(!Serial){
    digitalWrite(13,HIGH);delay(500);
    digitalWrite(13,LOW);delay(500);
  }
  Serial.println("System Reset");
}    
void loop(){  
  int n=0;
  while(!SerialUSB){
    Serial.println("USB Unplugged or Terminal Disconnected");
    digitalWrite(13,HIGH);delay(50);
    digitalWrite(13,LOW);delay(450);
    n=n+1;
    if (n>=5){
      Serial.println("Resetting to Connect if Disconnected");
      digitalWrite(13,HIGH);delay(450);
      digitalWrite(13,LOW);delay(50);
      rstc_start_software_reset(RSTC);
    }
  }
  SerialUSB.println("USB Plugged and Terminal Connected");
  Serial.println("USB Plugged and Terminal Connected");
  digitalWrite(13,HIGH);delay(50);
  digitalWrite(13,LOW);delay(50);
}


user77611: This is not possible to do because the protocol CDC/ACM don't send that type of information so you can't detect if the Arduino is connected or not, that's why it halts. (You could make a keep-alive function but I don't know if this is possible with what you're doing)

42225: <p>The following code, debugs SerialUSB Native Port, <em>plugged</em> to a PC, and <em>connected</em> through a Terminal Monitor (such as <em>Termite</em>, with any bps, 8N1, RTS/CTS).</p>
<p>At start, the ports seeks for communication, and you see fast blinks.</p>
<p>If <strong><code>SerialUSB</code></strong>, <strong>Plugged</strong> and <strong>Connected</strong>, communication happens, a.k.a. slow blinks.</p>
<p>But if USB is <strong>Unplugged</strong> after <strong><code>SerialUSB</code></strong>, even if you <strong>Disconnect</strong> it before, the code turn into an <strong><code>Halt</code></strong> state, a.k.a. no blinks.</p>
<p>If you <strong>Plug</strong> back the USB, it returns to <strong><code>!SerialUSB</code></strong>, so if you <strong>Disconnect</strong> (if it were connected) and <strong>Connect</strong> back the terminal, it recovers the <strong><code>SerialUSB</code></strong> state, not without a <code>reset()</code> line to react.
Seems the same happens for <code>Serial</code>.</p>
<p><strong>Any clue about why the Arduino goes into that <code>Halt</code> state after a USB disconnect?</strong></p>
<p><strong>How should we deal with that? How one should prevent this state?</strong></p>
<p>Most people asserts this is a power source problem. This happens <em>with</em> and <em>without</em> the 5V Barrel Power Source.</p>
<pre><code>// ARDUINO DUE BLINK LED
void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(115200);
  SerialUSB.begin(115200);
  while(!Serial){
    digitalWrite(13,HIGH);delay(500);
    digitalWrite(13,LOW);delay(500);
  }
  Serial.println(&quot;System Reset&quot;);
}    
void loop(){  
  int n=0;
  while(!SerialUSB){
    Serial.println(&quot;USB Unplugged or Terminal Disconnected&quot;);
    digitalWrite(13,HIGH);delay(50);
    digitalWrite(13,LOW);delay(450);
    n=n+1;
    if (n&gt;=5){
      Serial.println(&quot;Resetting to Connect if Disconnected&quot;);
      digitalWrite(13,HIGH);delay(450);
      digitalWrite(13,LOW);delay(50);
      rstc_start_software_reset(RSTC);
    }
  }
  SerialUSB.println(&quot;USB Plugged and Terminal Connected&quot;);
  Serial.println(&quot;USB Plugged and Terminal Connected&quot;);
  digitalWrite(13,HIGH);delay(50);
  digitalWrite(13,LOW);delay(50);
}
</code></pre>
 77611: <p>This is not possible to do because the protocol CDC/ACM don't send that type of information so you can't detect if the Arduino is connected or not, that's why it halts. (You could make a keep-alive function but I don't know if this is possible with what you're doing)</p>
