@SirMichael
The issue you're encountering is due to differences in data alignment and padding between the Arduino NANO (AVR architecture) and the M5Stack Core2 (ESP32 architecture). Here's why the sizeof your structure differs and how to fix it:
Why the Size Differs
Data Alignment:
The ESP32 (Core2) aligns data to 4-byte boundaries by default for performance reasons, while the AVR (NANO) does not.
This causes the compiler to insert padding bytes between struct members to meet alignment requirements.
Double Precision (double):
On AVR, double is typically 4 bytes (same as float).
On ESP32, double is 8 bytes (true IEEE 754 double precision).
This alone explains part of the size difference.
Padding:
The ESP32 compiler may insert padding after uint16_t members to align the double to an 8-byte boundary.
How to Fix It
To ensure the struct has the same size and layout on both platforms, you can:
Use #pragma pack:
Force the compiler to use 1-byte alignment (no padding) for the struct:#pragma pack(push, 1) // Disable padding
struct AmpStruct {
uint8_t bMode;
uint16_t iBand;
float dVolts; // Use float instead of double for consistency
uint16_t iAmpTemp;
uint16_t iFanOutput;
uint16_t iFwdPower;
uint16_t iRefPower;
};
#pragma pack(pop) // Restore default alignment
Note: Replace double with float to ensure consistency (4 bytes on both platforms).
Manually Pad the Struct:
If you must use double, manually add padding to match the ESP32 layout:struct AmpStruct {
uint8_t bMode;
uint8_t _pad1; // Manual padding
uint16_t iBand;
double dVolts;
uint16_t iAmpTemp;
uint16_t iFanOutput;
uint16_t iFwdPower;
uint16_t iRefPower;
};
Use Serialization:
Instead of sending the raw struct, serialize it into a byte array with fixed sizes:uint8_t buffer[24]; // Adjust size as needed
buffer[0] = AmpData.bMode;
memcpy(&buffer[1], &AmpData.iBand, 2);
memcpy(&buffer[3], &AmpData.dVolts, 8); // Or use float
// ... repeat for other members
Serial.write(buffer, sizeof(buffer));
Key Takeaways
Use float instead of double unless you need the extra precision.
Force 1-byte alignment with #pragma pack to avoid padding.
Test the struct size on both platforms after changes:Serial.print("Size of AmpStruct: ");
Serial.println(sizeof(AmpStruct));