Issue with ESP866 (ESP-12) and Data Structure.
Posted: Wed Sep 28, 2016 8:44 am
Hello,
While transferring Data using the RFM69 library I was confronted to a strange ESP8266 behaviour while converting buffered data to a data structure using various data types.
The simplest way I have found to reproduce the problem is the attached script.
This one is mapping an array of bytes:
volatile byte radioBuf[] ={0x40, 0x41,0x42, 0x43,0x44,0x45,0x46}; // Byte ShortInt "Long"Int
into a data structure (byte, short integer, long integer):
typedef struct {
byte byte0;
short int short1;
long int long2;
} Payload;
Payload theData;
I use the type "short" and "long" integer to maintain the compatibility with Arduino, where an integer is actualy only 2 bytes ( 4 bytes for ESP8266).
So the print of script gives with a regular Arduino:
radioBuf dump in HEX: 40414243444546
byte0= 64 and byte0 in HEX 40
short1= 16961 and short1 in HEX 4241
long2= 1178944579 and long2 in HEX 46454443
Which is obviously correct
Now with a Generic ESP8266 the result is:
radioBuf dump in HEX: 40414243444546
byte0= 64 and byte0 in HEX 40
short1= 17218 and short1 in HEX 4342
long2= 877020484 and long2 in HEX 34464544
Only the fist data is correctly aligned, after this bytes boundaries are lost
This problem occurs only with mixed data types are used and becomes more complicated while introducing char and float types or mixing short and long integer.
Does someone can explain this issue, and of course what should be the solution.
Robert
SCRIPT
While transferring Data using the RFM69 library I was confronted to a strange ESP8266 behaviour while converting buffered data to a data structure using various data types.
The simplest way I have found to reproduce the problem is the attached script.
This one is mapping an array of bytes:
volatile byte radioBuf[] ={0x40, 0x41,0x42, 0x43,0x44,0x45,0x46}; // Byte ShortInt "Long"Int
into a data structure (byte, short integer, long integer):
typedef struct {
byte byte0;
short int short1;
long int long2;
} Payload;
Payload theData;
I use the type "short" and "long" integer to maintain the compatibility with Arduino, where an integer is actualy only 2 bytes ( 4 bytes for ESP8266).
So the print of script gives with a regular Arduino:
radioBuf dump in HEX: 40414243444546
byte0= 64 and byte0 in HEX 40
short1= 16961 and short1 in HEX 4241
long2= 1178944579 and long2 in HEX 46454443
Which is obviously correct
Now with a Generic ESP8266 the result is:
radioBuf dump in HEX: 40414243444546
byte0= 64 and byte0 in HEX 40
short1= 17218 and short1 in HEX 4342
long2= 877020484 and long2 in HEX 34464544
Only the fist data is correctly aligned, after this bytes boundaries are lost
This problem occurs only with mixed data types are used and becomes more complicated while introducing char and float types or mixing short and long integer.
Does someone can explain this issue, and of course what should be the solution.
Robert
SCRIPT
Code: Select all
volatile byte radioBuf[] ={0x40, 0x41,0x42, 0x43,0x44,0x45,0x46}; // Byte ShortInt "Long"Int
typedef struct {
byte byte0;
short int short1;
long int long2;
} Payload;
Payload theData;
void setup ()
{
Serial.begin(115200);
theData = *(Payload*)radioBuf;
}
void loop ()
{
Serial.print ("radioBuf dump in HEX: ");
for (int i = 0; i< sizeof(radioBuf); i++)
{
Serial.print(radioBuf[i],HEX);
}
Serial.println();
Serial.print("byte0= "); Serial.print (theData.byte0), Serial.print(" and byte0 in HEX "); Serial.println (theData.byte0,HEX);
Serial.print("short1= "); Serial.print (theData.short1), Serial.print(" and short1 in HEX ");Serial.println(theData.short1,HEX);
Serial.print("long2= "); Serial.print (theData.long2), Serial.print(" and long2 in HEX ");Serial.println(theData.long2,HEX);
Serial.println ();
delay (1000);
}