我的ESP8266(Wemos D1 mini)在这里遇到一个奇怪的问题。
我正在编写一个用于使用nRF24L01发送器构建网络的库。
我定义了一个gloubl 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;
}
非常感谢您抽出宝贵的时间为我提供帮助。
迎接舒勒伯恩德
最佳答案
ESP8266只能从正确对齐的地址(即多个数据类型的地址)中读取float
s(以及16位int
s,32位int
s,double
s)。由于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
只是摆脱最后一行。