Arduino Due gets crazy if unplugged while sending

Hi all,
I´m trying to develop an aplication where the arduino Due is continuously working (always powered), and I can connect to it using the native USB, so I can get data about the process. If the native USB is unplugged while sending data, the serialUSB communication gets crazy when plugged again. The data sent via USB isn´t sent in the same order it should.
I have tried to reset the object SerialUSB using the provided functions SerialUSB.end() and SerialUSB.begin(..), but when connected again the messages are wrong.

I wrote a simple code to try to understand what was wrong.

long tm;
uint32_t ct=0;

void setup()
{
  SerialUSB.begin(115200);
  tm=millis();
  while(!SerialUSB)
    ; 
}

void loop()
{
  if(millis()-tm>10 && SerialUSB)
  {
    tm=millis();
    SerialUSB.print("Va");
    SerialUSB.print("lue");
    SerialUSB.print(":");
    SerialUSB.print(ct);
    SerialUSB.println("");
    ct++;
  }
}

At the begining, when powered on the board the messages are received correctly.

Value:2
Value:3
Value:4
Value:5
Value:6
Value:7
Value:8
Value:9
Value:10

If unplugged the USB while sending data, and then connected again, the data is sent in different order. When unplugged, the board keeps powered with the other, so it´s not restarted, because I need that the board keeps working although the native USB is not connected.
The received data becomes like this one

2290lueVa2291:Va
:lue
2292lueVa2293:Va
:lue
2294lueVa2295:Va
:lue
2296lueVa2297:Va
:lue
2298lueVa2299:Va
:lue
2300lueVa2301:Va
:lue
2302lueVa2303:Va
:lue

The difference in the count is correct, it´s because the program keeps running.

It keeps sending data in this way until the board is reseted or power off/on.
Any idea about what´s happening with the communication or how could it be solved without reseting the board will be apreciated.
Thanks.

I tried your sketch with some minor modifications and it works if the board is always powered thru the jack with a 9V battery pack. Although when I plug again the native USB port, I have to plug/unplug/plug until I see the correct Com Port in Tools>Port : (COM8(Arduino DUE(Native USB Port)))

void setup()
{
  SerialUSB.begin(115200);
  while(!SerialUSB);
     
}

void loop()
{
  static uint32_t tm = millis(), ct;
  
  if(millis()-tm>10)
  {
    tm=millis();
    SerialUSB.print("Value :"); 
    SerialUSB.print(ct);
    SerialUSB.println(" ");
    ct++;
  }
}

How do you power the board while native USB port is unpluged ?

Hi ard,
thanks for your prompt reply.
When unplugged the native USB it´s powered using the second USB connector. I have recorded a video to show better how do I get that errors. I also tested the same in other boards, with the same problem.

Thanks.

Add Serial.begin(115200); just before SerialUSB.begin(115200); in the code I posted above and your board will be powered by the USB cable connected to the programming port.

Adding Serial.begin(115200) before the SerialUSB.begin(115200) has no efect in the behaviour of my board. Without adding that, the board is powered with the other USB, the count doesn´t stop when plugged again the native USB.
I will try to upload the same sketch changing the Due library version. I don´t know what kind of test could be made to understand what´s wrong.

With the 1.6.6 version of the DUE compiler, the output is exactely as expected with the code I provided.

I have tried your code, and changed the sam library version to 1.6.6
The output after making the Native USB unplug (while sending data via terminal) and plug againkeeps being wrong. The order of the message has changed, with my code happened the same.
It is the output with the 1.6.6 version and your code.

788Value :
 789Value :
 790Value :
 791Value :
 792Value :
 793Value :
 794Value :
 795Value :
 796Value :
 797Value :
 798Value :
 799Value :
 800Value :
 801Value :

when it should be

Value :788
Value :789
Value :790
...

I can't reproduce your issue, I suspect something weird from the PC side.

Let's try that: You upload your sketch, select the correct Native USB COM port, open the Serial monitor window.

--> You see a correct output

--> Now close the Serial monitor window, but do not unplug the USB cable from the native USB port

--> Reopen the Serial monitor window. Do you have a correct output ?

Hi,
Yes, following the secuence that you say, the output isn´t correct. The is no problem if the board is not unplugged. If I just close the monitor and open it again averything is fine.
I follow the next steps.
--> Upload the scetch to the board, and open the serial monitor. (Data received correctly).
--> While serial monitor is opened, unplug the Native USB (while powered with the other USB)
--> Plug the Native USB again. Close the monitor opened before(because it is stopped).
--> Open a new serial monitor, and the received data from that moment the data order is wrong.
Like recorded in the video shared before.

No one else has this problem? :frowning:
I tried the same with different PCs, 2 boards, and different arduino versions of SAM library.

Thanks for your interest Ard.

Do you connect your board behind a hub or directly into the PC ?

What happens if you use the 'programming' port for the communications instead of the 'native port'?

Hi Southpark,
using the programming port (once dissabled the auto reset on serial connection, 3.3V-220Ohm-Rst), works fine as it should.
The problem is only with the native usb.

ard_newbie:
Do you connect your board behind a hub or directly into the PC ?

I didn't see this answer the last day, sorry.
Yes, in this test I'm using a hub to connect both USB cables. But I have the same problem without any hub, with the USB directly to the PC.

I am still having the same problem with the Due. I tried with a new computer, and installing the new arduino and Due library versions. When unplugged the Native USB cable while sending data, and powered through the programming USB to kkep the program running, when opened the Serial monitor, the data received is wrong. The order of the messages changes. Also tried with different arduino boards.
No one else having this isue?