View previous topic :: View next topic |
Author |
Message |
cenadius
Joined: 13 Mar 2010 Posts: 11
|
how to send and receive float for serial communication? |
Posted: Thu Apr 01, 2010 9:44 am |
|
|
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
|
|
Posted: Thu Apr 01, 2010 9:56 am |
|
|
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
|
|
Posted: Thu Apr 01, 2010 10:16 am |
|
|
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
|
|
Posted: Sun Apr 04, 2010 3:52 am |
|
|
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
|
|
Posted: Tue Apr 06, 2010 1:52 am |
|
|
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
|
|
Posted: Tue Apr 06, 2010 6:46 am |
|
|
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: 19538
|
|
Posted: Tue Apr 06, 2010 7:10 am |
|
|
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
|
|
Posted: Thu Apr 08, 2010 10:50 pm |
|
|
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 |
|
|
|