Best way to send data over CAN in little endian format

Manu

Well-known member
Hello,

I'm currently working on a project that use CAN. I have to mimic an existing device to allow my project to dialogue with another device that I can't modify. For this purpose I have to send data in intel format (little endian).

My original data is an INT32 (signed). I need to send it over 4 bits in little endian format. I use FlexCAN_T4 from Tonton81, but with a Teensy 3.2.

Can someone point me to the most efficient way to do this ?

Datas are GPS positions, so for example : 441303 (for 44'13''03''') or -441303 (for -44'13''03''').

I use CAN 11Bits broadcast.

Currently I do this :

Code:
  trame.buf[4] = CANlat & 0xff;
  trame.buf[5] = (CANlat >> 8) & 0xff;
  trame.buf[6] = (CANlat >> 16) & 0xff;
  trame.buf[7] = (CANlat >> 24) & 0xff;

Thank you,
 
Last edited:
What you're doing is fine. T4.x is little endian, so you could use (byte-wise) memcpy, as shown below, but it's good practice not to rely on the endian-ness of your processor. Your code would work correctly whether T4 was little-endian or big-endian, while the code below would only be correct for little-endian. Your could might also be just as fast. Curious to see if anyone has a different answer.

Code:
memcpy( &trame.buf[4], &CAN1at, 4 )
 
Back
Top