mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-19 13:54:23 +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:
parent
4119b9089b
commit
ed48d17e20
@ -208,11 +208,20 @@ 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::readBytes( char *buffer, size_t length)
|
||||
// terminates if length characters have been read, or timeout (see setTimeout)
|
||||
// returns the number of characters placed in the buffer
|
||||
// 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
|
||||
// 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;
|
||||
*buffer = 0;
|
||||
while(index < length-1 ){
|
||||
int c = timedRead();
|
||||
if( c <= 0 ){
|
||||
return 0; // timeout returns 0 !
|
||||
}
|
||||
else if( c == terminator){
|
||||
buffer[index] = 0; // terminate the string
|
||||
return index; // data got successfully
|
||||
}
|
||||
else{
|
||||
buffer[index++] = (char)c;
|
||||
}
|
||||
}
|
||||
buffer[index] = 0;
|
||||
return index; // here if buffer is full before detecting the terminator
|
||||
if (length < 1) return 0;
|
||||
size_t index = 0;
|
||||
while (index < length) {
|
||||
int c = timedRead();
|
||||
if (c < 0 || c == terminator) break;
|
||||
*buffer++ = (char)c;
|
||||
index++;
|
||||
}
|
||||
return index; // return number of characters, not including null terminator
|
||||
}
|
||||
|
||||
|
@ -73,11 +73,11 @@ class Stream : public Print
|
||||
|
||||
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)
|
||||
// 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
|
||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user