Pages: [1]   Go Down
Author Topic: Arduino reading strings or integers  (Read 379 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When writing through a serial connection to arduino from any programming (processing etc.), arduino reads its values as bytes correct?  On the programming side, if I write the number one as a string, what is that in terms of the byte value verses its integer value in arduino?  What does arduino read strings as?  Say if I write the value one through the serial connection.

Thanks!

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Say if I write the value one through the serial connection.
Are you writing values in binary or as strings (ASCII data)?

Quote
On the programming side, if I write the number one as a string, what is that in terms of the byte value verses its integer value in arduino?
Serial.read() will return '1' (or 49 or 0x31). If you subract '0' (or 48 or 0x30) from that value, you will get the numeric value of 1.
Logged

Cleveland, TN
Offline Offline
Full Member
***
Karma: 2
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I never see any references to stream.parseInt().

I have intentionally coded my serial data to use only integers so when i send the data, currently over serial monitor but ultimately through some form of RF, I simply look for my SOP, Look at the next character to determne where it is coming from since the serial in will ultimately come from potentially 4 different sources and then use parseInt to grab all of the integer data.

So if I send a data stream of <B,100,100,1,1,2300,400,5,4,3653> to the Arduino, I don't have to do any kind of crazy subtract '0' low byte, high byte, &= translations to get my integers, I just get integers.  Since my received data after that initial letter will always be the same number of items, I could even throw a string or other character data in there and handle it in code. For example,

<B,100,100,1,1,2300,400,5,4,3653,Wade>

Once I saw the < and looked at the first letter 'B', I know exactly what the format of this stream of data is so I can parseInt for 9 steps and then read the character data. Using a  module like the CC1101 from TI handles the error checking issues so I won't have to worry about that potential problem either since I think checksum would be a little more difficult to manage using this method.

Since I can send a 1 or a 0, this obviously works for boolean data as well.

So PaulS, is there something I should know that would steer me away from this or do you avoid it because it limits the data input to int's? I just find it so much easier to use. I ask because I have recently seen two of your posts where you reference the exact same code chunk to read serial and I learned a lot from it on how to start and end the read etc but for now am sticking to the parseInt method.
Logged

Luck,

Wade

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Stream::parseInt() function is new in 1.0. My code works for any version of the IDE.
Logged

Cleveland, TN
Offline Offline
Full Member
***
Karma: 2
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah!

Since I am new this is the only version I know.

I was starting to worry that it might not be the best method.
Logged

Luck,

Wade

Pages: [1]   Go Up
Jump to: