gpt4 book ai didi

c++ - 某些 ATmega 2560 的第一个索引中的结构数组数据被重置

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:35 26 4
gpt4 key购买 nike

我有 3 个完全相同的 PCB 容纳 ATmega2560 MCU。我将 Atmel Studio 7 用于 fuse 和闪烁 HEX。我正在使用 Visual Studio 和 Visual Micro 插件,混合使用 C 和 Arduino 编码。

我有一个结构数组定义。我将数据从 MCU 串行端口提供的 json 填充到这个结构数组中。 Json 解析器是 ArduinoJson。

定义:

#define MAX_RECORDS 20 //Max number of records in the struct

typedef struct Record {
uint8_t id;//unique number to define data sequence
uint8_t sec;
uint8_t obj;
uint16_t xs;
uint16_t ys;
uint16_t xe;
uint16_t ye;
uint8_t clr;
uint8_t tsz;
uint8_t tid;
} tRecord;

struct Record recordsOut[MAX_RECORDS];

主循环:

void loop() {
if (IsNetworkAlertState == 0) {
if (setupProg[0].gid == 255 || setupProg[0].bnr == 255 || recordsOut[0].id == 255) {
if (IsNoSetupAlertState == 0) {
IsNoSetupAlertState = 1;
}
}
else {
if (IsNoSetupAlertState == 1) {
IsNoSetupAlertState = 0; // Nothing to alert
RefreshScreen(); //Redraw data on screen after the setup is done!
}
}
}
}

从串行接收的示例数据:

const char* json = "{\"id\":1,\"sec\":1,\"obj\":1,\"xs\":1,\"ys\":0,\"xe\":158,\"ye\":62,\"clr\":0,\"tsz\":0,\"tid\":-1}";

来电者:

为了存储所有数据,这可能会被调用 20 次。最后写入非 volatile 存储器。

ApplyDesignSettings(json);

存储传入数据的函数:

void ApplyDesignSettings(char buffer[]) {

const size_t bufferSize = JSON_OBJECT_SIZE(10) + 70;
DynamicJsonBuffer jsonBuffer(bufferSize);

if (IsDebugOn == 1) {
Serial.print("buffer:");
Serial.println(buffer);
}

JsonObject& root = jsonBuffer.parseObject(const_cast<char*>(buffer));
if (!root.success()) {
Serial.println("parseObject() failed#1");
return;
}

uint8_t id = root["id"];
uint8_t sec = root["sec"];
uint8_t obj = root["obj"];
uint16_t xs = root["xs"];
uint16_t ys = root["ys"];
uint16_t xe = root["xe"];
uint16_t ye = root["ye"];
uint8_t clr = root["clr"];
uint8_t tsz = root["tsz"];
uint8_t tid = root["tid"];

int ref = id;
int idx = ref - 1;
recordsOut[idx].id = id;
recordsOut[idx].sec = sec;
recordsOut[idx].obj = obj;
recordsOut[idx].xs = xs;
recordsOut[idx].ys = ys;
recordsOut[idx].xe = xe;
recordsOut[idx].ye = ye;
recordsOut[idx].clr = clr;
recordsOut[idx].tsz = tsz;
recordsOut[idx].tid = tid;
}

到目前为止一切顺利。到目前为止我对此很满意。这里是“但是”;

有时,在刷新一些 ATmega2560 并推送上述数据后,它会丢失/覆盖/删除 struct 数组的第一个索引,我发现它是空的 (255)尽管其余记录都很好。

这是结果:

通过 for-loop 调试,我得到了下面的结果。这只发生在某些 ATmega2560 上。

(注意:在 ID:8(索引 7)之后都是空的,这在这个给定的样本中是预期的)

========================================
Design data in non-volatile memory:
========================================
id:255, sec:255, obj:255
id:2, sec:1, obj:2
id:3, sec:1, obj:3
id:4, sec:1, obj:3
id:5, sec:2, obj:1
id:6, sec:2, obj:2
id:7, sec:2, obj:3
id:8, sec:2, obj:3
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
id:255, sec:255, obj:255
========================================

一般第一个索引应该是这样的;

id:1, sec:1, obj:1

我希望我能解释清楚,让您知道哪里出了问题?

感谢您从现在开始的投入。

最佳答案

最终在将代码(将近 2000 行)缩小到某个点后,我可以解决它。还要感谢@Andy 帮助我继续在这个问题上四处寻找。

LoopElse 中,RefreshScreen(); 再次从 EEPROM 重新加载 recordsOut,这正在覆盖 struct array recordsOut 和下一轮 JSON,数据从那里继续。

我已经从 RefreshScreen(); 中删除了 EEPROM 读取功能,它已经开始工作了。

关于c++ - 某些 ATmega 2560 的第一个索引中的结构数组数据被重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48284856/

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