From 8dd6a40334613ce1c0c575a93bcd86ee1b109be6 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Wed, 17 Aug 2011 14:16:47 -0400 Subject: 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(). --- cores/arduino/Stream.cpp | 25 +++++++------------------ cores/arduino/Stream.h | 16 +++++----------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/cores/arduino/Stream.cpp b/cores/arduino/Stream.cpp index dea0aff..bf8304f 100644 --- a/cores/arduino/Stream.cpp +++ b/cores/arduino/Stream.cpp @@ -23,7 +23,7 @@ #include "Arduino.h" #include "Stream.h" -#define PARSE_TIMEOUT 5 // default number of seconds to wait +#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field // private method to read stream with timeout @@ -31,7 +31,7 @@ int Stream::timedRead() { //Serial.println(_timeout); this->_startMillis = millis(); - while(millis() - this->_startMillis < (this->_timeout * 1000L)) + while(millis() - this->_startMillis < this->_timeout) { if (this->available() > 0) { return this->read(); @@ -58,7 +58,7 @@ return c; // Public Methods ////////////////////////////////////////////////////////////// -void Stream::setTimeout( long timeout) // sets the maximum number of seconds to wait +void Stream::setTimeout( long timeout) // sets the maximum number of milliseconds to wait { this->_timeout = timeout; } @@ -200,17 +200,17 @@ float Stream::parseFloat(char skipChar){ // read characters from stream into buffer // terminates if length characters have been read, null is detected or timeout (see setTimeout) // returns the number of characters placed in the buffer (0 means no valid data found) -int Stream::readChars( char *buffer, size_t length) +int Stream::readBytes( char *buffer, size_t length) { - return readCharsUntil( 0, buffer, length); + return readBytesUntil( 0, buffer, length); } -// as readChars with terminator character +// as readBytes with terminator character // terminates if length characters have been read, timeout, or if the terminator character detected // returns the number of characters placed in the buffer (0 means no valid data found) -int Stream::readCharsUntil( char terminator, char *buffer, size_t length) +int Stream::readBytesUntil( char terminator, char *buffer, size_t length) { int index = 0; *buffer = 0; @@ -231,14 +231,3 @@ int Stream::readCharsUntil( char terminator, char *buffer, size_t length) return index; // here if buffer is full before detecting the terminator } - -// read characters found between pre_string and terminator into a buffer -// terminated when the terminator character is matched or the buffer is full -// returns the number of bytes placed in the buffer (0 means no valid data found) -int Stream::readCharsBetween( char *pre_string, char terminator, char *buffer, size_t length) -{ - if( find( pre_string) ){ - return readCharsUntil(terminator, buffer, length); - } - return 0; //failed to find the prestring -} diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h index 3f76392..1633f15 100644 --- a/cores/arduino/Stream.h +++ b/cores/arduino/Stream.h @@ -38,7 +38,7 @@ readBytesBetween( pre_string, terminator, buffer, length) class Stream : public Print { private: - long _timeout; // number of seconds to wait for the next char before aborting timed read + long _timeout; // number of milliseconds to wait for the next char before aborting timed read long _startMillis; // used for timeout measurement int timedRead(); // private method to read stream with timeout int getNextDigit(); // returns the next numeric digit in the stream or -1 if timeout @@ -49,11 +49,11 @@ class Stream : public Print virtual int peek() = 0; virtual void flush() = 0; - Stream() {_timeout=5;} + Stream() {_timeout=1000;} // parsing methods - void setTimeout(long timeout); // sets maximum seconds to wait for stream data, default is 5 seconds + void setTimeout(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 string is found // returns true if target string is found, false if timed out (see setTimeout) @@ -78,20 +78,14 @@ class Stream : public Print float parseFloat(char skipChar); // as above but the given skipChar is ignored - int readChars( char *buffer, size_t length); // read chars from stream into buffer + int readBytes( char *buffer, size_t length); // read chars from stream into buffer // terminates if length characters have been read or timeout (see setTimeout) // returns the number of characters placed in the buffer (0 means no valid data found) - int readCharsUntil( char terminator, char *buffer, size_t length); // as readChars with terminator character + int readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character // terminates if length characters have been read, timeout, or if the terminator character detected // returns the number of characters placed in the buffer (0 means no valid data found) - int readCharsBetween( char *pre_string, char terminator, char *buffer, size_t length); - // read characters found between pre_string and terminator into a buffer - // terminated when the terminator character is matched or the buffer is full - // returns the number of bytes placed in the buffer (0 means no valid data found) - - // Arduino String functions to be added here }; -- cgit v1.2.3-18-g5258