Search code examples
cmicrocontrollerat-commandusart

Receiving AT commands


I'm using a microcontroller to communicate with a SIM808 module and I want to send and receive AT commands.

The problem right now is that for some commands I receive only some portions of the answers I should receive, but for some others I receive what I should. For example, if I shut down the module I receive "NORMAL POWER DOWN", as expected.

I believe I'm receiving everything, I'm just not being capable of seeing it. I receive the beginning and the end of the response, so the problem should be on the way I parse and buffer. I'm using a FIFO buffered RXC interrupt.

For example, for the command "AT+CBC" I should receive something like:

" +CBC: 1,96,4175 OK "

But I receive "+CBC1,4130OK"

(I replaced the unreadable characters with a dot)

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

}

Solution

  • So, the problem didn't have to do with this part of the code. I am using an emulated serial port to print stuff from the micro-controller to the PC. The issue was that the rate with which I was printing a char to the PC was much faster than what the PC was receiving, that's why some parts didn't appear.