接收AT命令

时间:2016-08-22 17:05:09

标签: c microcontroller at-command usart

我正在使用微控制器与SIM808模块通信,我想发送和接收AT命令。

现在的问题是,对于某些命令,我​​只会收到一些我应该收到的答案,但对于其他一些命令,我​​会得到我应该得到的答案。例如,如果我关闭模块,我会按预期接收“NORMAL POWER DOWN”。

我相信我收到了所有东西,我只是没有能力看到它。我收到响应的开始和结束,所以问题应该在我解析和缓冲的方式上。我正在使用FIFO缓冲的RXC中断。

例如,对于命令“AT + CBC”,我应该收到类似的内容:

” + CBC:1,96,4175 好 “

但我收到“+ CBC1,4130OK”

(我用点替换了不可读的字符)

bool USART_RXBufferData_Available(USART_data_t * usart_data)
{
    /* Make copies to make sure that volatile access is specified. */
    uint8_t tempHead = usart_data->buffer.RX_Head;
    uint8_t tempTail = usart_data->buffer.RX_Tail;

    /* There are data left in the buffer unless Head and Tail are equal. */
    return (tempHead != tempTail);
}


uint8_t USART_receive_array (USART_data_t * usart_data, uint8_t * arraybuffer)
{
    uint8_t i = 0;
    while (USART_RXBufferData_Available(usart_data))
    {
        arraybuffer[i] = USART_RXBuffer_GetByte(usart_data);
        ++i;
    }

    return i;
}


void USART_send_array (USART_data_t * usart_data, uint8_t * arraybuffer, uint8_t buffersize)
{
    uint8_t i = 0;

    /* Wait until it is possible to put data into TX data register.
    * NOTE: If TXDataRegister never becomes empty this will be a DEADLOCK. */
    while (i < buffersize)
    {
        bool byteToBuffer;
        byteToBuffer = USART_TXBuffer_PutByte(usart_data, arraybuffer[i]);
        if(byteToBuffer)
        {
            ++i;
        }
    }
}

void send_AT(char * command){

    uint8_t TXbuff_size = strlen((const char*)command);

    USART_send_array(&expa_USART_data, (uint8_t *)command, TXbuff_size);

    fprintf(PRINT_DEBUG, "Sent: %s\n\n", command);

}

void receive_AT(uint8_t *RXbuff){

    memset (RXbuff, 0, 100);

    uint8_t bytes = 0;

    bytes = USART_receive_array(&expa_USART_data, RXbuff);

    int n;
    if (bytes>0)
    {
        RXbuff[bytes]=0;
        for (n=0;n<bytes;n++)
        {
            if (RXbuff[n]<32)
            {
                RXbuff[n]='.';
            }
        }
     }

     fprintf(PRINT_DEBUG, "Received: %s\n\n", RXbuff);

    }

int main(){

unsigned char RXbuff[2000];

send_AT("ATE0\r\n");
receive_AT(RXbuff);

send_AT("AT\r\n");
receive_AT(RXbuff);

send_AT("AT+IPR=9600\r\n");
receive_AT(RXbuff);

send_AT("AT+ECHARGE=1\r\n");
receive_AT(RXbuff);

send_AT("AT+CBC\r\n");
_delay_ms(2000);
receive_AT(RXbuff);

send_AT("AT+CSQ\r\n");
_delay_ms(2000);
receive_AT(RXbuff);

}

1 个答案:

答案 0 :(得分:0)

所以,问题与这部分代码没有关系。我使用模拟串行端口从微控制器到PC打印东西。问题在于,我在PC上打印字符的速度比PC接收的速度快得多,这就是为什么某些部分没有出现的原因。