Skip to content

[Serial] Make operator bool() meaningful #78

Open
@crasbe

Description

@crasbe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions