Hi, i'm trying to read a pair "id | value" from the serial monitor.
i.e I type there "2|15"
I read that with parseInt() and works well except because I get an extra 0 at the end of the string.
Why is that and how can I avoid it?
Thanks!
Hi, i'm trying to read a pair "id | value" from the serial monitor.
i.e I type there "2|15"
I read that with parseInt() and works well except because I get an extra 0 at the end of the string.
Why is that and how can I avoid it?
Thanks!
Hi Jay
Can you post your code, so we can take a look at it?
Thanks
Ray
Hi Ray, it' pretty simple
while(Serial.available() > 0)
{
current_char = Serial.parseInt();
Serial.println(current_char);
}
output when typing 2|16 at serial monitor:
2
16
0
Hi Jay
I'm assuming current_char is defined as int?
The 0 is the value returned by parseInt() when it trys to parse a number from the CR and or LF that you are sending after the second valid integer. It waits for a period defined by the serial timeout (default 1s) and then decides that the end of line character(s) are not a number, and returns 0.
If you will be sending the data to the Arduino from the IDE serial monitor, you can set "no line ending" at the bottom right of the window. You may want to reduce the timeout by using Serial.setTimeout() so that the second valid integer is recognised more quickly after you send the string.
Regards
Ray
I'm not sure that using timing to determine when to convert the incoming string to a int is the best way to do this.
Most systems either wait for a space after the text based number or often wait for newline
e.g. if you type a
2
then a
1
how is the program supposed to know that the number is not 21, but you are still typing (slowly)
another 1
then a 5
i.e so that the whole number is 2115
You could equally well be going to type
215000
That is unless you are only ever typing 4 digit numbers
Hi Roger
I wasn't suggesting he use timing to detect end of integer, just explaining why parseInt is giving him a spurious integer due to end of line characters and serial timeout.
If the commands will always be sent from serial monitor, rather than another terminal program, they will be sent as a group when "send" is pressed. Setting no EOL characters avoids the spurious integer.
You are right that a more robust approach is to ditch parseInt and write a parser specifically for the OPs command format.
Regards
Ray
Sorry HackScribble
My misunderstanding
No problem
Hi,
Hackscribble:
I'm assuming current_char is defined as int?
Correct
Hackscribble:
If you will be sending the data to the Arduino from the IDE serial monitor, you can set "no line ending" at the bottom right of the window.
I'm using serial monitor only for tests, the value it's supposed to come from another app.
Hackscribble:
You may want to reduce the timeout by using Serial.setTimeout() so that the second valid integer is recognised more quickly after you send the string.
Tried it with values as low as 200, no changes, going lower makes I don't receive any data.
Thank you.
I'm using serial monitor only for tests, the value it's supposed to come from another app.
OK. Just to confirm, if you set "no line endings" in serial monitor and send "123|456", are you now getting just 123 and 456 and not the extra 0?
Hackscribble:
I'm using serial monitor only for tests, the value it's supposed to come from another app.
OK. Just to confirm, if you set "no line endings" in serial monitor and send "123|456", are you now getting just 123 and 456 and not the extra 0?
That's correct.
So if I send it from an external app as I will not get that extra 0 ?
That's right. If the app sends just that format string with no CR or LF, you will not get a 0.
But the second call to parseInt will take 1 second to return the second number, assuming default serial timeout. That delay may or may not be a problem for you.
Well, I wasn't planning to use Serial.setTimeout(), as there is no a specific frame of time within I should receive the data.
EDIT nvm I think i'm missunderstanding how it works, need to double check it.
Thank you.