Skip to content

Commit 342be00

Browse files
committed
A few API changes to new Stream parsing functions.
Renamed readChars() -> readBytes(), readCharsUntil() -> readBytesUntil(). Changed timeouts to milliseconds from seconds; default from 5 to 1 seconds. Removed readCharsBetween().
1 parent 4c6a9b6 commit 342be00

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

hardware/arduino/cores/arduino/Stream.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
#include "Arduino.h"
2424
#include "Stream.h"
2525

26-
#define PARSE_TIMEOUT 5 // default number of seconds to wait
26+
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
2727
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
2828

2929
// private method to read stream with timeout
3030
int Stream::timedRead()
3131
{
3232
//Serial.println(_timeout);
3333
this->_startMillis = millis();
34-
while(millis() - this->_startMillis < (this->_timeout * 1000L))
34+
while(millis() - this->_startMillis < this->_timeout)
3535
{
3636
if (this->available() > 0) {
3737
return this->read();
@@ -58,7 +58,7 @@ return c;
5858
// Public Methods
5959
//////////////////////////////////////////////////////////////
6060

61-
void Stream::setTimeout( long timeout) // sets the maximum number of seconds to wait
61+
void Stream::setTimeout( long timeout) // sets the maximum number of milliseconds to wait
6262
{
6363
this->_timeout = timeout;
6464
}
@@ -200,17 +200,17 @@ float Stream::parseFloat(char skipChar){
200200
// read characters from stream into buffer
201201
// terminates if length characters have been read, null is detected or timeout (see setTimeout)
202202
// returns the number of characters placed in the buffer (0 means no valid data found)
203-
int Stream::readChars( char *buffer, size_t length)
203+
int Stream::readBytes( char *buffer, size_t length)
204204
{
205-
return readCharsUntil( 0, buffer, length);
205+
return readBytesUntil( 0, buffer, length);
206206
}
207207

208208

209-
// as readChars with terminator character
209+
// as readBytes with terminator character
210210
// terminates if length characters have been read, timeout, or if the terminator character detected
211211
// returns the number of characters placed in the buffer (0 means no valid data found)
212212

213-
int Stream::readCharsUntil( char terminator, char *buffer, size_t length)
213+
int Stream::readBytesUntil( char terminator, char *buffer, size_t length)
214214
{
215215
int index = 0;
216216
*buffer = 0;
@@ -231,14 +231,3 @@ int Stream::readCharsUntil( char terminator, char *buffer, size_t length)
231231
return index; // here if buffer is full before detecting the terminator
232232
}
233233

234-
235-
// read characters found between pre_string and terminator into a buffer
236-
// terminated when the terminator character is matched or the buffer is full
237-
// returns the number of bytes placed in the buffer (0 means no valid data found)
238-
int Stream::readCharsBetween( char *pre_string, char terminator, char *buffer, size_t length)
239-
{
240-
if( find( pre_string) ){
241-
return readCharsUntil(terminator, buffer, length);
242-
}
243-
return 0; //failed to find the prestring
244-
}

hardware/arduino/cores/arduino/Stream.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ readBytesBetween( pre_string, terminator, buffer, length)
3838
class Stream : public Print
3939
{
4040
private:
41-
long _timeout; // number of seconds to wait for the next char before aborting timed read
41+
long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4242
long _startMillis; // used for timeout measurement
4343
int timedRead(); // private method to read stream with timeout
4444
int getNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
@@ -49,11 +49,11 @@ class Stream : public Print
4949
virtual int peek() = 0;
5050
virtual void flush() = 0;
5151

52-
Stream() {_timeout=5;}
52+
Stream() {_timeout=1000;}
5353

5454
// parsing methods
5555

56-
void setTimeout(long timeout); // sets maximum seconds to wait for stream data, default is 5 seconds
56+
void setTimeout(long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
5757

5858
bool find(char *target); // reads data from the stream until the target string is found
5959
// returns true if target string is found, false if timed out (see setTimeout)
@@ -78,20 +78,14 @@ class Stream : public Print
7878

7979
float parseFloat(char skipChar); // as above but the given skipChar is ignored
8080

81-
int readChars( char *buffer, size_t length); // read chars from stream into buffer
81+
int readBytes( char *buffer, size_t length); // read chars from stream into buffer
8282
// terminates if length characters have been read or timeout (see setTimeout)
8383
// returns the number of characters placed in the buffer (0 means no valid data found)
8484

85-
int readCharsUntil( char terminator, char *buffer, size_t length); // as readChars with terminator character
85+
int readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
8686
// terminates if length characters have been read, timeout, or if the terminator character detected
8787
// returns the number of characters placed in the buffer (0 means no valid data found)
8888

89-
int readCharsBetween( char *pre_string, char terminator, char *buffer, size_t length);
90-
// read characters found between pre_string and terminator into a buffer
91-
// terminated when the terminator character is matched or the buffer is full
92-
// returns the number of bytes placed in the buffer (0 means no valid data found)
93-
94-
9589
// Arduino String functions to be added here
9690

9791
};

0 commit comments

Comments
 (0)