STM32在做串口实验收到数据后原样发出去时,接收数据不正常,如果接收到的数据有00时,会多一个00发出,如果接收到的数据没有00,收发正常,举例我发送01 03 00 32 00 03 A4 04, 接收到的却是01 03 00 00 32 00 00 03 A4 04,如果没有00就全部正常,没有用中断,采用的查询方式,下面附代码
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB| RCC_APB2Periph_USART1 ,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO ,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOAtiNG;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;//²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//×Ö³¤8λ
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even ;//USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART1_485_Receive_EN();
}
USART1_485_Receive_EN();
if (USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
{
if(1)
{
Uart1RevBuf[rx_index] = USART_ReceiveData(USART1);
rx_index ++;
if(rx_index >=UART1_RSBUF_LEN) rx_index = 0;
USART_ClearFlag(USART1,USART_IT_RXNE);
}
}
Uart1SentBuf[0]= Uart1RevBuf[0];
Uart1SentBuf[1]= Uart1RevBuf[1];
Uart1SentBuf[2]= Uart1RevBuf[2];
Uart1SentBuf[3]= Uart1RevBuf[3];
Uart1SentBuf[4]= Uart1RevBuf[4];
Uart1SentBuf[5]= Uart1RevBuf[5];
Uart1SentBuf[6]= Uart1RevBuf[6];
Uart1SentBuf[7]= Uart1RevBuf[7];
Uart1SentBuf[8]= Uart1RevBuf[8];
Uart1SentBuf[9]= Uart1RevBuf[9];
Uart1SentBuf[10]= Uart1RevBuf[10];
Uart1SentBuf[11]= Uart1RevBuf[11];
Uart1SentBuf[12]= Uart1RevBuf[12];
Uart1SentBuf[13]= Uart1RevBuf[13];
Uart1SentBuf[14]= Uart1RevBuf[14];
Uart1SentBuf[15]= Uart1RevBuf[15];
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1,Uart1SentBuf);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
数据位应该是9位,8位数据加一位奇偶校验位
一周热门 更多>