USB hot-plug doesn't work on Arduino MKRZero?

Is this a bug in the Arduino USB code or am I doing something wrong?

The problem can very easily be recreated in a simple hello world sketch

With the latest Arduino installed (1.8.9):

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  Serial.println("Hello World!");
}

Then:

  1. Upload the sketch.
  2. Open the COM port (I use Putty) and see the "Hello World" text.
  3. Power the MKRZero from external source (+5V on Vin).
  4. Disconnect USB cable to PC and close Putty terminal.
  5. Reconnect USB and open COM port (Again I use putty).
  6. Verify that the "Hello World" text is received.
  7. Try sending data to the Arduino by typing in the putty window.
    Step. 7 will result in the serial program "crashing" with the error: "Unable to write to serial device".

I first saw this problem in some application code (C#) I was writing, but then traced it all the way back to this simple test.

The Arduino serial monitor doesn’t report any problem, but it is clear (if the Arduino is set to loop back received data) that the data is not sent.

The only way I found to get out of this problem, is to power the hardware OFF and then ON Again (which is a no go for my application).

Perhaps the USB code is missing some sort of re-init when hot-plugged?

I will try and see if I can work around this with USB.detach / attach or something... but it would be nice to get this looked at by some more skilled programmers.

Lagoni's issue report on this topic:

That short snippet of code is not set to ECHO anything it gets from the RX side it is only set to TX.

Some terminal programs can use a local echo which just repeats regardless of what the other end is doing.
If the settings are not correct some programs will as you see report that they are unable to write to the end device as the ends device was never set to respond.

Maybe this example might be better for you ?

Bob.

ballscrewbob:
That short snippet of code is not set to ECHO anything it gets from the RX side it is only set to TX.

Some terminal programs can use a local echo which just repeats regardless of what the other end is doing.
If the settings are not correct some programs will as you see report that they are unable to write to the end device as the ends device was never set to respond.

Maybe this example might be better for you ?

Bob.

Correct, this short example doesn’t empty the RX buffer, but the terminal still is able to transmit to it the first time the device is powered up. But after a hot-plug the terminal can no longer write to the RX buffer (and hence the terminal error).
I tried this on my Linux machine and it doesn’t give any terminal error (same with the Arduino serial monitor), the COM port is correctly detected after hot-plug but no characters are received in the Arduino RX buffer.
This becomes clear if the Arduino codes write back the characters received from the terminal:

void loop() {
 // put your main code here, to run repeatedly:
 if(Serial.available()){
   Serial.write(Serial.read());  
 }
}

I therefore believe the problem to be with the serial part of the Arduino application not handling the hot-plug correctly and not so much with the USB part.