1

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!

wps
  • 39
  • 5
  • 1
    Show what bytes (ASCII key values, I presume?) the user sends, what resulting output you expect, and what you get instead. Without clear examples input/output, it's not possible to say what you should be doing. – underscore_d Feb 23 '22 at 08:45

1 Answers1

0

Unless you are sending a binary representation of int, you cannot just copy the bytes to a union and get a meaningful integer out.

On ARM, number 123 would be represented as {0x7b, 0x00, 0x00, 0x00} in bytes.

What you probably want to do is use a function like atoi().

I would re-arrange your code as below:

while (1) {
    if(HAL_UART_Receive(&huart1, &receive, 1, HAL_MAX_DELAY) != HAL_OK) {
        continue;
    }

    // Only buffer digits
    if (receive >= '0' && receive <= '9') {
        buffer[counter++] = receive;
    }

    if (receive == '\r' || counter == 4) {
        // Null-terminate the buffer - required by atoi()
        buffer[counter] = '\0';
        counter = 0;
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 0);
        sprintf((char*)send, "The integer received is %#X", atoi((char*)buffer));
        HAL_UART_Transmit(&huart2, send, sizeof(send), HAL_MAX_DELAY);
    }
}

Changes

  • Exit the loop early, if data could not be received.
  • Check that the received values are digits.
  • Check the counter value to protect against buffer overflow.
  • Reset the counter when reception is complete.
  • Use atoi() to convert a string to int.
Armandas
  • 2,276
  • 1
  • 22
  • 27