Arduino Forum

Products => Arduino Due => Topic started by: OptimusPrime on Dec 20, 2013, 03:34 pm

Title: SerialEvent on Due's native USB port never gets fired
Post by: OptimusPrime on Dec 20, 2013, 03:34 pm
Hi Community!
I just tried to modify the example "SerialEvent" in "Communication" to work with the due's native usb port. Everything seems to work out fine, but the SerialEvent is never beeing called. I read through the reference and stumbled upon some threads e.g.  http://forum.arduino.cc/index.php?PHPSESSID=9n0ue2d191td03larfose2vfb0&topic=135011.0 (http://forum.arduino.cc/index.php?PHPSESSID=9n0ue2d191td03larfose2vfb0&topic=135011.0) but i didn't find a solution yet to catch this event.
Anyone struggeling with similar issues? Is there another event for the native port (SerialUSBEvent)?

thanks a lot and merry christmas!
Title: Re: SerialEvent on Due's native USB port never gets fired
Post by: MarkT on Dec 21, 2013, 08:46 pm
Perhaps you could post your code?
Title: Re: SerialEvent on Due's native USB port never gets fired
Post by: OptimusPrime on Jan 01, 2014, 11:47 pm
The code is the example "SerialEvent" in "Communication" of your arduino ide.
Basically it is all about getting this to run with the nativeport.

Works with the programming port:
Code: [Select]

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
hardware serial RX.  This routine is run between each
time loop() runs, so using delay inside loop can delay
response.  Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}


Should work with the native port - but SerialEvent is never called:
Code: [Select]

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  SerialUSB.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    SerialUSB.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
hardware serial RX.  This routine is run between each
time loop() runs, so using delay inside loop can delay
response.  Multiple bytes of data may be available.
*/

////////////////////no magic here
void serialEvent() {
  while (SerialUSB.available()) {
    // get the new byte:
    char inChar = (char)SerialUSB.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
Title: Re: SerialEvent on Due's native USB port never gets fired
Post by: sean_8 on Feb 27, 2014, 08:59 am
I too am having this exact issue. Tried using SerialUSB and Serial0, no dice.
Title: Re: SerialEvent on Due's native USB port never gets fired
Post by: AdderD on Feb 27, 2014, 03:01 pm
Here is a snippet from variant.cpp:

Code: [Select]

void serialEventRun(void)
{
  if (Serial.available()) serialEvent();
  if (Serial1.available()) serialEvent1();
  if (Serial2.available()) serialEvent2();
  if (Serial3.available()) serialEvent3();
}


You will notice that there are 4 serial event handler functions defined. You'll also notice that SerialUSB is not one of them. To the best of my knowledge there is no serialEvent type function for the SerialUSB (native port) on the Due. You need to do this yourself. This isn't as big of a problem as you might expect. That function I showed above? It is called every time your loop function is called. So, really, it is just a convenience. Put these lines in your loop function:

Code: [Select]

while (SerialUSB.available()) {
   //do stuff with data from the native port
}


This will do what you want. Or, put a line just like the ones from variant.cpp in your loop. If you want serialEvent to handle your native USB port traffic then put this line into your loop function:
Code: [Select]

if (SerialUSB.available()) serialEvent();


Problem solved! No, I have no idea why this doesn't seem to be in the Arduino core already.