似乎您已设置调制解调器回显,因此您将收到您发送的每个字符。
我会将您的代码重写为:
void main(void)
{
uint8_t cmd[10];
uint8_t answer[20];
uint16_t timeout = 500; //max miliseconds to wait for an answer
UART1_Init(9600);
Delay_ms(1000);
TRISB=0;
cmd[0]='A';
cmd[1]='T';
cmd[2]=13;
cmd[3]=10;
cmd[4]=0;//marks end of CMD string
while(1)
{
uint8_t answer_len = SendModemCMD(cmd,answer,timeout);
UART1_Write_Text(answer);
Delay_ms(500);//not really needed ...
}
}
uint8_t SendModemCMD(uint8_t *cmd,uint8_t* answer,uint16_t timeout)
{
uint16_t local_timeout;
uint8_t answer_len=0;
while(*cmd!=0)
{
UART1_Write(*cmd++);
local_timeout=timeout;
while(local_timeout>0 && !UART1_Data_Ready())
{
Delay_ms(1);
local_timeout--;
}
if(UART1_Data_Ready())
{
UART1_Read();//discard echoed character
}
}
uint8_t finished=0;
while(finished==0)
{
local_timeout=timeout;
while(local_timeout>0 && !UART1_Data_Ready())
{
Delay_ms(1);
local_timeout--;
}
if(UART1_Data_Ready())
{
*answer++=UART1_Read();
answer_len++;
}
else
{
finished=1;
}
}
*answer=0;
return answer_len;
}