Go Down

Topic: Serial issues with a leonardo (Read 1 time) previous topic - next topic

permnoob

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.

is this a leonardo issue?  or is it me?

permnoob

ok it seems to be a leonardo issue.  I knew I would regret trying new hardware

PaulS

Quote
or is it me?

I'd go with it being just you. Or, more likely, your undisclosed code.

permnoob

I didn't post the code since I said I was using example code, I figured there was no need.

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;
    }
  }
}


PaulS

What is sending the serial data? I see no proof that serialEvent() is not called.

permnoob

I have it connectedto my laptop via USB cable.  If I add somethign like

Code: [Select]

delay(2000);
Serial.println("Setup compelte");

to the end of setup, I see that msg on the laptop screen no issue, so I know serial com works.

permnoob

adding a similar chunk of code inside of serialevent never prints to the seral monitor.

PaulS

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".

permnoob

I am sending it using the serial monitor from the arduino IDE

PaulS

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.

Adding
Code: [Select]
if(Serial.available() > 0)
  serialEvent();

to the end of loop() accomplishes the same thing.

permnoob

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

PaulS

Quote
Is there some inherent benifit to using it

No.

Quote
or does it just simplify coding for new folks?

No.

Personally, I think it was a useless addition to the Arduino tool set. A solution looking for a problem, rather than a problem needing a solution.

Quote
I don't plan on using it any more, just adds confusion and strange flow to my cod.e

Agreed, especially since you must use global variables to have any data sharing with loop().

Go Up