I want my PC to receive float value from the microcontroller so as to plot graph based on the value. I already can let microcontroller to receive value from PC. I am using union decode the float into bytes.
My union in both microcontroller and PC socket(I connect my PC to microcontroller through bluetooth, HC-06 and use HC-06 to connect to my stm32f103 through uart)
union
{
float data;
struct
{
uint8_t bytes[4];
};
}Float[250];
How I transmit bytes from STM32F103 to PC: I first put my value in an array and then put the elements inside the union bytes before sending. (The value is keep updating, I tried to send the whole array before but seems failed).
for(int i = 0; i < 250; i++)
{
Float[i].F = data[i];
}
for(int i = 0; i < 250; i++)
{
for(int j = 0; j < 4; j++)
{
HAL_UART_Transmit(&huart1, (uint8_t* )&Float[i].bytes[j], 1, HAL_MAX_DELAY);
while(USART1->SR != USART_SR_TC){};
}
}
HAL_UART_Transmit(&huart2, (uint8_t*)"Transmitted to PC\n", 18, HAL_MAX_DELAY);
value = 0;
memset(data, 0, 250);
counter = 0;
In my PC, i receive the data through rfcomm
status = recv(s, buffer, 1000, 0);
Then decode the bytes into floats:
for(int i = 0; i < 250; i++)
{
for(int j = 0; j < 4; j++)
{
Float[i].bytes[j] = buffer[j + rcv_counter];
}
rcv_counter += 4;
}
However, when i received the bytes send from microcontroller and decode them, i got all zero... I am not sure what is the reason. I also glad people provide some other way to do this. . My main goal is just to transmit float values from microconttroller to PC through uart. Thx very much.
Just to update my progress:
Now I can receive true float value from stm32f103 only once, if I continue requesting stm32f103 to send value, it will send only zero, I not so sure why is this happening.
float test[5] = {1.0, 5.6, 4.5, 2.3, 8.9}; // float value to be send
HAL_UART_Transmit(&huart1, (uint8_t* )test, sizeof(test), HAL_MAX_DELAY); // Transmit from stm32f103 to PC
union
{
float data;
struct
{
uint8_t bytes[4];
};
}Float[5]; // Union to decode bytes
if(recv(s, buffer, 20, 0) >= 0)
{
cout << "Message received from bluepill" << endl;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 4; j++)
{
Float[i].bytes[j] = buffer[j + rcv_counter];
}
rcv_counter += 4;
}
for(int i = 0; i < 5; i++)
{
cout << Float[i].data << endl;
}
} // Way to receive 5 float value and print to console
When i try this, i can only success one time, start from second time, i keep receiving 0, thx for helping