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

时间:2018-01-16 15:37:56

标签: c++ arrays json struct atmega

我有3个完全相同的PCB可容纳ATmega2560 MCU。 我使用Atmel Studio 7进行保险丝和闪烁HEX。 我使用Visual Studio与Visual Micro插件混合使用C和Arduino编码。

我有一个struct数组定义。我将数据从由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次。 最后,它被写入非易失性存储器。

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; 
}

到目前为止一切顺利。到目前为止,我对此很满意。 这是&#34; &#34;;

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

结果如下:

通过for循环调试,我得到了以下内容。这种情况仅发生在一些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

我希望我能解释得足以告诉你出了什么问题?

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

1 个答案:

答案 0 :(得分:0)

最后,在将代码(近2000行)缩小到某一点之后,我可以解决它。还要感谢@Andy帮助我继续关注这个问题。

Else的{​​{1}}内,Loop再次从RefreshScreen();重新加载记录,这覆盖了结构数组EEPROM和下一轮JSON数据在那里继续存在。

我已从recordsOut删除了EEPROM读取功能,它已开始工作。