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'被接收器和收集器独立使用,而字节计数由它们递增和递减。 –