convert floating-point to its byte repsentation to send through CAN bus using memcpy function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-22 12:06 PM - edited ‎2023-08-22 12:12 PM
Hi
I am trying to convert floating point number to its byte representation to send through CAN Bus HAL_CAN_AddMessage() and when I receive it I will convert back to its corresponding floating point number. I can convert the first floating point number to its byte representation and when I receive it back by the HAL_CAN_GetMessage() I convert it back to its floating point number. The first floating point number works fine. The problem occurs when I try to do the second one. I do not receive anything for the second one.
assume that the floating point variable are:
float voltage = 7.3789; I got this value by reading a register with 16bits and 2 bytes
float temperature 21.98290; I got this value by reading a register with 16bits and 2 bytes
the first memcpy() works fine
this is the entire code:
It would be enormous help if someone knows how to solve it
- Labels:
-
STM32CubeIDE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-22 12:58 PM
That the TxData buffer has the last 4 bytes as zero isn't exactly convincing that the data passed into SendFloatsOverCAN() are correct.
Print them out there, confirm they are as expected and temperature != 0
>>float temperature 21.98290; I got this value by reading a register with 16bits and 2 bytes
floats are 4 bytes, not 2, show this, the value and the computation, I'm not convinced.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-22 3:59 PM
Just send the two values, Voltage and Current as the register values. Then the receiving node will convert the bytes to float values.
TimerCallback tutorial! | UART and DMA Idle tutorial!
If you find my solution useful, please click the Accept as Solution so others see the solution.
