vitormhenrique
Well-known member
Hello Guys,
Does the serialEvent() function work ("out of the box") for the hardware serial 2 on teensy - lc? Or do I need to attach the interrupt using something like attachInterrupt(INT9, serialEvent, CHANGE);
A code like this would not work...
Does the serialEvent() function work ("out of the box") for the hardware serial 2 on teensy - lc? Or do I need to attach the interrupt using something like attachInterrupt(INT9, serialEvent, CHANGE);
A code like this would not work...
Code:
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
Serial2.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 (Serial2.available()) {
// get the new byte:
char inChar = (char)Serial2.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;
}
}
}