【发布时间】: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