SerialUSB com error on Arduino DUE

Hello everyone,

I am currently programming an app for an Arduino UDE which will be a serial gateway between a device and my computer, it needs to transmit a Linux debug flow to my computer's serial port. Sadly, the issue i am encoutering is that for a reason i ignore, the SerialUSB port seems to freeze and crash all the other serial ports on the DUE without emitting a single byte. I have tried sending this debug flow through the Programming Port and it worked just fine. HEre is the program i made.

//DUE
const String DEVICE_NAME = "IMX_Loader";
const String SCRIPT_VERSION = "v1.0";
const String DEVELOPPER = "by Joris LE MALLET";
String Data1 = "Data 1 received = ";
String Data2 = "Data 2 received = ";

int n = 0;
int number;
int BootPin = 8;
int ResPin = 51;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
  SerialUSB.begin(115200);
  Serial.setTimeout(2);
  SerialUSB.setTimeout(6);
  Serial1.setTimeout(6);

  pinMode(BootPin, OUTPUT);
  pinMode(ResPin, OUTPUT);
  digitalWrite(ResPin, HIGH);
  digitalWrite(BootPin, LOW);
  number = 0;

  while(!SerialUSB){
    
    };
}

void loop() {
  // put your main code here, to run repeatedly:
  //SerialUSB.println(number);
  if(Serial.available() > 0){ //Programming port of the DUE, used to toggle the signals (RESET, BOOTSTRAP) and to put the DUE in Gateway mode or in piloting mode
          String strRecu = Serial.readString();
          if(strRecu.equals("*IDN?")){
              SerialUSB.println(DEVICE_NAME+" "+SCRIPT_VERSION+" "+DEVELOPPER);    
          }
          if(strRecu.equals("Toggle pilot mode")){ //Command to start the piloting mode for the relays
            Serial.println("Entering piloting mode");
              while(Serial.available() == 0){
                
                    }
             strRecu = Serial.readString();
             if(strRecu.equals("start bootstrap")){ //Command to turn the LED on
                    digitalWrite(BootPin, LOW);
                }
             else if(strRecu.equals("stop bootstrap")){
                    digitalWrite(BootPin, HIGH);
                }
             else if(strRecu.equals("Reset")){
                    digitalWrite(ResPin, LOW);
                    delay(3000);
                    digitalWrite(ResPin, HIGH);
                }
           }
           else if(strRecu.equals("Toggle gateway mode")){
                //Serial.println("Entering gateway mode");
                int i = 0;
                Data1 = Data1 + i;
                //Serial.println(Data1);
                SerialUSB.println("Hello1");
                while(Serial1.available() == 0/*&&(SerialUSB.available() == 0)*/){
                }
                while(1){
                    while(Serial1.available() != 0/*&&(SerialUSB.available() == 0)*/){
                      Serial.println(Serial1.readString());
                    }
                    
                  }
                
                
                Data2 = Data2 + i;
                //Serial.println(Data2);
                //Serial.println("Data 2 received = %i");
                SerialUSB.println("Parsing...1");
                while((Serial2.available() > 0)||(SerialUSB.available() > 0)){
                  Serial.println("Data received to be parsed");
                  SerialUSB.println("Parsing...2");
                      if(Serial2.available() > 0){ //Hardware Serial 2 (pins 16 and 17), used to send the commands and to read the debug flow from the PFD2020
                              /*strRecu = Serial2.readStringUntil(0x0D);*/
                              SerialUSB.print(Serial2.readString());
                              SerialUSB.print("\n");                           
                      }
                      if(SerialUSB.available() > 0){ //NAT port of the DUE, used to read the commands destined to the PFD2020
                      // strRecu = ;
                         Serial2.print(SerialUSB.readString());
                         Serial.println("Hello2");
                      }
                }                  
          }
    }  
    //number ++;
   // delay(20);
}```

This bit of code is suspect.

while(1){
                    while(Serial1.available() != 0/*&&(SerialUSB.available() == 0)*/){
                      Serial.println(Serial1.readString());
                    }
                    
                  }

It anyway creates an infinite loop (the while(1)) in which only Serial1 is being handled. I am not quite sure of your intentions here.

To test if i had an issue with the board itself or not i wrote this infinite loop to keep sending data without pause, i commented the SerialUSB to check if the SerialUSB.available() would block or not the DUE. The problem is that if you replace Serial by SerialUSB in the println, you won't get any of the data you received. the while(1) will in the end be used as a timeout (1 will be replaced by a value which will be decremented if there is no data to print until it reached 0 to create a timeout)

I think you are programming a 'sketch' for a 'DUE'

I actually suspect that it has something to do with the cross between Serial & SerialUSB
Serial is as far as i know connected to the USB port already, with it's pre-set baud-rate etc.
SerialUSB accesses the native USB on the MCU directly.
These are not separate ports.
On a Micro, the native USB UART is not accessible through any pins, and it isn't either on a DUE, but UART0 does forward the data to the native USB.
I would remove all references to 'Serial' or 'SerialUSB' and see if the problem persists.
Your line

if(Serial.available() > 0){ 

disables any part of the program when there is nothing in the Serial0 buffer, but if all that comes in ends up in the SerialUSB buffer, that condition will never be true.
I don't think they are actually 2 separate ports. I may be wrong, i don't own a DUE and so i can not verify this.

also i suggest you comment these lines out for now until you have it all working properly.

  //Serial.setTimeout(2);
  //SerialUSB.setTimeout(6);
  //Serial1.setTimeout(6);

The exact behavior of setTimeout() is not quite clear to me.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.