Arduino Mirco Serial communication crashes

Hi guys,

I am experiencing a strange issue while using my arduino micro for com port communication.

First things first:

Basically what i am trying to do is to check if there is a certain amount of bytes available on the com port and if so I want to analyse it.

The problem is... after transmitting 2-3 bytes via the serial monitor console the IDE (1.0.6 and 1.6.5) crashes (while the uno did its job just fine) and by crashing I mean freezing of the whole serial monitor & arduino ide application without any error message.

Here is my testing code:

int temp;
int bytes;
void setup(){

Serial.begin(9600);
//while(!Serial){}
delay(5000);
Serial.println("Setup finished");
}

void loop(){

bytes=Serial.available();
Serial.print("bytes: ");
Serial.println(bytes);
if (bytes >4 ){
temp=Serial.read(); // remove one bytes from the buffer
delay(1);
Serial.println("got data");
}
delay(1000);

}

So i send a character via the serial monitor... the micro plots "bytes: 1" (which is fine). When I type in the second character the micro keeps plotting "bytes: 1" (although it should be 2) and after another charater the freeze occurs.

So I guess there is a problem with the serial buffer on the micro?

I would appreciate any help.

thx in advance

But you read each character, so it is no longer "available"

No, I just read it when there are more than 4 bytes available

Here your sketch produces the probably expected results,
no hangs, no freezes (1.6.5). I sent 7 characters.

Setup finished
bytes: 0
bytes: 0
bytes: 1
bytes: 1
bytes: 2
bytes: 3
bytes: 3
bytes: 4
bytes: 5
got data
bytes: 4
bytes: 5
got data
bytes: 4
bytes: 5
got data
bytes: 4

I can't think of any reason to keep always 4 bytes in the input buffer,
but maybe you can enlighten me.

Thats exactly the response i got from my uno... did you use a micro? Because if your micro doesnt cause any crashes maybe mine is broken?!

And regarding to your question: Its just a test code. I cut down my code to this snippet to find the problem (and to save you from reading my whole code).... later on i will read all 4 bytes and analyse it.

I tested it on a Nano and a Pro Mini, both giving the same result.

I tested on two Micros and a Leonardo and was able to replicate the problem with each.

IDE version 1.6.5, OSX 10.9.5.
I also tested on Linux, same problem so it is not a computer issue.

Works like charm on a Due.

Tested it on a Leonardo, failing, as posted.

The Tx-led of the Leonardo keeps blinking,
so the hang seems to be on the Leonardo-driver side.

IDE and serial monitor unfreeze after board disconnect.

Thank your for your testing efforts. Good to know I am not the only one with this kind of problem.

Since it seems to be an issue I cant fix, I bypassed it by reading and storing every single byte as soon as it arrives. It works for now… but when my code gets bigger and starts slowing my while loop down I guess there will be situations where there are more than 2 bytes in the serial input buffer…

Does it make sence to report it as a bug?

qqq2001:
Does it make sence to report it as a bug?

I think so. :smiley: