我遇到了一个奇怪的行为。我正在使用 ESP8266 arduino SPIFFS 来存储配置设置。这是mycode的相关部分;
void loop()
{
handleUartRxOk();
}
void handleUartRxOk() {
String cmd;
char charBuff[3200];
char char_print[50];
static bool terminatorReceived = false;
char incomingChar = 0; // for incoming serial data
if (Serial.available()) {
incomingChar = Serial.read();
saveChar(incomingChar);
if (incomingChar == '\r') {
terminatorReceived = true;
}
if (terminatorReceived) {
buffer[buffer_index - 1] = '\0';
cmd = String(buffer);
if (cmd == "XXX") {
ConfigSettings.ssid = "SSID_XX";
ConfigSettings.password = "PASSWORD_XX";
saveConfig();
}
buffer_index = 0;
terminatorReceived = false;
}
}
}
在上面的代码中,UART 将收到一个命令“XXX\r”,然后运行 saveConfig() 将配置参数 ssid 和参数保存到 SPIFSS。在我添加更多完全不相关的代码之前,这段代码工作得很好。
这就是新代码的样子。
void handleUartRxOk() {
String cmd;
char charBuff[3200];
char char_print[50];
static bool terminatorReceived = false;
char incomingChar = 0; // for incoming serial data
if (Serial.available()) {
incomingChar = Serial.read();
saveChar(incomingChar);
if (incomingChar == '\r') {
terminatorReceived = true;
}
if (terminatorReceived) {
buffer[buffer_index - 1] = '\0';
cmd = String(buffer);
if (cmd == "XXX") {
ConfigSettings.ssid = "SSID_XX";
ConfigSettings.password = "PASSWORD_XX";
saveConfig();
}
//Why does adding this else statement cause saveConfig() to crash when run?
else {
strcat(charBuff, cmd.c_str());
}
buffer_index = 0;
terminatorReceived = false;
}
}
}
添加额外的 else 子句后,向 UART 发送“XXX\r”并导致 saveConfig() 将导致异常错误。这令人费解,因为新代码甚至没有运行。
异常错误如下;
Exception (3):
epc1=0x401002f0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x400072f6 depc=0x00000
000
ctx: sys
sp: 3fff06b0 end: 3fffffb0 offset: 01a0