Mega 2560 not reading from serial line

Hello,

I was trying some of the basic Serial sketch examples on my R3 Mega 2560. It seems unable to recognize characters in the buffer (Serial.available(), serialEvent do not work). However, it can print to the serial line (Serial.print() works). I did the loop back test as documented on the forum, and the Mega echoes things fine. Anyone have a clue as to what's going on?

Here's the sketch example (serialEvent) that I was using to test.

/*
Serial Event example

When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.

A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.

Created 9 May 2011
by Tom Igoe

This example code is in the public domain.

*/

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

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

void loop() {
Serial.println("B"); //I can see this
if (stringComplete) {
Serial.println(inputString);
inputString = "";
stringComplete = false;
}
}

void serialEvent() {
Serial.println("A"); //I can't see this
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}

Thanks guys :slight_smile: EDIT: I have a Mac, if that might have something to do with it. I just found out about the "shift+upload" and got this error: avrdude: usbdev_open(): did not find any USB device "usb" Also, I'm using Arduino 1.01.

I copied the code, commented out the Serial.println("B") in the loop, and it ran fine. I get the correct number of 'A', and if I select "Both NL and CR" at the bottom next to the baud rate, I see the string I sent following that.

notanaverageday:

    if (inChar == '\n') {

stringComplete = true;
   }

This shows that you need to append a newline. The serial monitor window, by default, does not. So make sure you change the drop-down on the lower-right corner to "Newline". Otherwise, stringComplete will never be true and

notanaverageday:
I have a Mac, if that might have something to do with it.

That has nothing to do with your issue.

notanaverageday:
I just found out about the "shift+upload" and got this error:

Shift-Upload doesn't work with 1.0 anymore. Verbose output is enabled through preferences (cmd-,)

Thank you! Silly mistake. Works fine now. :slight_smile: