Pages: [1]   Go Down
Author Topic: Serial issues with a leonardo  (Read 1090 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
or is it me?
I'd go with it being just you. Or, more likely, your undisclosed code.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;
    }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(Serial.available() > 0)
   serialEvent();
to the end of loop() accomplishes the same thing.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Pages: [1]   Go Up
Jump to: