完全按MX配置,PC向MCU第一次发送命令会响应
FF FF FF FF FF 02 80 00 00 00
FF FF FF FF FF FF 06 80 00 0E 00 00 FE E0 D1 05 05 05 02 08 01 00 00 00 49
但实际是发FF FF FF FF FF 02 80 00 00 82
在次发命令就进不了接收中段
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM21_Init();
MX_USART1_UART_Init();
HAL_TIM_Base_Start_IT(&htim21);
HAL_UART_Receive_IT(&huart1,&RxBuffer, 1);//开中断接收
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET); //LED OFF
while (1)
{
HartManagement(); //HART通讯命令处理
if( ((RXdlytm==0)&&(Rxreal**==1))||((TXdlytm==0)&&(Txreal**==1)) )
{//收/发送超时,重新接收
Txreal**=0;
RX_dp=0;
nField=0;
HAL_UART_Receive_IT(&huart1,&RxBuffer, 1);//开中断接收
}
}
}
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 1200;
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_ONEBIT_SAMPLING_DISABLED;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_HalfDuplex_Init(&huart1);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: trasfer complete*/
if(TX_dp < TX_cn+1)
{
TxBuffer=Uartbuff[TX_dp++];
HAL_UART_Transmit_IT(&huart1,&TxBuffer,1);
TXdlytm=25; //100mS
}else{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET); //LED OFF
Txreal**=0;
Rxreal**=1;
RXdlytm=25;
}
}
|