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 CCS Technical Support

How to receive float number via RS-232 serial link

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



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

How to receive float number via RS-232 serial link
PostPosted: Tue Feb 28, 2017 3:44 am     Reply with quote

Hello,

I'm trying to send and receive float number via RS-232 rf link but I don't
know how to receive it.
My purpose is to send float number via rf link and then receive it and
then send it to PC with putty program.
This works fine with one byte characters or only one number at a time with
cput() and getc()
but how to receive float numbers?

I send float number like this:
Code:
float value;
.
.
value = 12.3;
printf("%2.1f",value);       //send data to RKI-1197 this: rf link I'm using

I'll be happy if you will help me !

best regards
-arto-
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Feb 28, 2017 4:03 am     Reply with quote

CCS sample code shows how to send and receive strings of characters.

Try that first, then you will need to add conversions from float to string and back to float.

Mike

PS Or you could refer back to your 2012 post on sending hex numbers.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Tue Feb 28, 2017 4:17 am     Reply with quote

Honestly 'think again'.

Problem is that generic 'float' covers so many different formats, and such a huge range. Better to use 'fixed point'. You are actually almost doing this. You are telling your output to only give one digit of the decimal. However your printf format is immediately 'wrong'. The number in front of the decimal point in a C printf, is the 'total field width', _not_ the digits in front of the decimal. So your value, actually needs %4.1 (the decimal counts as a character).

Now, how big might your number get?. 999.9, 9999.9?.
Can your number ever be -ve?. Is this a value you want if so?.

Honestly it is always better to use 'fixed digit' formats. So don't do maths using floats, instead use (say) 1/10 unit integers. So you have an integer from (say) 0 to 65535, representing the numbers from 0.0 to 6553.5.
Now this can then be printed using %06.1Lw, to give a number output as 0000.0 to 6553.5. Note how now the number is always the same length, and the decimal is in the same place. So, then you can just read the characters one after the other, start with a 'total' equal to zero, and for each character, multiply the total by ten, then add the value of the digit. Skip the decimal, and you have received the number.

It is always far easier to keep formats constant length. If you must send 'float', then add tests to limit it to a know range (you will see a lot of instrumentation formats that support numbers from (say 0 to 10000).

Even better (save maths and fiddling), don't actually send the number in human readable form. An int16 integer is just two bytes. Send these. Or if you want to only use a limited part of the ASCII range, just send hex. %04x, given an int16 value, prints the four hex digits representing this value.

Seeing 'float' being used, is 99% of the time a sign of not really thinking about the limitations/capabilities of the chip, but instead trying to 'think human'....

To your original question, if you are determined to to go this way, then look at the answer already given, and the function 'atof'.
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 28, 2017 5:16 am     Reply with quote

Ttelmah wrote:
Honestly 'think again'.

Problem is that generic 'float' covers so many different formats, and such a huge range. Better to use 'fixed point'. You are actually almost doing this. You are telling your output to only give one digit of the decimal. However your printf format is immediately 'wrong'. The number in front of the decimal point in a C printf, is the 'total field width', _not_ the digits in front of the decimal. So your value, actually needs %4.1 (the decimal counts as a character).

Now, how big might your number get?. 999.9, 9999.9?.
Can your number ever be -ve?. Is this a value you want if so?.

Honestly it is always better to use 'fixed digit' formats. So don't do maths using floats, instead use (say) 1/10 unit integers. So you have an integer from (say) 0 to 65535, representing the numbers from 0.0 to 6553.5.
Now this can then be printed using %06.1Lw, to give a number output as 0000.0 to 6553.5. Note how now the number is always the same length, and the decimal is in the same place. So, then you can just read the characters one after the other, start with a 'total' equal to zero, and for each character, multiply the total by ten, then add the value of the digit. Skip the decimal, and you have received the number.

It is always far easier to keep formats constant length. If you must send 'float', then add tests to limit it to a know range (you will see a lot of instrumentation formats that support numbers from (say 0 to 10000).

Even better (save maths and fiddling), don't actually send the number in human readable form. An int16 integer is just two bytes. Send these. Or if you want to only use a limited part of the ASCII range, just send hex. %04x, given an int16 value, prints the four hex digits representing this value.

Seeing 'float' being used, is 99% of the time a sign of not really thinking about the limitations/capabilities of the chip, but instead trying to 'think human'....

To your original question, if you are determined to to go this way, then look at the answer already given, and the function 'atof'.


Thank you Ttelmah for your good answer Smile

Ttelmah wrote:

Now, how big might your number get?. 999.9, 9999.9?.
Can your number ever be -ve?. Is this a value you want if so?.

MY float area is: value = min. -99.9 max. +99.9 ( only one decimal)

Ttelmah wrote:
The number in front of the decimal point in a C printf, is the 'total field width', _not_ the digits in front of the decimal.

OK I really thought it is digits in front of decimal...

So what would be the best way to send and receive this value ?

brdgs
-arto-
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 28, 2017 5:19 am     Reply with quote

Mike Walne wrote:
CCS sample code shows how to send and receive strings of characters.

Try that first, then you will need to add conversions from float to string and back to float.

Mike

PS Or you could refer back to your 2012 post on sending hex numbers.


Mike lot of thanks for your good answer Smile

I will try that way ...

all the best !

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