2019-11-20 02:54 PM
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?
2019-11-20 04:50 PM
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
2019-11-20 04:54 PM
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.
2019-11-20 10:32 PM
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).