Pages: [1]   Go Down
Author Topic: Convert serial.read() to usable int  (Read 4473 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to receive some numbers that the numbers can be either positive or negative.
I am able to receive the numbers correctly using char but not int.
Since that I want to convert char back to int, but I have no luck with that (only some random numbers and totally differently from the original)...
Any suggestion how to do it? My code is below. Thanks guys.

Code:
char recievedChar;
int value;
void setup()
{
 Serial.begin(115200);
 Serial2.begin(115200);
}
void loop()
{
 if (Serial2.available()){
 recievedChar = Serial2.read();
 value = recievedChar;
 Serial.print(value);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26297
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are the numbers binary, or ASCII decimal?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Char occupies 1 byte. Int requires two bytes. To transmit an int you need at least 2 bytes. More if you convert it to a human-readable string. To receive multibyte strings you need an end-of-string marker, for example \n, or any other char that would be invalid inside the string.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are the numbers binary, or ASCII decimal?

ASCII, I am actually receiving data from another arduino.

On the other arduino, I only use:
Code:
Serial.println(data);
data is in int form which can be either positive or negative numbers.

I am not sure if I am sending the format incorrectly or not but what I send from Arduino A to Arduino B, the received values are matched with the transmitted values.
But I am not able to use the value on Arduino B...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26297
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you thought about using "atoi"?
(Hint: You're not the first person to ask this question, just the first this week)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you try this on the receiving end:

Code:
void loop() {
    if (Serial1.available() > 0) {
        char ch = Serial1.read();
        // show the byte on serial monitor
        Serial.print(ch, HEX);
    }
}

Match what you see on the receiving arduino serial monitor with the int value you're sending from the other one...
       
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you thought about using "atoi"?
(Hint: You're not the first person to ask this question, just the first this week)

I tried before, but no luck....
Arduino says:
error: invalid conversion from 'char' to 'const char*'
error: initializing argument 1 of 'int atoi(const char*)'

Code:
char recievedChar;
int value;
void setup()
{
 Serial.begin(115200);
 Serial2.begin(115200);
}
void loop()
{
 if (Serial2.available()){
 recievedChar = Serial2.read();
 value = atoi(recievedChar);
 Serial.print(value);
 }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you try this on the receiving end:

Code:
void loop() {
    if (Serial1.available() > 0) {
        char ch = Serial1.read();
        // show the byte on serial monitor
        Serial.print(ch, HEX);
    }
}

Match what you see on the receiving arduino serial monitor with the int value you're sending from the other one...
       


I am able to receive the same values, but I am not able to use the values on the receiving end...
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you try my code ? What value(s) did you send ? What did you see on the receiving arduino serial monitor ?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26297
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I tried before, but no luck....
Arduino says:
error: invalid conversion from 'char' to 'const char*'
error: initializing argument 1 of 'int atoi(const char*)'
That's because "atoi" takes a C string as its argument, not a single char.

Seriously, this comes up at least once a week - have a search through the forum.
I think Nick Gammon recently posted a tutorial on the subject.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you try my code ? What value(s) did you send ? What did you see on the receiving arduino serial monitor ?

Just tried.

What I am sending is actually the roll of my IMU (positive/negative values) by using serial.print(roll);

What I am sending using serial.print(roll):
Code:
-9
-7
-5
-3
-3
-4
-7
-10
-2
-4
-10
-2
-3
2
0
-7
-7
-4
-6
-5

When I use your program, the receiving arduino is receiving values like:

Code:
D
A
2D
32
D
A
2D
37
D
A
2D
35
D
A
2D
34
D
A
2D
33
D
A

If I use my original code, I can receive the same values as what I am sending...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26297
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
2D
32
D
A
AKA  "-2\r\n"

Useful
Ditto
« Last Edit: October 08, 2012, 02:24:03 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guy, it's working now. I should really scan the forum first...haha smiley-sweat
Logged

Pages: [1]   Go Up
Jump to: