STM32F103C8T6的串口2连接sim808模块,每次发送AT指令,接收数组总是把发送内容和接收内容一起放在数组里,比如发送“AT
”,接收到“AT
OK
” ,为什么把发送的内容也收到啦,两天啦,没找到原因,头疼啦
void initSerialPort4G(void){
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable the USART2 Pins Software Remapping */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
}
void Initial4G(){
SendCommand4G("AT
", "OK",1);
// SendCommand4G("AT
", "OK",1);
SendCommand4G("AT+SAPBR=3,1,"Contype","GPRS"
", "OK",1);
SendCommand4G("AT+SAPBR=3,1,"APN","cmnet"
", "OK",1);
SendCommand4G("AT+SAPBR=1,1
", "OK",1);
}
void SendCommand4G(char* cmd, char *rsp, uint8_t clean)
{
// uint8_t retry=5;
uint8_t i=0;
for(i=0;i<3;i++)
{
uint16_t len;
char * redata;
Usart_SendString(USART2,(uint8_t *)cmd);
WaitForTransmitComplete(USART2);
Delay_ms(5);
redata = get_rebuff2(&len);
printf("redata = %s",redata);
printf("len = %d",len);
if(len>0)
{
if(strncmp(redata,rsp,2) == 0)
{
break;
}
else
{
clean_rebuff2();
}
}
else
{
Delay_ms(3);
}
}
if(clean==1)
{
clean_rebuff2();
}
else{
}
}
void USART2_IRQHandler(void)
{
Usart2_ReceiveString();
}
void Usart2_ReceiveString(void)
{
if(uart_p2<UART2_BUFF_SIZE)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
uart_buff2[uart_p2] = USART_ReceiveData(USART2);
uart_p2++;
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
// printf("uart_buff2[uart_p2] = %s",uart_buff2[uart_p2]);
}
}
else
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
clean_rebuff2();
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>