CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

rs232 problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
yahyooh



Joined: 22 Aug 2008
Posts: 12

View user's profile Send private message

rs232 problem
PostPosted: Thu Feb 17, 2011 8:26 am     Reply with quote

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" ? Embarassed Laughing Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19337

View user's profile Send private message

PostPosted: Thu Feb 17, 2011 8:58 am     Reply with quote

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

View user's profile Send private message

thanx alot
PostPosted: Fri Feb 18, 2011 2:26 am     Reply with quote

Thank you Ttelmah I really enjoyed your smart code and I will use it in my project.
Laughing Cool Very Happy
Best Wishes
yahyooh



Joined: 22 Aug 2008
Posts: 12

View user's profile Send private message

which library
PostPosted: Fri Feb 18, 2011 3:35 am     Reply with quote

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: 19337

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 5:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 12:32 pm     Reply with quote

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 Wink Wink
Ttelmah



Joined: 11 Mar 2010
Posts: 19337

View user's profile Send private message

PostPosted: Sat Feb 19, 2011 5:39 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group