【问题标题】:HAL_UART_Receive function's size parameterHAL_UART_Receive function\'s size parameter
【发布时间】:2022-12-01 22:20:50
【问题描述】:

I am trying to receive data with STM32F407 by using UART. I've realized that when I'd receive a data, I don't know it's size. Because of this, the code doesn't work when I enter the third parameter like this:

HAL_UART_Receive(&huart4, (uint8_t*)data, strlen(data),100);

How can I solve this in poll method?

Code explanation: If received data is 1, led D12 turns on. Numbers from 1 up to 4 triggers different pins/leds. And 0 turns off all.

Code:

char data[50];
  while (1)
  {
      HAL_UART_Receive(&huart4, (uint8_t*)data, strlen(data), 100);

      if(strcmp(data,"1")==0){
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
      }
      if(strcmp(data,"2")==0){
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
      }
      if(strcmp(data,"3")==0){
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
      }
      if(strcmp(data,"4")==0){
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
      }
      if(strcmp(data,"0")==0){
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
      }
      else{
          continue;
      }
  }

【问题讨论】:

    标签: c uart gpio stm32f4discovery stm


    【解决方案1】:

    you only seem to try and recieve on byte of data, so using a size of 1 should work. Some usual solutions to this kind of problem is either use a first fixed-sized message to send the size of the message you are going to send, or to read it byte per byte and terminate it by either NULL byte or EOT (ascii code 4).

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-01
      • 2012-04-26
      • 2018-11-07
      • 2022-12-02
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多