arduino / arduino/ArduinoCore-avr

Serial.available() only returns 0 or 1 when sending multiple single bytes to atmega32u4

Open
#267 11 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

`Serial.available()` never returns anything other than `0` or `1` when sending single bytes to the software serial of an ATmega32U4.

test code:

```cpp
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(Serial.available());
delay(100);
}
```

I can reproduce this on the client by using PuTTY to send individual bytes.

This code:

```cpp
while(Serial.available() < 4);
```

will lockup the program.

This code:

```cpp
while(Serial.available() < 4) {
if (serialEventRun)
serialEventRun();
}
```

will not lockup the program but gets stuck in an infinite loop with single bytes being sent in. If I use the built in serial monitor sending a string of multiple bytes will exit the loop.

Example:

Starting from no data having been sent to the board, `Serial.available()` returns `0`, as expected. I then send one byte, and `Serial.available()` now returns `1`, as expected.

🐛 When I then send an additional byte, `Serial.available()` still returns `1`, even though I would expect it to return `2` under these conditions. Continuing to send more bytes only results in `Serial.available()` continuing to return `1`.

### Additional context

The return value of `Serial.available()` is as expected when using an UNO R3 board.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the behavior with the provided setup/loop code, PuTTY, and an ATmega32U4, then trace the Serial.available() entry point and the serialEventRun path. The fix is complete when successive single-byte sends are counted correctly, while the UNO R3 behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.