I am using the bellow test code to verify Serial parseInt function and having the following issues:
- parseInt will skip leading zeroes in Serial buffer - example if buffer = TEST0012300 than
lcd_i2c.print((int) Serial.parseInt());
will print "12300"
-
paresInt can skip a character - per doc. But it suppose to parse ( read / output) only int(s) so in theory it should skip ALL other characters anyway - per Stream class parseInt function definition which defaults to SKIP_ALL
-
After adding "skip character" as a parameter , the compiler outputs this message
In file included from C:\Documents and Settings\Vaclav\Local Settings\Application Data\Arduino15\packages\arduino\hardware\sam\1.6.4\libraries\Wire/Wire.h:27:0,
from PAL_15_NEW_START.ino:40:
C:\Documents and Settings\Vaclav\Local Settings\Application Data\Arduino15\packages\arduino\hardware\sam\1.6.4\cores\arduino/Stream.h: In function 'int main()':
C:\Documents and Settings\Vaclav\Local Settings\Application Data\Arduino15\packages\arduino\hardware\sam\1.6.4\cores\arduino/Stream.h:95:8: error: 'long int Stream::parseInt(char)' is protected
long parseInt(char skipChar); // as above but the given skipChar is ignored
^
In file included from PAL_15_NEW_START.ino:124:0:
C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build792391424716958793.tmp\sketch\AAA_Main.h:135:46: error: within this context
lcd_i2c.print((int) Serial.parseInt('c'));
^
Error compiling.
When including the "skip" parameter the parseInt fails because is now (?) protected.
Compiler reports error, which is "ignored" , but the code won't compile.
My conclusion - use parseInt with care.
char SerialBuffer[256];
do
{
// test serial int
while (!Serial.available());
while (Serial.available())
{
lcd_i2c.clear();
lcd_i2c.print(__func__);
lcd_i2c.setCursor(0, 1);
lcd_i2c.print((int) Serial.parseInt("1")); // fails compilation
lcd_i2c.print((int) Serial.parseInt()); // skips leading zeroes in serial buffer
for (;;);
//while (lcd_i2c.print((int) Serial.parseInt() != 0 ));
// lcd_i2c.setCursor(0, 2);
// return number of characters in SerialBuffer
lcd_i2c.print(Serial.readBytes(SerialBuffer, 3));
// lcd_i2c.setCursor(0, 3);
// lcd_i2c.print(Serial.print(SerialBuffer[0]));
// print buffer
lcd_i2c.setCursor(0, 3);
for (int i = 0; i != 3; i++)
lcd_i2c.print(SerialBuffer[i]);
// //for (;;);
lcd_i2c.setCursor(16, 3);
lcd_i2c.print(__LINE__);
delay(DELAY);
delay(5000);
lcd_i2c.clear();
}
} while (true);
for (;;);