为什么系统向上位机可以发送数据,但是上位机发送命令串口确没有反应
串口配置:
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
}
串口接收回调函数:
uint8_t Rx_dat[16];
uint8_t str1[50];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)
{
if(Rx_dat[0] == 0xBF && Rx_dat[2] == 0xFB)
{
switch(Rx_dat[1])
{
case 0xa1:
LED2_ON;
sprintf((char *)str1,"%d:%d:%d LED2打开!\r\n",hh,min,sec);
break;
case 0xa2:
LED2_OFF;
sprintf((char *)str1,"%d:%d:%d LED2关闭!\r\n",hh,min,sec);
break;
default:
sprintf((char *)str1,"%d:%d:%d 这是一个错误的命令!\r\n",hh,min,sec);
break;
}
HAL_UART_Transmit(&huart1,str1,sizeof(str1),10000);
HAL_UART_Receive_IT(&huart1,Rx_dat,3);
}
}
}
main函数:
LCD_Init();
LCD_SetBackColor(Black);
LCD_Clear(Black);
LCD_SetTextColor(White);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
LED_Check();
char tep[] = "XMF07欢迎你!\r\n";
HAL_UART_Transmit(&huart1, (uint8_t*)tep,sizeof(tep), 100);
HAL_TIM_Base_Start_IT(&htim2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_UART_Receive_IT(&huart1,Rx_dat,3);
}
/* USER CODE END 3 */
串口助手: