I have connect my HC-06 to pc using bluetooth, so i am able to send data to HC-06. My work is to send any int I typed in PC to the bluepill (which will then display the value in another screen). I am doing this using union concept.
typedef union int_buffer
{
int i;
struct
{
uint8_t bytes[4];
};
}int_buff;
int_buff int_Tx; // Transmit to bluepill
uint8_t receive;
uint8_t send[50], buffer[5];
int counter = 0;
Above is variable I declared. Below is my code:
/* USER CODE BEGIN WHILE */
while (1)
{
if(HAL_UART_Receive(&huart1, &receive, 1, HAL_MAX_DELAY) == HAL_OK) // Sending one by one
{
buffer[counter++] = receive;
}
if(buffer[counter - 1] == '\r')
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
memcpy(int_Tx.bytes, buffer, sizeof(int));
sprintf((char* )send, "The integer received is %#X", int_Tx.i);
HAL_UART_Transmit(&huart2, send, sizeof(send), HAL_MAX_DELAY);
}
I have already know that UART is sending bytes by bytes. Firstly, I collect all the bytes sent by user and store them into a buffer. When '\r' is pressed, the int is transmitted to bluepill, which can view the value on serial studio. My problem is that there are some value displayed but they are all random number and unknown symbol.
I am not so sure that my concept correct or not. I think it is just storing every bytes into union, then print out the int of union. Thx for the help!