Apr 06, 2010 Hi, everyone. I'm writing a small demo program for practice (I'm still quite new to the Arduino). What it does is it lights an LED after a certain number of button presses, and it prints messages to the serial port when the button and/or LED state changes. This part of the Arduino programming course shows how to get data into an Arduino sketch from the serial port. Data can be sent to the Arduino from the Serial Monitor window in the Arduino IDE. A user can enter data in the input field in the serial monitor window to send values and data to the Arduino.
Active2 months ago
Arduino (C language) parsing string with delimiter (input through serial interface)
Didn't find the answer here :/
I want to send to my arduino through a serial interface (Serial.read()) a simple string of three numbers delimited with comma. Those three numbers could be of range 0-255.
Eg.
255,255,255 0,0,0 1,20,100 90,200,3
What I need to do is to parse this string sent to arduino to three integers (let's say r, g and b).
So when I send 100,50,30 arduino will translate it to
I tried lots of codes, but none of them worked. The main problem is to translate string (bunch of chars) to integer. I figured out that there will probably be strtok_r for delimiter purpose, but that's about it.
Thanks for any suggestions :) Detective conan download episodes.
user1461310user1461310
8 Answers
To answer the question you actually asked, String objects are very powerful and they can do exactly what you ask. If you limit your parsing rules directly from the input, your code becomes less flexible, less reusable, and slightly convoluted.
Strings have a method called indexOf() which allows you to search for the index in the String's character array of a particular character. If the character is not found, the method should return -1. A second parameter can be added to the function call to indicate a starting point for the search. In your case, since your delimiters are commas, you would call:
Arduino Serial Input String Number
Then you could use that index to create a substring using the String class's substring() method. This returns a new String beginning at a particular starting index, and ending just before a second index (Or the end of a file if none is given). So you would type something akin to:
Finally, the integer values can be retrieved using the String class's undocumented method, toInt():
More information on the String object and its various methods can be found int the Arduino documentation.
lschlessinger
1,18622 gold badges1616 silver badges4545 bronze badges
dsnettletondsnettleton
Use
sscanf ;
Use my code here if you want to parse a stream of string ex:
255,255,255 0,0,0 1,20,100 90,200,3 Parsing function for comma-delimited string
Community♦
sdaosdao
I think you want to do something like this to read in the data:
Then use atoi() to convert the data to integers and use them.
cstruttoncstrutton
2,49311 gold badge1616 silver badges2828 bronze badges
This is great!
The last comment about 'thirdvalue = 0' is true from the code given in the most upvoted response by @dsnettleton. However, instead of using 'lastIndexOf(',');' , the code should just add a '+1' to 'secondCommaIndex' like @dsnettleton correctly did for commaIndex+1 (missing +1 is probably just a typo from the guy).
Here is the updated piece of code
That’s how your body balances it.Carb Backloading promises you:-. He came to know this secretof keeping the insulin level low in the body in the first half andincreasing it in the second half. How not to remain self deprived of your favorite food. At what time you should eat carbs to build the muscles. Carb backloading pdf free.
Example)
For a myString = '1,2,3'
Note: Don't confuse INDEX with the LENGTH of the string. The length of the string is 5. Since the String indexOf counts starting from 0, the last index is 4.
The reason why just
returns 0 when using .toInt() is because thirdValue = ',3' and not '3' which screws up toInt().
ps. sorry to write all the instructions out but as a mech eng, even I sometimes would like someone to dumb down code for me especially having been in consulting for the past 7 years. Keep up the awesome posting! Helps people like me out a lot!
mctmct
Simplest, I think, is using
parseInt() to do this task:
does the trick.
R ZachR Zach
Milind ChaudharyMilind Chaudhary
@cstrutton -Excellent suggestion on using 'indexOf' . it saved me a ton of time for my project. One minor pointer though,
I noticed the thirdvalue did not get displayed (was coming back as ZERO). Upon playing with it little-bit and going through the doc at http://arduino.cc/en/Tutorial/StringIndexOfI realized, I can use lastIndexOf for the last value.
Here are two lines of modifications that provided correct third value.
karthikr
Arduino Read Serial Input String
75.6k1717 gold badges153153 silver badges158158 bronze badges
user2234414user2234414
fredrickfredrick
Not the answer you're looking for? Browse other questions tagged cstringparsingarduinodelimiter or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |