cancel
Showing results for 
Search instead for 
Did you mean: 

convert floating-point to its byte repsentation to send through CAN bus using memcpy function

Ali Suleimani
Associate II

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:

uint32_t TxMailbox[3];    //create a variable Mailbox to transmit data
uint8_t TxData[8];    // buffer for transmit data
uint8_t RxData[8];    // buffer for recieving data
uint8_t count = 0;
 
float receivedVoltage;
float receivedTemperatuer;
 
 
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
   HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData); // data will be recieved from CAN_RX_FIFO0, the header will be stored in RxHeader, data will be saved in RxData
 
   memcpy(&receivedVoltage, &RxData, sizeof(receivedVoltage));
   memcpy(&receivedTemperatuer, &RxData + sizeof(receivedVoltage), sizeof(receivedTemperatuer));
 
}
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
char uart_buf[50];
int uart_buf_len;
    uint16_t timer_val;
 
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_CAN2_Init();
  MX_TIM6_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_CAN_Start(&hcan2);   // start the can here
  HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);    //activate the notification of recieving data
 
  TxHeader.DLC = 8;  // this specifies the length of the data that will be send //8
  TxHeader.ExtId = 0;  // since we use standard protocol we set it to zero
  TxHeader.IDE = CAN_ID_STD; // the IDE is the standard ID
  TxHeader.RTR = CAN_RTR_DATA;  // we send data not frame
  TxHeader.StdId = 0x102;  // this is the identifire, must check why 0x103
  TxHeader.TransmitGlobalTime = DISABLE;
 
 
 // HAL_CAN_AddTxMessage(&hcan2, &TxHeader, (uint8_t*)TxData, &TxMailbox);
 
  //uint8_t dev_address1 = 0x16;
  uint8_t dev_address = 0x6C;
 
  void SendFloatsOverCAN( float voltageVal, float temperatureVal){
  memcpy(&TxData, &voltageVal, sizeof(voltageVal));
  memcpy(&TxData + sizeof(voltageVal), &temperatureVal, sizeof(temperatureVal));
 
  HAL_CAN_AddTxMessage(&hcan2, &TxHeader, TxData, TxMailbox);
  }
while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
 
  main_function(dev_address);
  SendFloatsOverCAN(voltage, temperature);   // voltage and temperature are the floating points varaibles
  HAL_Delay(500);
 
  }
the same data is in RxData and TxData
AliSuleimani_0-1692735046445.png

 

It would be enormous help if someone knows how to solve it

2 REPLIES 2

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.

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

Just send the two values, Voltage and Current as the register values. Then the receiving node will convert the bytes to float values.