mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-18 12:54:25 +01:00
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:
parent
682b58e577
commit
cb39ad9739
@ -172,6 +172,13 @@ print KEYWORD2 Serial_Print
|
||||
println KEYWORD2 Serial_Println
|
||||
available KEYWORD2 Serial_Available
|
||||
flush KEYWORD2 Serial_Flush
|
||||
setTimeout KEYWORD2
|
||||
find KEYWORD2
|
||||
findUntil KEYWORD2
|
||||
parseInt KEYWORD2
|
||||
parseFloat KEYWORD2
|
||||
readBytes KEYWORD2
|
||||
readBytesUntil KEYWORD2
|
||||
|
||||
setup KEYWORD3 Setup
|
||||
loop KEYWORD3 Loop
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user