ESP8266 尝试从字节数组读取浮点数时出现异常

ESP8266 Exception while trying to read float from byte array

我的 ESP8266 (Wemos D1 mini) 有一个奇怪的问题。 我正在编写一个用于使用 nRF24L01 发射器构建网络的库。

我定义了一个 gloabl uint8_t arry 代表我的接收缓冲区。 当从其他设备接收到传输时,数组会被填满。填充数组后,我开始解析缓冲区,以确定是否存在有效的 Header/Frame。 header 包含一些典型的数据,如发件人、校验和...如果 header 有效,则会解析帧的数据。 数据的结构总是相同的。可以有 x 个所谓的 DataTriples。 DataTriple 是键 (cstring)、值类型 (uint8_t) 和值本身的组合。值的类型可以是基本类型,如 INT8、UINT8 或 FLOAT 或 STRING。

现在很奇怪。

为了节省内存,我没有将缓冲区中的值复制到专用变量中。我总是指向缓冲区中的位置。当我想读取一个值时,我会执行一个简单的指针转换并读出这些值。 如果该值是一个字符串或一个简单的 1 字节长 uint8_t,这将非常有效。但是如果它是一个浮点数或一个 uint32_t 我得到一个 LoadStoreAlignmentCause Excetion 告诉我,我想访问未对齐的内存。 怎么会这样?指针明确指向正确的缓冲区位置。我检查了这个,因为如果我创建一个新的 float 变量并执行从缓冲区到变量地址的 memcpy,一切都很好。但是我创建了一个浮点指针并指向缓冲区中的位置,我得到了这个异常。

谁能告诉我原因或我做错了什么?

这里有一些代码片段,以便更好地理解。

bool SBSmartHomeBasicDevice::parseDataTriples() {
if (_IncommingTransmission) {
    uint8_t* iCurrentPos;
    uint8_t iDataTripleCount = 0;
    // _LastReceivedFrame.Payload points to the receive buffer
    for (iCurrentPos = _LastReceivedFrame.Payload; iCurrentPos < (_LastReceivedFrame.Payload + _LastReceivedFrame.Header.PayloadSize); iCurrentPos) {
        // Catch the type of the triple
        uint8_t type = *((uint8_t*)iCurrentPos);
        // increase the pointer about uint8_t size
        iCurrentPos += sizeof(uint8_t);
        // Catch the key of the triple
        char* key;
        key = (char*)(iCurrentPos);
        // increase the pointer about strlen of key + 1 
        iCurrentPos += strlen((char*)iCurrentPos) + 1;
        // catch the value
        void* value = (void*)(iCurrentPos);
        _LastReceivedTriples[iDataTripleCount].setType(type);
        _LastReceivedTriples[iDataTripleCount].setKey(key);


        // ***
        // Here starts the interesting part 
        // ***
        float* fTmp = (float*)iCurrentPos;
        // The following works perfect
        // ****
        float f;
        memcpy(&f, fTmp, sizeof(float));
        Serial.println(f);
        // ****
        // The following causes an exception
        Serial.println(*fTmp);
        // *** EXCEPTION ***


        _LastReceivedTriples[iDataTripleCount].setValue(iCurrentPos);
        // now increase the pointer
        switch (type) {
        case SMART_HOME_VALUE_TYPE_STRING:
            iCurrentPos += strlen((char*)iCurrentPos) + 1;
            break;
        case SMART_HOME_VALUE_TYPE_INT8:
        case SMART_HOME_VALUE_TYPE_UINT8:
            iCurrentPos += sizeof(int8_t);
            break;
        case SMART_HOME_VALUE_TYPE_INT16:
        case SMART_HOME_VALUE_TYPE_UINT16:
            iCurrentPos += sizeof(int16_t);
            break;
        case SMART_HOME_VALUE_TYPE_FLOAT:
            iCurrentPos += sizeof(float);
            break;
        case SMART_HOME_VALUE_TYPE_INT32:
        case SMART_HOME_VALUE_TYPE_UINT32:
            iCurrentPos += sizeof(int32_t);
            break;
        default:
            Serial.println("parseDataTriples(): Unknown ValueType");
        }
        _LastReceivedTriples[iDataTripleCount].print();
        iDataTripleCount++;
        _LastReceivedTriplesCount = iDataTripleCount;
    } 
    return true;
}
return false;

}

非常感谢您花时间帮助我。

问候 Schullebernd

ESP8266 只能从正确对齐的地址读取 floats(和 16 位 ints、32 位 ints、doubles),即地址是数据类型的倍数。由于 float 是 4 个字节长,它只能从 4 的倍数的地址读取。否则你会得到提到的异常。

您正在使用 uint8_t 数组。 uint8_t 是 1 个字节长,因此不需要并且可能没有以任何方式对齐。该数组可能从地址 170001 开始。如果您现在在数组中的位置 40 处有一个 float 值,您的代码将尝试从地址 170041 读取一个 float 值,该值未正确对齐并导致异常。

解决方案是首先将 float 值的字节复制到本地的、正确对齐的变量中。这就是您所做的,但随后您在未对齐的位置再次访问它并得到一个异常。

    float* fTmp = (float*)iCurrentPos; // does not copy; takes a pointer to the orignal location

    float f;
    memcpy(&f, fTmp, sizeof(float)); // copies data to aligned variable

    Serial.println(f); // prints value from aligned variable

    Serial.println(*fTmp); // prints value for original, non-aligned location

去掉最后一行。