1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-21 15:54:39 +01:00

readBytes() and readBytesUntil() handle zero bytes and return # of bytes read.

http://code.google.com/p/arduino/issues/detail?id=586
This commit is contained in:
David A. Mellis 2011-11-19 16:23:19 -05:00
parent 4119b9089b
commit ed48d17e20
2 changed files with 25 additions and 24 deletions

View File

@ -208,11 +208,20 @@ 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, 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
int Stream::readBytes( char *buffer, size_t length) // the buffer is NOT null terminated.
//
size_t Stream::readBytes(char *buffer, size_t length)
{ {
return readBytesUntil( 0, buffer, length); size_t count = 0;
while (count < length) {
int c = timedRead();
if (c < 0) break;
*buffer++ = (char)c;
count++;
}
return count;
} }
@ -220,24 +229,16 @@ int Stream::readBytes( char *buffer, size_t length)
// 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::readBytesUntil( char terminator, char *buffer, size_t length) size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
{ {
unsigned int index = 0; if (length < 1) return 0;
*buffer = 0; size_t index = 0;
while(index < length-1 ){ while (index < length) {
int c = timedRead(); int c = timedRead();
if( c <= 0 ){ if (c < 0 || c == terminator) break;
return 0; // timeout returns 0 ! *buffer++ = (char)c;
} index++;
else if( c == terminator){ }
buffer[index] = 0; // terminate the string return index; // return number of characters, not including null terminator
return index; // data got successfully
}
else{
buffer[index++] = (char)c;
}
}
buffer[index] = 0;
return index; // here if buffer is full before detecting the terminator
} }

View File

@ -73,11 +73,11 @@ class Stream : public Print
float parseFloat(); // float version of parseInt float parseFloat(); // float version of parseInt
int readBytes( char *buffer, size_t length); // read chars from stream into buffer size_t 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 readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character size_t 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)