Dear friends,
I am using XS128 controller for one of my applications in which I am making use of CAN. Here, I will have to transmit the floating point values over the bus. I have no issues transmitting the integer values but transmission of signed and floating values is a matter of concern to me. Can any one, who worked on this, help me with a ready solution?
Thanks in advance.
Looks like you are going to transmit IEEE754 floating point number as a binary to some other device. You have two tasks. 1) split/combine float to/from 4 bytes. 2) take care about http://en.wikipedia.org/wiki/Endianness
1) To extract/pack bytes from float or other multibyte variable, float, double, long integer:
#define byteNof(n, x) (((char*)&(x))[n])
float x;
char bytes[4];
// from float
bytes[0] = byteNof(0, x);
bytes[1] = byteNof(1, x);
bytes[2] = byteNof(2, x);
bytes[3] = byteNof(3, x);
// to float
byteNof(0, x) = bytes[0];
byteNof(1, x) = bytes[1];
byteNof(2, x) = bytes[2];
byteNof(3, x) = bytes[3];
2) HC12 is big endian. You windows PC is most likely little endian. You decide, what format to send your data, little or big endian. Then on machine, which is contrary to your network format, instead of sequence from above, you change array indexes like this
byteNof(0, x) = bytes[3];
byteNof(1, x) = bytes[2];
byteNof(2, x) = bytes[1];
byteNof(3, x) = bytes[0];