Skip to content

Fix for issues #846 and #847 #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions hardware/arduino/cores/arduino/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi
_timeout = timeout;
}

// find returns true if the target character is found
bool Stream::find(char target)
{
int c = 0;
do {
c = timedRead();
if ((char)c == target)
return true;
} while (c >= 0);
// if c is -1, it means a timeout occured
return false;
}

// find returns true if the target string is found
bool Stream::find(char *target)
{
Expand Down
7 changes: 5 additions & 2 deletions hardware/arduino/cores/arduino/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class Stream : public Print
private:
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
unsigned long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout
int timedPeek(); // private method to peek stream with timeout
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout

public:
Expand All @@ -56,6 +54,9 @@ class Stream : public Print

void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second

bool find(char target); // reads data from the stream until the target character is found
// returns true if target character is found, false if timed out (see setTimeout)

bool find(char *target); // reads data from the stream until the target string is found
// returns true if target string is found, false if timed out (see setTimeout)

Expand Down Expand Up @@ -84,6 +85,8 @@ class Stream : public Print
// Arduino String functions to be added here

protected:
int timedRead(); // private method to read stream with timeout
int timedPeek(); // private method to peek stream with timeout
long parseInt(char skipChar); // as above but the given skipChar is ignored
// as above but the given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
Expand Down