cancel
Showing results for 
Search instead for 
Did you mean: 

How to send a floating point value as a payload in HAL_CAN

KA
Associate II

Using The HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox) function , can a floating point value be sent as a payload?

3 REPLIES 3

You can send 8-bytes of data, the micro-controller and CAN bus don't know/care what that data represents.

Cast the pointer to the data, or copy into an intermediate buffer.

sizeof(float) is 4

sizeof(double) is 8

ie

double mydouble;

memcpy(data, &mydouble, sizeof(mydouble));

float myfloatarray[2];

memcpy(data, myfloatarray, sizeof(myfloatarray));

HAL_CAN_AddTxMessage(hcan, pHeader, (void *)&mydouble, pTxMailbox);

#CProgramming #DataRepresentation101

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
alister
Lead

A payload may contain any data, so of course it may contain floats.

You'll need to define a protocol so the receiver knows what the message is and thereby where the float(s) are in it.

Google about float types, e.g. single-precision float (32-bit) or about other float types so you know how much space they'll need in your payload.

Google about the natural alignment of float types in the ARM so you know the necessary measures to copy them into your payload.

E.g. if a float's position in the payload isn't naturally aligned you'll either manually bytewise copy it into your message or declare a packed struct type describing it so the compiler knows to bytewide copy it for you. To manually bytewise copy you'd cast the address of your float variable to byte pointer.

Ozone
Lead

CAN (at least CANopen) uses little endian representation for values greater then one byte.

CANopen has mechanisms to transfer objects of arbitrary size (segmented transfer).