I have a leonardo connected to my laptop. Idon't play with serial too much, nottwo way atleast. my sketch didn't work (serialevent never got called) so I tried the serial event example sketch, and that doesn't work w/ my leonardo either. If I just have a Serial.Print, it displays fine on the laptop, but as I said, serial event never seems to be called, e ven in the example.
I didn't post the code since I said I was using example code, I figured there was no need.
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;
}
}
}
I'll repeat my question once more. What is sending the serial data? Yes, I know it is the PC, but a PC is collection of hardware. There is some application on the PC that sends data, not "the PC".
I've confirmed that the serialEvent() method is NOT called on the Leonardo. Not that is is really a big deal, as all that means is that at the end of loop, the amount of data in the serial buffer is determined, and if that is not zero, the function is called.
I have never used serial event before, always did it sas you described. Untill I was fighting this issue I always thought SerialEvent did some magick behind the scenes (say some kind of "highlevel" inturrupt or something else I don't know about) so that it was actually called outside of normal loop() looping. Is there some inherent benifit to using it, or does it just simplify coding for new folks? I don't plan on using it any more, just adds confusion and strange flow to my cod.e