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
It would be enormous help if someone knows how to solve it