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 send and receive float for serial communication?

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



Joined: 13 Mar 2010
Posts: 11

View user's profile Send private message

how to send and receive float for serial communication?
PostPosted: Thu Apr 01, 2010 9:44 am     Reply with quote

The ccs function for rs232 send and receive data can only use getc() and putc()?

If i want to send a float value or string, getc() and putc() seem can't be use for this purpose, is there any other function other than getc() and putc()?

I know there is a printf() to substitute the putc(), but if i use printf() then the getc() cant receive the exact value......

can any1 guide me?

thanks.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Apr 01, 2010 9:56 am     Reply with quote

It depends if you want to send it in ASCII format or in binary ?

I like to just send the 4 bytes of the float and just re-build the float on the receiver but if you are sending to anything other than another pic you will have to re-arrange some of the bits.
cenadius



Joined: 13 Mar 2010
Posts: 11

View user's profile Send private message

PostPosted: Thu Apr 01, 2010 10:16 am     Reply with quote

Thanks for reply.

If I want to send in binary, how to convert it to binary and and convert it back to float at receiver?
Torello



Joined: 29 Sep 2006
Posts: 120

View user's profile Send private message

PostPosted: Sun Apr 04, 2010 3:52 am     Reply with quote

In a nut shell:

Create a struct with all your variables that you want to parse in each line that you send in your serial communication.
Receive the line in a buffer (int8 array), undo it from overhead information (like command and checksum (if there is)) and then memcpy the bare binary data in the struct variable. In this way each variable get typecasted and is directly accessible.
Of course you need to send the data in the right sequence according to the type of variables used.

I use this a lot to parse (UBX) data from a Gps module. I think is it a beautiful, easy way to do it.

Regards,
Edwin
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 1:52 am     Reply with quote

Or

To send
Code:

float f;
int8 *p;

f = 0.0123;
p = &f;  // pointer p points to the first byte of f

putc(p[0]);  // use p as an array Send the first byte
putc(p[1]);  // Send the second byte
putc(p[2]);  // Send the third byte
putc(p[3]);  // Send the forth byte


To receive
Code:

int8 f;
int8 *p;

p = &f;
p[0] = getc();
p[1] = getc();
p[2] = getc();
p[3] = getc();

printf("%f", f);
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 6:46 am     Reply with quote

Bravo Wayne_ !

A very clear simple example of C pointers in use. That is going in my (virtual) scrapbook.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 7:10 am     Reply with quote

Add one more comment to this thread.
In CCS C (for the PIC 12/16/18), the format of FP numbers in memory, is different from the IEEE format used inside a PC.
The file 'ieeefloat.c', contains two routines, to convert a PIC float directly to a PC float, and vice-versa.

So take Wayne's example, and modify as:
Code:

#include "ieeefloat.c"

float f;
int32 PCval
int8 *p;

f = 0.0123;
PCval=f_PICtoIEEE(f);
p = &PCval;  // pointer p points to the first byte of f

putc(p[0]);  // use p as an array Send the first byte
putc(p[1]);  // Send the second byte
putc(p[2]);  // Send the third byte
putc(p[3]);  // Send the fourth byte


And similarly in the other direction.

This then allows the PC end, to use exactly the same techniques, to put the bytes directly into a 'single' (4 byte) float, and the value will work.

Best Wishes
Gavin Pinto



Joined: 18 Oct 2009
Posts: 27
Location: Australia

View user's profile Send private message

PostPosted: Thu Apr 08, 2010 10:50 pm     Reply with quote

Ttelmah wrote:
Add one more comment to this thread.
In CCS C (for the PIC 12/16/18), the format of FP numbers in memory, is different from the IEEE format used inside a PC.
The file 'ieeefloat.c', contains two routines, to convert a PIC float directly to a PC float, and vice-versa.

So take Wayne's example, and modify as:
Code:

#include "ieeefloat.c"

float f;
int32 PCval
int8 *p;

f = 0.0123;
PCval=f_PICtoIEEE(f);
p = &PCval;  // pointer p points to the first byte of f

putc(p[0]);  // use p as an array Send the first byte
putc(p[1]);  // Send the second byte
putc(p[2]);  // Send the third byte
putc(p[3]);  // Send the fourth byte


And similarly in the other direction.

This then allows the PC end, to use exactly the same techniques, to put the bytes directly into a 'single' (4 byte) float, and the value will work.

Best Wishes


Excellent Ttelmah. Thanks for showing the difference between floating point storage formats.
_________________
Clear Logic
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