Hi, I nearly can't believe it.
Is there no simple command to read data from serial input ?
Example: The user types 123 and terminates the entry by pressing ENTER or CR.
The routine waits until ENTER or CR is pressed.
Result: A variable (byte, int, etc.) filled with the value 123.
Other languages can do that with a simple command like :
"Input "Enter Value: )" , var"
This prints "Enter Value: " and waits for the input, to be terminated as said above.
With the C-Type Ardunio language - Does one really has to handle the input buffers, collect the char(s), make them into a string, find the terminating char, remove it from the string, use .toint() to change it into byte, int, etc. ? Can't believe it.
Hoping to have missed some really nice command(s) Anyone with suggestions ? Thanks !
boylesg:
Only blocks if there is something to read from Serial.
Which is a pretty long time for the Arduino, especially if some time passes between digit's (over which you have no control) Like I said, might get away with it but it is wasteful and blocking.
Thanks so far for the replies.
"Blocking" would be welcome !
All time critical tasks are handled by interrupts anyway (should never be in the main loop anyway).
What's wrong with
[i]int x = 0;
if (Serial.available())
{
x = Serial.parseInt();
}[/i]
?
Well that's not even half of what my Input-example does. And, let's say, there a 3 values to be entered, let's call the X, Y and Z
With my Input-Example I would write
Input "Enter X", x
Input "Enter Y", y
Input "Enter Z", z
And the program would wait nicely until x, y and z are entered.
How would that work with
if (Serial.available())
{
x = Serial.parseInt();
}
if (Serial.available())
{
y = Serial.parseInt();
}
if (Serial.available())
{
z = Serial.parseInt();
}
I wonder ?
As the routines don't wait until all numbers for X have been entered, it just goes through the loop all the time.
And, yes, I know about the "Serial Input Basics". That's why I said I can't believe that it is so fiddly and awkward to do a simple job like reading one or more bytes with this C-type language. Reminds me a bit of the "good old times" when I used punch-cards with IBM 029
Well, as there seems to be no "real" command to do this and even creating a special library for a simple input-routine has been suggested, could someone please suggest how I could implement
the above described input-function:
... do something
Input var x and wait for CR or LF
... do something
Input var x and wait for CR or LF
... do something
Input more vars
Thanks very much for the kind assistance, guess I'm spoiled by other languages
I'm really stuck with this problem.
Still can't believe that there is no command for such an trivial and ordinary task. Does no one
enter any values ?! Can't be all hardcoded ?!
Setting an IP-Adress for example. Okay, that would happen in one place of the program and not in separate sections and one might tinker with this serial parse command (is that blocking?)
Greece2001:
Setting an IP-Adress for example. Okay, that would happen in one place of the program and not in separate sections and one might tinker with this serial parse command (is that blocking?)
It's not clear to me what you're asking. By "this serial parse command", do you mean parseInt()? If so, why not just write that? It's already been said several times earlier in the thread that function is blocking so I guess you're asking something else, but I don't know what.
But look what I found : https://arduino-projekte.webnode.at/meine-libraries/serieller-monitor/
One can select the expected type of var (byte, Int16, float, etc.) and choose
between blocking (great! That's what I needed!) and non-blocking.
Problem solved and maybe not only for me (no need to write a lib, there is this one ...)
But look what I found : https://arduino-projekte.webnode.at/meine-libraries/serieller-monitor/
One can select the expected type of var (byte, Int16, float, etc.) and choose
between blocking (great! That's what I needed!) and non-blocking.
Problem solved and maybe not only for me (no need to write a lib, there is this one ...)
Thanks for all your comments !
the library does nothing else then
int x = 0;
if (Serial.available())
{
x = Serial.parseInt();
}
you can use parseFloat(), readBytesUntil(), readBytes()
or
you could read a whole line into a buffer and then use sscanf or parse the buffer with strtok