Problem using Serial Monitor with ATmega32U4

I'm using the Serial Monitor as a way of pasting programs to a language interpreter (uLisp).

On boards like the Arduino Micro based on the ATmega32U4 the serial hangs up if I try to paste a block of text with more than a small number of characters (about 384). I can reproduce this effect using a simple test program:

void setup() {
  Serial.begin(9600);
}

int count = 0;

void loop() {
  if (count%64 == 0) {
    Serial.println();
    Serial.print(count);
    Serial.write(' ');
  }
  while (!Serial.available());
  Serial.write(Serial.read());
  count++;
}

This doesn't happen on other boards, such as the Arduino Uno or Arduino Mega 2560, so it isn't simply that I'm filling the serial buffer.

Any suggestions?

In setup, add while (!Serial); after Serial.begin();

That might do the trick.

Thanks for the suggestion, but unfortunately that makes no difference.

OK, in that case you will have to be a little clearer in your description.

I have tested your code (with my modification) on a Leonardo and can not reproduce the problem. All characters (I've tested sending up to roughly 2500 characters) correctly display.

Just in case: Be aware that your while(!Serial.available()); will block the code if no data is available but that should be the case on e.g. an Uno as well.

void setup()
{
  Serial.begin(250000);

  while (!Serial);
  Serial.println("Ready");
}

int count = 0;

void loop()
{
  if (count % 64 == 0)
  {
    Serial.println();
    Serial.print(count);
    Serial.write(' ');
  }
  while (!Serial.available());
  Serial.write(Serial.read());
  count++;
}

I've tried your exact code on several Arduino boards and am still getting the same problem.

My setup is: Arduino IDE 1.8.2 on a MacBook Pro running Mac OS X 10.11.6.

For sample text I am pasting in the copyright message from the start of an Arduino core source file.

I don't have a Leonardo, but with an Arduino Micro or a LilyPad Arduino USB (both ATmega32U4-based) I get:

With an Arduino Uno or Arduino Nano I get the full text appearing, as expected.

I hope someone else can reproduce this problem; thanks!

Did you fix the problem?

I just had a similar issue with a Micro (ATmega32U4) and reading your post gave me an idea on trying smaller text, it worked, then I thought it could be a buffer issue, so I created a small loop and printed from 1 to 100 character long on one single line, one Serial.println(), and to my surprise, they all went through, so I got back to my original Sketch and now it works...

That's all I can share since my problem is gone. No matter how many times I plug it in and load the Sketch, it always works now and I can not reproduce the problem anymore...

I hope it helps in any way...

From discussing this problem on another forum I believe it may only affect the Arduino IDE on the Mac. I can't test this because I don't have a PC.

Please could someone with a Mac confirm they can reproduce my problem.

I have also confirmed that I get the same problem with the Arduino MKRZero, using the same test program (see my first post in this topic), except there the limit is 1024 characters.

The Arduino Zero (Programming Port) doesn't have the problem.

johnsondavies:
if I try to paste a block of text with more than a small number of characters (about 384). I can reproduce this effect using a simple test program:

I am assuming you are pasting data into the serial monitor to send it to the Arduino?

The Serial Input Buffer is 64 bytes long so 384 characters is a LOT of data.

My wild quess is that you are overwhelming the 32u4 because it sends data at the full USB speed and ignores the baud rate - so the effective baud rate may be well over 1,000,000.

Your code would need to clear the serial input buffer very smartly to keep up with that.

And, as you seem to be resending the received characters you are probably choking the serial output buffer also.

Try sending less than 64 bytes at a time and see does the problem go away?

And to identify if it is the re-sending that is causing the problem try receiving the full message before sending it back

...R
Serial Input Basics

@johnsondavies
Have you tried another terminal program instead of Serial Monitor. No idea what is available for Mac (maybe minicom is already installed).

It might be the IDE but can also be the driver.

@Robin2
Tested with a Leonardo on a PC and it has no problems at 250000 or 9600 baud. One line of 2500 characters; no CR/LF. Output below with code from reply #3. There should be 10 As, 10 Bs etc if one removes the word ready (and yes, I did count them :wink: ).

Ready

0 A234567890A234567890A234567890A234567890A234567890A234567890A234
64 567890A234567890A234567890A234567890B234567890B234567890B2345678
128 90B234567890B234567890B234567890B234567890B234567890B234567890B2
192 34567890C234567890C234567890C234567890C234567890C234567890C23456
256 7890C234567890C234567890C234567890C234567890D234567890D234567890
320 D234567890D234567890D234567890D234567890D234567890D234567890D234
384 567890D234567890E234567890E234567890E234567890E234567890E2345678
448 90E234567890E234567890E234567890E234567890E234567890F234567890F2
512 34567890F234567890F234567890F234567890F234567890F234567890F23456
576 7890F234567890F234567890G234567890G234567890G234567890G234567890
640 G234567890G234567890G234567890G234567890G234567890G234567890H234
704 567890H234567890H234567890H234567890H234567890H234567890H2345678
768 90H234567890H234567890H234567890I234567890I234567890I234567890I2
832 34567890I234567890I234567890I234567890I234567890I234567890I23456
896 7890J234567890J234567890J234567890J234567890J234567890J234567890
960 J234567890J234567890J234567890J234567890K234567890K234567890K234
1024 567890K234567890K234567890K234567890K234567890K234567890K2345678
1088 90K234567890L234567890L234567890L234567890L234567890L234567890L2
1152 34567890L234567890L234567890L234567890L234567890M234567890M23456
1216 7890M234567890M234567890M234567890M234567890M234567890M234567890
1280 M234567890M234567890N234567890N234567890N234567890N234567890N234
1344 567890N234567890N234567890N234567890N234567890N234567890O2345678
1408 90O234567890O234567890O234567890O234567890O234567890O234567890O2
1472 34567890O234567890O234567890P234567890P234567890P234567890P23456
1536 7890P234567890P234567890P234567890P234567890P234567890P234567890
1600 Q234567890Q234567890Q234567890Q234567890Q234567890Q234567890Q234
1664 567890Q234567890Q234567890Q234567890R234567890R234567890R2345678
1728 90R234567890R234567890R234567890R234567890R234567890R234567890R2
1792 34567890S234567890S234567890S234567890S234567890S234567890S23456
1856 7890S234567890S234567890S234567890S234567890T234567890T234567890
1920 T234567890T234567890T234567890T234567890T234567890T234567890T234
1984 567890T234567890U234567890U234567890U234567890U234567890U2345678
2048 90U234567890U234567890U234567890U234567890U234567890V234567890V2
2112 34567890V234567890V234567890V234567890V234567890V234567890V23456
2176 7890V234567890V234567890W234567890W234567890W234567890W234567890
2240 W234567890W234567890W234567890W234567890W234567890W234567890X234
2304 567890X234567890X234567890X234567890X234567890X234567890X2345678
2368 90X234567890X234567890X234567890Y234567890Y234567890Y234567890Y2
2432 34567890Y234567890Y234567890Y234567890Y234567890Y234567890Y23456
2496 7890

sterretje:
@Robin2
Tested with a Leonardo on a PC and it has no problems at 250000 or 9600 baud

Interesting, and thanks for taking the trouble to test it. The baud rate is irrelevant with a Leonardo.

...R

I'm more or less aware of that. From previous experiences, I thought that the baud rates did not have have to match, but that did not work at the first attempt this time :wink:

sterretje:
From previous experiences, I thought that the baud rates did not have have to match, but that did not work at the first attempt this time :wink:

Sorry. I did not mean to imply that they did not have to match - I have never tested that. But my experience is that it runs at the same (high) speed regardless of the setting. And, almost certainly, I was doing my testing with a Python program rather than the Serial monitor.

...R

You did not imply that :slight_smile: I did some testing in the past and thought that they did not have to match; this time however it did not work with different baudrates, so I must have been mistaken.

The same issue in github: