Floating values transmission over CAN

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Floating values transmission over CAN

1,397 次查看
vishaka_maithil
Contributor I

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.

标签 (1)
0 项奖励
回复
1 回复

1,270 次查看
kef
Specialist I

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];

 

0 项奖励
回复