View previous topic :: View next topic |
Author |
Message |
yahyooh
Joined: 22 Aug 2008 Posts: 12
|
rs232 problem |
Posted: Thu Feb 17, 2011 8:26 am |
|
|
Can anyone tell me how can I receive more than one digit in ascii (I mean rs232) and convert them to int variable.
For example if I want to receive number 12 via rs232 I have to receive the value of "1" and the value of "2", both on ascii. How can I convert them into number "12" ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Feb 17, 2011 8:58 am |
|
|
The numeric value of a single ASCII digit, is:
dig-'0'
So you can use simple code like:
Code: |
char c;
int16 value;
value=0;
while (isdigit(c=getc()) {
value*=10;
value+=c-'0';
}
|
Which will load each received character in turn into 'c', and test if this is a digit. If not, the loop exits. If it is, then it takes the 'sum so far' (value), multiplies it by ten, and adds the value of the new digit.
The result is in 'value'.
Obviously you can get ingenious, and add things like timeout's etc..
Alternatively, just put the characters into a text array, null terminate this when you have the complete 'number' (so when you see a line feed, or space etc.), to turn the array into a 'string', and use 'atol', which converts an ASCII string of numbers into an int16 value.
Best Wishes |
|
|
yahyooh
Joined: 22 Aug 2008 Posts: 12
|
thanx alot |
Posted: Fri Feb 18, 2011 2:26 am |
|
|
Thank you Ttelmah I really enjoyed your smart code and I will use it in my project.
Best Wishes |
|
|
yahyooh
Joined: 22 Aug 2008 Posts: 12
|
which library |
Posted: Fri Feb 18, 2011 3:35 am |
|
|
The function ISDIGIT(), does it need a certain library to be defined
because I tried to compile the code it didn't work.
Thanx alot. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Feb 18, 2011 5:01 am |
|
|
That is what the manual is for....
For every function, it tells you what include files are needed:
"Requires: #INCLUDE <ctype.h>"
Use the manual, it is _essential_.
Best Wishes |
|
|
yahyooh
Joined: 22 Aug 2008 Posts: 12
|
|
Posted: Fri Feb 18, 2011 12:32 pm |
|
|
Hi Ttelmah I faced a new problem this time on how can I display
a float from variables. I tried this code but it didn't work.
Code: |
float distance1,equy;
if(distance1>=6&&distance1<=9){
puts("\n Near");
equy=-1/3*(distance1-6)+1;
printf("%4g",equy);} //the problem is here
|
When I simulate the previous code on Proteus 7.6 it appears 1.0000
No digits after the point. What should I do?
thanx alot
Best Wishes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Feb 19, 2011 5:39 am |
|
|
The code is written to assume the number is complete, on the first 'non digit'. Ideal for inputting integers, but not floats.
Multiple choices:
Use the string input, and look at 'atof'.
Or test for the decimal, and add handling for this:
Code: |
char c;
int32 temp;
int16 divisor;
int1 decimal_flag;
temp=0;
decimal_flag=FALSE;
divisor=1;
while (isamong((c=getc()),"0123556789.")) {
if (c=='.') {
decimal_flag=TRUE;
continue;
}
if (decimal_flag) divisor*=10;
temp*=10;
temp+=c-'0';
}
//Then at this point, the float result, is temp/(float)divisor
//(max 5 decimal digits).
|
Best Wishes |
|
|
|