Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: October 08, 2012, 01:29:54 am » |
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. 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
Online
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: October 08, 2012, 01:32:06 am » |
Are the numbers binary, or ASCII decimal?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
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)
|
 |
« Reply #2 on: October 08, 2012, 01:35:02 am » |
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
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #3 on: October 08, 2012, 01:47:48 am » |
Are the numbers binary, or ASCII decimal?
ASCII, I am actually receiving data from another arduino. On the other arduino, I only use: 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
Online
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: October 08, 2012, 01:53:49 am » |
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.
|
|
|
|
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)
|
 |
« Reply #5 on: October 08, 2012, 01:56:20 am » |
I suggest you try this on the receiving end: 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
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #6 on: October 08, 2012, 02:01:10 am » |
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*)' 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
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #7 on: October 08, 2012, 02:05:44 am » |
I suggest you try this on the receiving end: 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
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #8 on: October 08, 2012, 02:09:57 am » |
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
Online
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: October 08, 2012, 02:10:28 am » |
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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #10 on: October 08, 2012, 02:16:56 am » |
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): -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: 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
Online
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: October 08, 2012, 02:18:52 am » |
2D 32 D A AKA "-2\r\n" UsefulDitto
|
|
|
|
« 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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #12 on: October 08, 2012, 02:35:16 am » |
Thanks guy, it's working now. I should really scan the forum first...haha 
|
|
|
|
|
Logged
|
|
|
|
|
|