You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在onGetResult()中识别EEPROM芯片索引并获取双芯片信息?

解决方法

针对你遇到的WB_RES::EepromInfo未携带芯片索引的问题,这里有几个实用的解决思路:

1. 利用请求上下文传递索引

异步请求发起时,可以通过AsyncRequestOptions的context字段附带芯片索引,在回调中取出关联:

// 发起请求时,将芯片索引存入上下文
AsyncRequestOptions reqOptions;
reqOptions.context = reinterpret_cast<void*>(eepromIndex); // eepromIndex为0或1
asyncGet(WB_RES::LOCAL::COMPONENT_EEPROM_EEPROMINDEX_INFO(), reqOptions, eepromIndex);

在onGetResult回调中还原索引:

void onGetResult(const WB_RES::RequestId& requestId, const WB_RES::Result& result) override {
    if (result.isSuccess()) {
        // 从上下文取出芯片索引
        int chipIndex = static_cast<int>(reinterpret_cast<intptr_t>(result.getRequestOptions().context));
        const WB_RES::EepromInfo* eepromData = result.getPayload<WB_RES::EepromInfo>();
        
        // 根据索引处理对应芯片的读写逻辑
        if (chipIndex == 0) {
            // 适配芯片0的容量做读写
        } else if (chipIndex == 1) {
            // 适配芯片1的容量做读写
        }
    }
}

2. 维护请求ID与索引的映射表

如果上下文传递不可行,可以在发起请求时记录RequestId和芯片索引的对应关系:

// 类成员或全局变量,保存请求ID到芯片索引的映射
std::unordered_map<WB_RES::RequestId, int> reqChipMap;

// 发起请求时记录映射
WB_RES::RequestId reqId = asyncGet(WB_RES::LOCAL::COMPONENT_EEPROM_EEPROMINDEX_INFO(), AsyncRequestOptions::Empty, eepromIndex);
reqChipMap[reqId] = eepromIndex;

回调中通过请求ID查找索引:

void onGetResult(const WB_RES::RequestId& requestId, const WB_RES::Result& result) override {
    auto mapIter = reqChipMap.find(requestId);
    if (mapIter != reqChipMap.end()) {
        int chipIndex = mapIter->second;
        reqChipMap.erase(mapIter); // 处理完成后清理映射
        
        if (result.isSuccess()) {
            const WB_RES::EepromInfo* eepromData = result.getPayload<WB_RES::EepromInfo>();
            // 根据chipIndex调整读写方法
        }
    }
}

3. 检查EepromInfo结构是否有隐藏索引字段

仔细查看WB_RES::EepromInfo的定义(比如头文件),部分API会将设备索引隐藏在结构体的某个成员中,例如:

// 示例EepromInfo结构(实际以你的API定义为准)
struct EepromInfo {
    uint32_t capacity;
    uint8_t deviceIndex; // 可能存在的芯片索引字段
    // 其他成员...
};

如果存在这类字段,直接读取eepromData->deviceIndex即可获取芯片索引。


内容的提问来源于stack exchange,提问作者Shiva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:35:18