Arduino Forum

Products => Arduino Due => Topic started by: adrianMC on Mar 21, 2019, 10:12 am

Title: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 21, 2019, 10:12 am
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.


Code: [Select]

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.

Code: [Select]

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
Code: [Select]

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.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: ard_newbie on Mar 21, 2019, 10:57 am
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)))

Code: [Select]

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 ?
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 21, 2019, 02:13 pm
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.

https://youtu.be/1HOkCi_645E

Thanks.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: ard_newbie on Mar 21, 2019, 02:37 pm

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.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 22, 2019, 08:38 am
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.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: ard_newbie on Mar 22, 2019, 08:44 am

With the 1.6.6 version of the DUE compiler, the output is exactely as expected with the code I provided.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 23, 2019, 09:58 pm
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.

Code: [Select]

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


when it should be

Code: [Select]

Value :788
Value :789
Value :790
...
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: ard_newbie on Mar 24, 2019, 06:19 am
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 ?
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 24, 2019, 09:21 pm
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? :(
I tried the same with different PCs, 2 boards, and different arduino versions of SAM library.

Thanks for your interest Ard.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: ard_newbie on Mar 25, 2019, 06:02 am

Do you connect your board behind a hub or directly into the PC ?
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: Southpark on Mar 25, 2019, 06:15 am
What happens if you use the 'programming' port for the communications instead of the 'native port'?
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 25, 2019, 08:42 am
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.
Title: Re: Arduino Due gets crazy if unplugged while sending
Post by: adrianMC on Mar 28, 2019, 10:02 am
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.