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().
This commit is contained in:
David A. Mellis 2011-08-17 14:16:47 -04:00
parent 31ff05504f
commit 8dd6a40334
2 changed files with 12 additions and 29 deletions

View File

@ -23,7 +23,7 @@
#include "Arduino.h" #include "Arduino.h"
#include "Stream.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 #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
// private method to read stream with timeout // private method to read stream with timeout
@ -31,7 +31,7 @@ int Stream::timedRead()
{ {
//Serial.println(_timeout); //Serial.println(_timeout);
this->_startMillis = millis(); this->_startMillis = millis();
while(millis() - this->_startMillis < (this->_timeout * 1000L)) while(millis() - this->_startMillis < this->_timeout)
{ {
if (this->available() > 0) { if (this->available() > 0) {
return this->read(); return this->read();
@ -58,7 +58,7 @@ return c;
// Public Methods // 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; this->_timeout = timeout;
} }
@ -200,17 +200,17 @@ float Stream::parseFloat(char skipChar){
// read characters from stream into buffer // read characters from stream into buffer
// terminates if length characters have been read, null is detected or timeout (see setTimeout) // 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) // 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 // 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) // 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; int index = 0;
*buffer = 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 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
}

View File

@ -38,7 +38,7 @@ readBytesBetween( pre_string, terminator, buffer, length)
class Stream : public Print class Stream : public Print
{ {
private: 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 long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout int timedRead(); // private method to read stream with timeout
int getNextDigit(); // returns the next numeric digit in the stream or -1 if 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 int peek() = 0;
virtual void flush() = 0; virtual void flush() = 0;
Stream() {_timeout=5;} Stream() {_timeout=1000;}
// parsing methods // 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 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) // 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 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) // 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) // 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 // 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) // 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 // Arduino String functions to be added here
}; };