Go Down

Topic: Convert serial.read() to usable int (Read 5032 times) previous topic - next topic

alanl852

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: [Select]
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);
}

AWOL

Are the numbers binary, or ASCII decimal?
"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.

tuxduino

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.

alanl852


Are the numbers binary, or ASCII decimal?


ASCII, I am actually receiving data from another arduino.

On the other arduino, I only use:
Code: [Select]
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...

AWOL

Have you thought about using "atoi"?
(Hint: You're not the first person to ask this question, just the first this week)
"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.

tuxduino

I suggest you try this on the receiving end:

Code: [Select]

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...
       

alanl852


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: [Select]
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);
}
}

alanl852


I suggest you try this on the receiving end:

Code: [Select]

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...

tuxduino

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

AWOL

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.
"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.

alanl852


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: [Select]

-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: [Select]

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...

AWOL

#11
Oct 08, 2012, 09:18 am Last Edit: Oct 08, 2012, 09:24 am by AWOL Reason: 1
Quote
2D
32
D
A

AKA  "-2\r\n"

Useful
Ditto
"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.

alanl852

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

Go Up