Skip to content

Commit f3d8628

Browse files
committed
Don't consume trailing char in parseInt() and parseFloat (Paul Stoffregen).
http://code.google.com/p/arduino/issues/detail?id=624
1 parent 3414012 commit f3d8628

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

hardware/arduino/cores/arduino/Stream.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,39 @@
2929
// private method to read stream with timeout
3030
int Stream::timedRead()
3131
{
32-
//Serial.println(_timeout);
33-
this->_startMillis = millis();
34-
while(millis() - this->_startMillis < this->_timeout)
35-
{
36-
if (this->available() > 0) {
37-
return this->read();
38-
}
39-
}
32+
int c;
33+
_startMillis = millis();
34+
do {
35+
c = read();
36+
if (c >= 0) return c;
37+
} while(millis() - _startMillis < _timeout);
38+
return -1; // -1 indicates timeout
39+
}
40+
41+
// private method to peek stream with timeout
42+
int Stream::timedPeek()
43+
{
44+
int c;
45+
_startMillis = millis();
46+
do {
47+
c = peek();
48+
if (c >= 0) return c;
49+
} while(millis() - _startMillis < _timeout);
4050
return -1; // -1 indicates timeout
4151
}
4252

43-
// returns the next digit in the stream or -1 if timeout
53+
// returns peek of the next digit in the stream or -1 if timeout
4454
// discards non-numeric characters
45-
int Stream::getNextDigit()
55+
int Stream::peekNextDigit()
4656
{
4757
int c;
48-
do{
49-
c = timedRead();
50-
if( c < 0)
51-
return c; // timeout
58+
while (1) {
59+
c = timedPeek();
60+
if (c < 0) return c; // timeout
61+
if (c == '-') return c;
62+
if (c >= '0' && c <= '9') return c;
63+
read(); // discard non-numeric
5264
}
53-
while( c != '-' && (c < '0' || c > '9') ) ;
54-
55-
return c;
5665
}
5766

5867
// Public Methods
@@ -130,7 +139,7 @@ long Stream::parseInt(char skipChar)
130139
long value = 0;
131140
int c;
132141

133-
c = getNextDigit();
142+
c = peekNextDigit();
134143
// ignore non numeric leading characters
135144
if(c < 0)
136145
return 0; // zero returned if timeout
@@ -142,9 +151,10 @@ long Stream::parseInt(char skipChar)
142151
isNegative = true;
143152
else if(c >= '0' && c <= '9') // is c a digit?
144153
value = value * 10 + c - '0';
145-
c = timedRead();
154+
read(); // consume the character we got with peek
155+
c = timedPeek();
146156
}
147-
while( (c >= '0' && c <= '9') || c == skipChar );
157+
while( (c >= '0' && c <= '9') || c == skipChar );
148158

149159
if(isNegative)
150160
value = -value;
@@ -168,7 +178,7 @@ float Stream::parseFloat(char skipChar){
168178
char c;
169179
float fraction = 1.0;
170180

171-
c = getNextDigit();
181+
c = peekNextDigit();
172182
// ignore non numeric leading characters
173183
if(c < 0)
174184
return 0; // zero returned if timeout
@@ -185,7 +195,8 @@ float Stream::parseFloat(char skipChar){
185195
if(isFraction)
186196
fraction *= 0.1;
187197
}
188-
c = timedRead();
198+
read(); // consume the character we got with peek
199+
c = timedPeek();
189200
}
190201
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
191202

hardware/arduino/cores/arduino/Stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Stream : public Print
4141
long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4242
long _startMillis; // used for timeout measurement
4343
int timedRead(); // private method to read stream with timeout
44-
int getNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
44+
int timedPeek(); // private method to peek stream with timeout
45+
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
4546

4647
public:
4748
virtual int available() = 0;

readme.txt

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,19 @@ The boards can be assembled by hand or purchased preassembled; the open-source
66
IDE can be downloaded for free.
77

88
For more information, see the website at: http://www.arduino.cc/
9-
or the forums at: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl
9+
or the forums at: http://arduino.cc/forum/
1010

11-
To report a bug or a make a suggestions, go to:
12-
[hardware] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=hwbugs
13-
[software] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=swbugs
11+
To report a bug in the software, go to:
12+
http://code.google.com/p/arduino/issues/list
13+
14+
For other suggestions, use the forum:
15+
http://arduino.cc/forum/index.php/board,21.0.html
1416

1517
INSTALLATION
1618
Detailed instructions are in reference/Guide_Windows.html and
1719
reference/Guide_MacOSX.html. For Linux, see the Arduino playground:
1820
http://www.arduino.cc/playground/Learning/Linux
1921

20-
If you are using a USB Arduino, you will need to install the drivers for the
21-
FTDI chip on the board. These can be found in the drivers/ directory.
22-
23-
* On Windows, plug in the Arduino board and point the Windows Add Hardware
24-
wizard to the drivers/FTDI USB Drivers sub-directory of the Arduino
25-
application directory.
26-
27-
* On the Mac, install the FTDIUSBSerialDriver_10_4_10_5_10_6.mpkg package.
28-
29-
* On Linux, drivers are included in kernel versions 2.4.20 or greater.
30-
3122
CREDITS
3223
Arduino is an open source project, supported by many.
3324

@@ -37,6 +28,5 @@ Gianluca Martino, and David A. Mellis.
3728
Arduino uses the GNU avr-gcc toolchain, avrdude, avr-libc, and code from
3829
Processing and Wiring.
3930

40-
Icon designed by ToDo: http://www.todo.to.it/
41-
"About" image created by Thomas Glaser (envis precisely).
31+
Icon and about image designed by ToDo: http://www.todo.to.it/
4232

0 commit comments

Comments
 (0)