arduino / arduino/ArduinoCore-avr
[Serial] Make operator bool() meaningful
- Dominant language
- C
- Stars
- 1.5k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
Currently, if(Serial) always returns true which is specified in the documentation (https://www.arduino.cc/en/Serial/IfSerial) but contradicts the purpose considering that on 32u4 based boards with USB serial it's used to check if the serial port is ready. This breaks interchangability between the platforms.
For the SoftwareSerial library it would be an easy change when changing the code from
`operator bool() { return true; }`
to
`operator bool() { return isListening(); }`
For HardwareSerial, it's not so easy but maybe even more important, because it crashes when accessing read and print before initializing the library with begin(). I'm not sure what would be the best way to implement it or what's the Arduino style of handling this, so I didn't include a pull request.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by comparing the SoftwareSerial and HardwareSerial operator bool() implementations described in the issue, then review the linked Arduino Serial documentation. Determine how readiness should behave across platforms and before begin() is called. Done means if(Serial) consistently reflects whether the serial port is ready without causing read or print access to crash.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- embedded-iot
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100