Search code examples
cuartgpiostm32f4discovery

HAL_UART_Receive function's size parameter


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;
      }
  }

Solution

  • Thanks everyone for their help. I've handled the issues. This is the code:

    while (1)
      {
          char data[10];
              HAL_UART_Receive(&huart2, (uint8_t*)data, 10, 1500);
    
              //char data[50];
              // HAL_UART_Receive(&huart4, (uint8_t*)data, strlen(data), 1500);
    
              if(IS_CONTAIN(data,'1')==1){
                  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
              }
              if(IS_CONTAIN(data,'2')==1){
                  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
              }
              if(IS_CONTAIN(data,'3')==1){
                  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
              }
              if(IS_CONTAIN(data,'4')==1){
                  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
              }
              if(IS_CONTAIN(data,'0')==1){
                  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;
              }
              memset(data, 0, 10*sizeof(data[0]));
              HAL_Delay(1500);
      }
    
    

    IS_CONTAIN function:

    int IS_CONTAIN(char* num,char parameter){//Çalışıyor
        int state = strlen(num);
        int i=0;
        while(i+1<=state){
            if(num[i]==parameter){
                return 1;
            }
            i++;
        }
        return 0;
    }
    

    libs: string.h