2016-04-19 34 views
3

我正在使用Atmega32和sim900作为项目。我一直发送“AT”命令并等待“OK”响应,但我所得到的只是AT \ r \ n。我检查并重新检查了接线和我的波特率,但仍然没有找到。无论发送给sim900,我只会回显同一个发送的字符串。Sim900只响应回命令 - 无响应

任何人都可以帮助我吗?我真的很感激它。

我在这里发布我的代码:

int sim900_init(void) 
{ 
    while(1) 
    { 
    sim_command("AT"); 
    _delay_ms(2000); 
    } 
return 0; 
} 

void usart_init(void) 
{ 
/************ENABLE USART***************/ 
    UBRRH=(uint8_t)(MYUBRR>>8); 
    UBRRL=(uint8_t)(MYUBRR);       //set baud rate 
    UCSRB=(1<<TXEN)|(1<<RXEN);    //enable receiver and transmitter 
    UCSRC=(1<<UCSZ0)|(1<<UCSZ1)|(1<<URSEL); // 8bit data format 

    UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC) 

/***************FLUSH ALL PRIVIOUS ACTIVITIES********************/ 

    flush_usart(); 

/*********APPOINT POINTERS TO ARRAYS********************/ 

    command=commandArray;  // Assigning the pointer to array 
    response=responseArray;  //Assigning the pointer to array 

/*****************ENABLE INTRUPT***************************/ 
sei();       //Enabling intrupts for receving characters 
} 


void flush_usart(void) 
{ 
    response_full=FALSE;  //We have not yet received the 
} 

void transmit_char(unsigned char value) 
{ 
    while (!(UCSRA & (1<<UDRE)));   // wait while register is free 
    UDR = value; 
} 

void sim_command(char *cmd) 
{ 
    int j=0; 
    strcpy(command,cmd); 
    while(*(cmd+j)!='\0') 
    { 
     transmit_char(*(cmd+j)); 
     j++; 
    } 
    transmit_char(0x0D); // \r     // after all the at commands we should send \r\n so, we send it here after the string 
    transmit_char(0x0A); // \n 

} 

unsigned char recieve_char(void) 
{ 
    char temp; 
    while(!(UCSRA) & (1<<RXC));   // wait while data is being received 
    temp=UDR; 
    LCDdata(lcdchar,temp); 
    return temp; 
} 

void recive_sim900_response(void) 
{ 
    static int i=0; 
    char temp; 
    temp=recieve_char(); 

    if(temp!='\n' && temp!='\r')  // We dont want \r \n that will be send from Sim so we dont store them 
    *(response+i)=temp; 

     if(i==8)     //when null char is sent means the string is finished- so we have full response 
     {        //we use them later in WaitForResponse function. we wait until the full response is received 
      i=0;          
      response_full=TRUE; 
     } 
     else 
      i++; 
} 
+0

在你显示的代码中,你永远不会调用'recive_sim900_response()'或任何东西。 (不相关,但是:你的响应缓冲区真的是** 9 **字节吗?) – JimmyB

+0

'flush_usart()'现在还应该将索引重置到缓冲区(它是'recive_sim900_response()'中的'static int i')。) – JimmyB

+0

'if(temp!='\ n'&& temp!='\ r')'后面缺少大括号。 – JimmyB

回答

0

你是唯一一个谁从gsmlib.org工作有完全一样的问题,因为一

不知怎的,图书馆,而是直接进入AT命令使用使用Arduino作为桥梁的Arduino串行监视器,或者仅仅使用FTDI并没有。

原因是显然SIM900需要命令以'\ r'结尾。我通过尝试GTKTerm找到了这个。

如果输入“AT”,并在GTKTerm什么是实际发送的是迫切输入“AT”后面两次“\ r”(0X0D)和一个0X0A

0

默认情况下,GSM模块处于回显ON模式。你需要改变你的命令。

sim_command("AT"); 

你需要输入= CR/LF后命令,以便修改这样的代码,并给予一试

sim_command("AT\r"); 

而如果你想关闭回显命令你那么送你应该发送此命令一旦您有AT命令确定回应。

sim_command("ATE0\r"); //Echo back OFF 
sim_command("ATE1\r"); //Echo back ON