C语言 通过PIC微控制器接收AT命令的OK响应


char rcv[10];
void main()
{
UART1_Init(9600);
Delay_ms(2000);
TRISB=0x00;
UART1_Write_Text("at");
UART1_Write(13); //Enter key = CF + LF
UART1_Write(10);
delay_ms(500);
while (1)
{ PORTB.RB0=1; // Endless loop
while(!UART1_Data_Ready()); // If data is received,
rcv[0]=UART1_Read();
rcv[1]=UART1_Read();
rcv[2]='';
UART1_Write_Text(rcv);
PORTB.RB0=0;
}
}

使用的编译器:MikroC我得到的rcv输出是tttttttttt。请帮助我在这里收到来自GSM调制解调器的OK响应,因为这与超级终端一起工作。在microelectronika的PICPLC16v6开发板上使用PIC 18F4520

似乎你已经打开了调制解调器回声,所以你会收到你发送的每个字符。

我会把你的代码重写成这样:

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;   
}

相关内容

  • 没有找到相关文章

最新更新