Arduino Function Serial.read and Serial.readString: Serial monitor of Arduino is a very useful feature.Serial monitor is used to see receive data, send data,print data and so on.Serial monitor is connected to the Arduino through serial communication. This serial communication occurs using RX. Here, I will describe how to read the input from Serial Monitor and show the input. For this, we require Arduino IDE for writing the program. Here, I have my IDE.
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,2550,0,01,20,10090,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 :)
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:
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.
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
Example)
For a myString = '1,2,3'
commaIndex = 1 (Searches from index 0, the spot taken by the character 1, to the location of the first comma)
secondCommaIndex = 3 (Searches from index 2, the spot taken by the character 2, to the location of the next comma)
firstValue reads from index 0-1 = '1'
secondValue reads from index 2-3 = '2'
thirdvalue reads from index 4-4(the last index spot of the string) = '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
Input String Python
@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.