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);
}