1
我正在使用微控制器與SIM808模塊進行通信,我想發送和接收AT命令。接收AT命令
現在的問題是,對於某些命令,我只收到我應該收到的某些部分答案,但對於其他人,我收到了我應該收到的答案。例如,如果我按照預期關閉模塊,則會收到「正常斷電」。
我相信我收到了一切,我只是無法看到它。我收到響應的開始和結束,所以問題應該在我解析和緩衝的方式上。我正在使用一個FIFO緩衝的RXC中斷。
例如,指令 「AT + CBC」 我應該得到的東西,如:
「 + CBC:1,96,4175 OK 」
但我收到「+ CBC1, 4130OK」
(I替換爲點的不可讀的字符)
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);
}
關於這個問題本身的兩個觀察:你發佈了不屬於任何函數的「浮動代碼」;你沒有展示RXbuff是如何定義的,或者它的範圍如何與它使用的「浮動代碼」相關。 –
我編輯了代碼 –
「/ *緩衝區中有數據,除非頭部和尾部相等。* /」但(圓形)緩衝區也可能已滿。出於這個原因,你需要第三個變量:緩衝區中的字節數。然後'RX_Head'和'RX_Tail'被接收器和收集器獨立使用,而字節計數由它們遞增和遞減。 –