Don't consume trailing char in parseInt() and parseFloat (Paul Stoffregen).

http://code.google.com/p/arduino/issues/detail?id=624
This commit is contained in:
David A. Mellis 2011-09-09 16:24:47 -04:00
parent 3414012dc8
commit f3d8628c5e
3 changed files with 43 additions and 41 deletions

View File

@ -29,30 +29,39 @@
// private method to read stream with timeout
int Stream::timedRead()
{
//Serial.println(_timeout);
this->_startMillis = millis();
while(millis() - this->_startMillis < this->_timeout)
{
if (this->available() > 0) {
return this->read();
}
}
int c;
_startMillis = millis();
do {
c = read();
if (c >= 0) return c;
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
}
// returns the next digit in the stream or -1 if timeout
// discards non-numeric characters
int Stream::getNextDigit()
// private method to peek stream with timeout
int Stream::timedPeek()
{
int c;
do{
c = timedRead();
if( c < 0)
return c; // timeout
}
while( c != '-' && (c < '0' || c > '9') ) ;
_startMillis = millis();
do {
c = peek();
if (c >= 0) return c;
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
}
return c;
// returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters
int Stream::peekNextDigit()
{
int c;
while (1) {
c = timedPeek();
if (c < 0) return c; // timeout
if (c == '-') return c;
if (c >= '0' && c <= '9') return c;
read(); // discard non-numeric
}
}
// Public Methods
@ -130,7 +139,7 @@ long Stream::parseInt(char skipChar)
long value = 0;
int c;
c = getNextDigit();
c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
@ -142,9 +151,10 @@ long Stream::parseInt(char skipChar)
isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit?
value = value * 10 + c - '0';
c = timedRead();
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == skipChar );
while( (c >= '0' && c <= '9') || c == skipChar );
if(isNegative)
value = -value;
@ -168,7 +178,7 @@ float Stream::parseFloat(char skipChar){
char c;
float fraction = 1.0;
c = getNextDigit();
c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
@ -185,7 +195,8 @@ float Stream::parseFloat(char skipChar){
if(isFraction)
fraction *= 0.1;
}
c = timedRead();
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );

View File

@ -41,7 +41,8 @@ class Stream : public Print
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
int timedPeek(); // private method to peek stream with timeout
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
public:
virtual int available() = 0;

View File

@ -6,28 +6,19 @@ The boards can be assembled by hand or purchased preassembled; the open-source
IDE can be downloaded for free.
For more information, see the website at: http://www.arduino.cc/
or the forums at: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl
or the forums at: http://arduino.cc/forum/
To report a bug or a make a suggestions, go to:
[hardware] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=hwbugs
[software] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs
To report a bug in the software, go to:
http://code.google.com/p/arduino/issues/list
For other suggestions, use the forum:
http://arduino.cc/forum/index.php/board,21.0.html
INSTALLATION
Detailed instructions are in reference/Guide_Windows.html and
reference/Guide_MacOSX.html. For Linux, see the Arduino playground:
http://www.arduino.cc/playground/Learning/Linux
If you are using a USB Arduino, you will need to install the drivers for the
FTDI chip on the board. These can be found in the drivers/ directory.
* On Windows, plug in the Arduino board and point the Windows Add Hardware
wizard to the drivers/FTDI USB Drivers sub-directory of the Arduino
application directory.
* On the Mac, install the FTDIUSBSerialDriver_10_4_10_5_10_6.mpkg package.
* On Linux, drivers are included in kernel versions 2.4.20 or greater.
CREDITS
Arduino is an open source project, supported by many.
@ -37,6 +28,5 @@ Gianluca Martino, and David A. Mellis.
Arduino uses the GNU avr-gcc toolchain, avrdude, avr-libc, and code from
Processing and Wiring.
Icon designed by ToDo: http://www.todo.to.it/
"About" image created by Thomas Glaser (envis precisely).
Icon and about image designed by ToDo: http://www.todo.to.it/