0

我有一个这样的结构:

typedef struct {
    boolean isExists = false;

    uint16_t readBuffer[32]     = {0};
    uint16_t writeBuffer[13]    = {0};
    uint16_t ledBrgBuff[9]      = {0};

    uint16_t address    = 0x0000;
    uint16_t id         = 0x0000;
    uint16_t serial     = 0x0000;
    String   kind       = "RESERVED";
    String   name       = "RESERVED";

    uint16_t readNum        = 21;
    uint16_t writeNum       = 2;

    byte read_failCounter   = 0;
    byte write_failCounter  = 0;
    byte allowedFails       = 10;

    long lastWriteFailMSG   = 0;
    long lastReadFailMSG    = 0;
    boolean isTesting       = false;
    boolean criticalOff     = false;
    boolean onFault         = false;
    uint8_t data[RESPONSE_COUNT];
} expStruct;

我之前使用的是从 ram 动态分配的结构数组,如下所示:

expStruct * expanders[MAX_EXPANDER] EXT_RAM_ATTR; // Globally
    
void fillStructsTest(){
    // Fill all the array of structs with data from a json string from the filesystem
    JsonArray hwConf = doc.as<JsonArray>();

    memset(expanders, 0, sizeof(expanders)); // here I null all the struct in the array

    for(byte i = 0; i < hwConf.size(); i ++){
      JsonObject module = hwConf[i];
      expanders[expCounter]            = new expStruct;
      expanders[expCounter]->address   = module["address"].as<uint16_t>();
      expanders[expCounter]->id        = module["moduleid"].as<uint16_t>();
      expanders[expCounter]->serial    = module["serialnr"].as<uint16_t>();
      expanders[expCounter]->kind      = module["kind"].as<String>();
      expanders[expCounter]->name      = module["name"].as<String>();
      expCounter++;
    }
}

那工作得很好。我可以像这样使用我的结构数组:

void useStructTest(){
  if( expanders[0] != 0 ){
    expanders[0]->name = "Test";
    // I can reach every variable inside any of the structs in the array if the value on the index 
    // is not 0
  }
}

现在我想将这个结构数组放入我的 esp32 上的 PSRAM 中。我是这样接近它的:

typedef struct {
    boolean isExists = false;

    uint16_t readBuffer[32]     = {0};
    uint16_t writeBuffer[13]    = {0};
    uint16_t ledBrgBuff[9]      = {0};

    uint16_t address    = 0x0000;
    uint16_t id         = 0x0000;
    uint16_t serial     = 0x0000;
    String   kind       = "RESERVED";
    String   name       = "RESERVED";

    uint16_t readNum        = 21;
    uint16_t writeNum       = 2;

    byte read_failCounter   = 0;
    byte write_failCounter  = 0;
    byte allowedFails       = 10;

    long lastWriteFailMSG   = 0;
    long lastReadFailMSG    = 0;
    boolean isTesting       = false;
    boolean criticalOff     = false;
    boolean onFault         = false;
    uint8_t data[RESPONSE_COUNT];
} expStruct;
expStruct *expanders = (expStruct *) ps_malloc(MAX_EXPANDER * sizeof(expStruct));


void fill_PSRAM_StructsTest(){
        // Fill all the array of structs with data from a json string from the filesystem
        JsonArray hwConf = doc.as<JsonArray>();
    
        // memset(expanders, 0, sizeof(expanders)); // I can't do this now
    
        for(byte i = 0; i < hwConf.size(); i ++){
          JsonObject module = hwConf[i];
          // expanders[expCounter]            = new expStruct; // I can't do this now either.
          // also I must replace the -> with . because the compiler erroring me
          expanders[expCounter].address   = module["address"].as<uint16_t>();
          expanders[expCounter].id        = module["moduleid"].as<uint16_t>();
          expanders[expCounter].serial    = module["serialnr"].as<uint16_t>();
          expanders[expCounter].kind      = module["kind"].as<String>();
          expanders[expCounter].name      = module["name"].as<String>();
          expCounter++;
        }
}

在这里的某个地方,我的 ESP 在没有有意义的串行输出的情况下崩溃了,这就是:

Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x400ec9a7  PS      : 0x00060630  A0      : 0x80090915  A1      : 0x3ffdaee0
A2      : 0x00000000  A3      : 0x3f80afdc  A4      : 0x00000000  A5      : 0x3ffc25fc
A6      : 0x00000000  A7      : 0x3ffdaf64  A8      : 0x800ec9a2  A9      : 0x3ffdaeb0
A10     : 0x00000001  A11     : 0x3f40203a  A12     : 0x3ffdafd0  A13     : 0xf655c6f4
A14     : 0x3ffdadc0  A15     : 0x00000005  SAR     : 0x00000010  EXCCAUSE: 0x0000001d
EXCVADDR: 0x0000006e  LBEG    : 0x4008d6b1  LEND    : 0x4008d6c1  LCOUNT  : 0xfffffff7

ELF file SHA256: 0000000000000000

Backtrace: 0x400ec9a7:0x3ffdaee0 0x40090912:0x3ffdb000
  #0  0x400ec9a7:0x3ffdaee0 in mbusTask(void*) at lib/modbus-esp8266-3.0.6/src/Modbus.h:238
      (inlined by) mbusTask(void*) at src/Own_Headers/busCommunication.h:559
  #1  0x40090912:0x3ffdb000 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
ets Jul 29 2019 12:21:46

我应该在函数中再次分配每个索引,还是应该像以前一样正常使用它?

4

1 回答 1

0

对不起我的英语,你不能在 PSRAM 中使用字符串(动态)

您使用:字符名称[100];没有字符串!!!

和 ... 从 JSON 复制到 stuct :-)

strcpy(expanders[expCounter].name, module["name"].as <字符串> ().c_str());

于 2021-09-16T18:30:02.660 回答