View previous topic :: View next topic |
Author |
Message |
Requan
Joined: 11 May 2008 Posts: 74
|
Odd problem with hex data send via uart |
Posted: Sat May 26, 2012 5:26 am |
|
|
Hi,
In my modbus application i want to send float as hex via uart.
I converded float to hex and send data:
Code: |
float ftValue= 21.34;
int32 decValue= f_PICtoIEEE(ftValue);
printf("%Lx",decValue)
|
but i received in terminal data in ascii so 8 bytes insted 4,
e.g. value 31 it sent as 33 31 instead 31.
How to fix it? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat May 26, 2012 6:21 am |
|
|
It is not such an odd problem as the result is exactly as expected.
%Lx will send the data as human readable text, i.e. ASCII characters.
What I understand is that you want to receive the data as binary, not hex. Then use %Ld |
|
|
Requan
Joined: 11 May 2008 Posts: 74
|
|
Posted: Sat May 26, 2012 7:13 am |
|
|
Yes i want to receive the data as binary. I tried %Ld - not helps.
Only %c send proper but 1 byte only.
(my pic 18f67j60, ccs 4,132) |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Sat May 26, 2012 9:47 am |
|
|
you're really better off just doing putc():
Code: |
float ftValue= 21.34;
int32 decValue= f_PICtoIEEE(ftValue);
putc(make8(decValue,0));
putc(make8(decValue,1));
putc(make8(decValue,2));
putc(make8(decValue,3));
|
or you can order them 3,2,1,0 if you prefer big endian. |
|
|
Requan
Joined: 11 May 2008 Posts: 74
|
|
Posted: Sun May 27, 2012 6:56 am |
|
|
jeremiah
Thanks again.
Last question: what will be faster:
use function f_PICtoIEEE or use union? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sun May 27, 2012 7:49 am |
|
|
Learn by doing !!!
It'll only take you 5-10 minutes and you'll learn firsthand which is faster, just dump out the listing and check the code to see how and why one is faster, maybe..... it could be a tie.....
Also faster may not be better if it takes up more codespace (ROM).... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Sun May 27, 2012 8:35 am |
|
|
I think you would have great problems doing what PICtoIEEE does with a union, and it'd certainly be a lot slower. Look at the source code. It is basically optimised as far as possible.
For what is being shown, you could replace the mak8's, with a union. Slightly tidier, and takes exactly the same time.
For the IEEE conversions, I did it years ago, before the functions were supplied, using a pointer, with the following code:
Code: |
void toccs(int *Value) {
//program to convert a 4 bytes 'word' from IEEE to CCS format
int1 sign;
sign = shift_left(&Value[1],1,0);
sign = shift_left(&Value[0],1,sign);
shift_right(&Value[1],1,sign);
}
void toIEEE(int *Value) {
//complement to the above
int1 sign;
sign = shift_left(&Value[1], 1, 0);
sign = shift_right(&Value[0], 1, sign);
shift_right(&Value[1], 1, sign);
}
|
As you see, you basically have to relocate the sign bit inside the 4 bytes. The CCS supplied version is much more efficient, since it optimises the rotations, but this has the advantage of working anywhere in memory, instead of having to use fixed addresses. YPYM.
Best Wishes |
|
|
Requan
Joined: 11 May 2008 Posts: 74
|
|
Posted: Mon May 28, 2012 1:54 am |
|
|
Ttelmah
Thanks for good example.
I am still learning and i don't know if think in correct way, but i though about use union instead of function PICtoIEEE, e.g.
Code: |
union my_var
{
float ft;
int8 b[4];
}temperature;
|
and put data in temperature.ft, next take from temperature.b |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Mon May 28, 2012 3:04 am |
|
|
No. That will replace the 'make8' functions being shown, but not do what PICtoIEEE does. The IEEE float format, has the two sign bits positioned differently. You have to pull the sign bit out of the second byte. Put this into the first byte in place of the sign bit that is there, then put the bit that was in the first byte back into the second.....
The code being shown uses the supplied function to do this, _then_ outputs the bytes, now in IEEE format. The actual output, can be done with a union, but not (easily) the bit swap. It could be done by declaring a structure, with the required bit fields, then using these to do the swap, and then the union to do the output.
Best Wishes |
|
|
|