gpt4 book ai didi

c++ - 如何显示/保存返回 AT 命令的值

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:23 25 4
gpt4 key购买 nike

大家好!我正在使用 Arduino UNO 和 SIM908 进行一个项目。我试图理解 AT 命令。当我进入时

Serial.print("AT")
Serial.println("AT+CGPSSTATUS?");

串行返回一个值,我想将该值保存到缓冲区中

char buffer[size]

除了 AT 命令的返回值,我不想有其他字符串。

那个文件我也红了 SIM908 AT Command Manual_V1.01

在第13页可以看到(注:< CR>< LF>:我在第一个<后面加了一个空格,其他不显示)

The "AT" or "at" prefix must be set at the beginning of each Command line. To terminate a Command line enter < CR>. Commands are usually followed by a response that includes. "< CR>< LF>< CR>< LF>" Throughout this document, only the responses are presented, < CR>< LF> are omitted intentionally

然后,我问如何“提取”< CR>< LF> 之间的响应和 < CR>< LF>

看看这个例子(如果我错了请告诉我),我怎样才能检测到 < CR>< LF>

void setup()
{
char buffer[200];
Serial.println("AT+CGPSSTATUS?");
}
void loop()
{
if (Serial.available())
{
// HERE I SHOULD CHECK IF CR ANF LF
Serial.write(Serial.read());
// AND SAVE IT IN buffer. IS'T NOT?
}
}

}

你明白我的意思了吗?你怎么能帮我把 AT 命令的返回值存储在缓冲区中?

非常感谢您的帮助

最佳答案

你给我看的东西很有趣。这是我调整代码的方式

我调整了我的代码,顺便说一句,我在发送 AT 命令时创建了一个用于测试 Serail 的文件。关注函数是 loop() 和 read_AT_string()。 (我将 read_String 重命名为 read_AT_string()。

这里是我的代码,我会在问题之后解释你的提议

#include <SoftwareSerial.h>

int baud_rate = 9600;
int pin_gsm = 3;
int pin_gps = 4;
int pin_power = 5;
//int pin_dtr = 6;
boolean debug = true;
boolean raedy_to_go = false;

// Reading String
#define BUFFERSIZE 200
char buffer[BUFFERSIZE];
char inChar;
int index;

void setup()
{
Serial.begin(baud_rate);
delay(5000); // Wait for 5sec after begin

if(debug)
{
Serial.println(F("\n****************************"));
Serial.println(F("STARTING SYSTEM Read AT stream"));
Serial.println(F("******************************"));
}
pinMode(pin_gsm,OUTPUT); // Set the pins
pinMode(pin_gps,OUTPUT);
pinMode(pin_power,OUTPUT);

powerUpSim908:
if(powerUpSim908())
{
delay(1000);

if(gps_power()){

gsm_enable();
raedy_to_go = true;

if(debug)
{
Serial.println(F("\n****************************"));
Serial.println(F("READY TO GO\n"));
Serial.println(F("****************************\n"));
}
}
else
{
raedy_to_go = false;
if(debug)
{
Serial.println(F("\nNOT READY TO GO.\nGPS could not be power\nRestart the module\nor/and check the battery level.\n"));
}
goto powerUpSim908;
}
}
else
{
raedy_to_go = false;
if(debug)
{
Serial.println(F("\nNOT READY TO GO.\nCheck the battery level.\n"));
}
};
}

void loop()
{
/*
if (Serial.available())
{
Serial.print("Character received: ");
Serial.write(Serial.read());
Serial.println("");
}
*/
if(raedy_to_go)
{

read_AT_string("AT",5000);
delay(10000);

}

}

char read_AT_string(char* command, int timeout)
{
unsigned long previous;
previous = millis();


Serial.println(F("\nDISPLAY BUFFER:"));
index=0;

Serial.println(command);
do
{
if(Serial.available() > 0) // Don't read unless
// there you know there is data
{
Serial.println("1");
if (Serial.peek() == 13) // check if CR (without reading)
{
Serial.println("13");
if(Serial.available() > 0)
{
Serial.read(); // read and ignore
if (Serial.peek()==10) // then check if LF (without reading)
{
Serial.println("10");
if(index < Serial.readBytesUntil(13, buffer, BUFFERSIZE-1)) // One less than the size of the buffer array
{
Serial.println("b");
inChar = Serial.read(); // Read a character
buffer[index] = inChar; // Store it
index++; // Increment where to write next
buffer[index] = '\0'; // Null terminate the string
}
}
}
}
}
}while(((millis() - previous) < timeout));

Serial.println(buffer);
buffer[0]='\0';
Serial.println(F("END DISPLAY BUFFER"));
}

/* FUNCTION */

boolean powerUpSim908(void)
{
if(debug)
{
Serial.println(F("Powering up SIM908"));
}
boolean turnedON = false;
//uint8_t answer=0;
int cont;

for (cont=0; cont<3; cont++)
{
digitalWrite(pin_power,HIGH);
delay(1500);
digitalWrite(pin_power,LOW);

Serial.println(F("Checking if the module is up"));
if(sendATcommand("AT", "OK", 5000))
{
cont = 4; // Leave the loop
turnedON = true;
}
else
{
turnedON = false;
if(debug)
{
Serial.println(F("\nTrying agin to turn on SIM908"));
}
};
}

if(turnedON)
{
if(debug)
{
Serial.println(F("Module is tunrned up\n"));
}
}
else
{
if(debug)
{
Serial.println(F("Module is NOT tunrned ON\n"));
}
}
return turnedON;
}

boolean sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
{
uint8_t x=0;
bool answer=false;
//åchar response[100];
//buffer[0]='\0';
unsigned long previous;

//memset(response, '\0', 100); // Initialice the string
//Serial.println(response);

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

if (ATcommand[0] != '\0')
{
Serial.println(ATcommand); // Send the AT command
}

x = 0;
previous = millis();

index=0;
do
{
if(Serial.available() > 0)
// there you know there is data
{
if(index < BUFFERSIZE-1) // One less than the size of the array // Same as buffer size
{
inChar = Serial.read(); // Read a character
buffer[index] = inChar; // Store it
index++; // Increment where to write next
//Serial.println(index);
buffer[index] = '\0'; // Null terminate the string
}
}
}while(((millis() - previous) < timeout));


if(strstr(buffer,"NORMAL POWER DOWN") != NULL)
{
answer = false;
}
else if (strstr(buffer, expected_answer) != NULL) // check if the desired answer (OK) is in the response of the module
{

/*
Serial.println(F("### BUFFER"));
Serial.println(buffer);
Serial.println(F("### END BUFFER"));
*/
answer = true;
}
else
{
answer = false;
}

if(debug)
{
if(answer)
{
//Serial.println(F("Expected answer : OK!\n"));
}
else
{
//Serial.println(F("Expected answer : KO!\n"));
};
}
return answer;
}


void gps_enable(void)
{
if(debug)
{
Serial.println(F("\nEnabling GPS ..."));
}
digitalWrite(pin_gps,LOW); //Enable GPS mode
digitalWrite(pin_gsm,HIGH); //Disable GSM mode
delay(2000);
}



void gsm_enable(void)
{
if(debug)
{
Serial.println(F("\nEnabling GSM ..."));
}
digitalWrite(pin_gsm,LOW); //Enable GSM mode
digitalWrite(pin_gps,HIGH); //Disable GPS mode
delay(2000);
}


/* UTILISTIES */


/* GPS */

boolean gps_power(void) //turn on GPS power supply
{
/*
Serial.println("AT");
delay(2000);
*/

boolean gpspwr = false;
boolean gpsrst = false;


if(sendATcommand("AT+CGPSPWR=1","OK",2000))
{
gpspwr = true;
if(debug)
{
Serial.println("turn on GPS power supply => OK");
}
}
else
{
if(debug)
{
Serial.println("turn on GPS power supply => KO");
}
};
//delay(1000);

if(sendATcommand("AT+CGPSRST=1","OK",2000))
{
gpsrst = true;
if(debug)
{
Serial.println("reset GPS in autonomy mode => OK");
}
}
else
{
if(debug)
{
Serial.println("reset GPS in autonomy mode => KO");
}
}; //reset GPS in autonomy mode

delay(1000);

if(gpspwr && gpsrst)
{
return true;
}else
{
return false;
}
}

在 read_AT_string 处,第一个 if(Serial.peek()==13) 总是返回 false。

打印了 1,但没有打印 '13',那我想

if(Serial.peek()==13)

返回错误

这是 5 秒内打印的内容

AT DISPLAY BUFFER:
1
1
1
1
1
1
1
1
1
[...] // It prints 1 until now
1

END DISPLAY BUFFER

关于c++ - 如何显示/保存返回 AT 命令的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com