SIM900:如何确定串行(UART)发送和接收之间等待的时间



我已经用C编写了一个库,用于在我的uC中使用SIM900 GSM,但它有很多错误。有时有效,有时无效。我认为我的硬件运行良好。

我重写了它,并确保基本功能没有错误。

  • SIM900_transmit(char*(
  • SIM900_reveive(字符**(
  • SIM900_on((
  • SIM900_off((

现在,我想编写SIM900_command函数,该函数将使用SIM 900_transmit(char*(和SIM900_reveive

所以我的详细问题是:

如何知道AT命令和从SIM900接收到答案之间需要等待多长时间。我不想只放_delay_ms(1000(。

提前感谢。。。

通常,您会等到一个字符可用后再接收它。请查看此处。你有一个单一UART:的AVR设备

unsigned char uart_recieve (void)
{
    while(!(UCSRA) & (1<<RXC));
    return UDR;
}

因此,对于while(!(UCSRA) & (1<<RXC));,您基本上会阻止执行,直到uart中有可用的字符为止。

我发现了我的主要问题。现在我将修复旧的SIM900库。

旧代码:

/*
 * Sent the AT command.
 */
softuart_puts_P( "Transmit" );
SIM900_transmit( "ATr" );
softuart_puts_P( " completedrn" );
/*
 * Wait until SIM900 answers back.
 */
while ( uart_available() == 0 )//BUG: Loop in a loop(receive has one) caused chaos.
{   
    /*
     * Sent the AT command.
     */
    softuart_puts_P( "Received: [" );
    cString answer = newEmptyString();
    SIM900_receive( &answer );
    softuart_puts( answer );
    softuart_puts_P( "]rn" );
    deleteString( &answer );
}

新代码

/*
 * Sent the AT command.
 */
softuart_puts_P( "Transmit" );
SIM900_transmit( "ATr" );
softuart_puts_P( " completedrn" );
/*
 * Wait until SIM900 answers back.
 */
while ( uart_available() == 0 );// TODO: Put timeout in case SIM900 is switched off.
/*
 * Sent the AT command.
 */
softuart_puts_P( "Received: [" );
cString answer = newEmptyString();
SIM900_receive( &answer );
softuart_puts( answer );
softuart_puts_P( "]rn" );
deleteString( &answer );    

这就完成了以下工作:while(uart_available((==0(

谢谢你的帮助。

相关内容

  • 没有找到相关文章

最新更新