<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SizeOf Structure M5Stack Core2 vs NANO]]></title><description><![CDATA[<p dir="auto">I've been struggling with trying to pass a structure between an Arduino NANO and an M5Stack Core2 through Serial Comms.</p>
<p dir="auto">I can get the structure to pass correctly between two Arduino UNOs or NANOs, but when I try to get it to use on my project between a NANO and the M5Stack Core2, it fails. (Serial comms passes strings just fine!)</p>
<p dir="auto">I finally figured out that the sizeof the Structure is reported as <strong>15 bytes</strong> on the NANO and <strong>24 bytes</strong> on the M5Stack Core2.</p>
<p dir="auto">I changed my declarations from:<br />
<strong>byte to uint8_t<br />
int to uint32_t</strong><br />
on the NANO and M5.</p>
<p dir="auto">I do also have a double in the structure:</p>
<pre><code>//Use the same struct for comms both ways!
struct AmpStruct {
  uint8_t bMode;
  uint16_t iBand;
  double dVolts;
  uint16_t iAmpTemp;
  uint16_t iFanOutput;
  uint16_t iFwdPower;
  uint16_t iRefPower;
};
//Initialize the values to create the usable structure
AmpStruct AmpData = {0, 0, 0, 0, 0, 0, 0};
</code></pre>
<p dir="auto">Can anyone tell me what the difference is?  Is there a way to make the structure the same between the two?  There has to be way to declare the variables so they take up the same number of bytes on the two platorms.</p>
<p dir="auto">I do have the comms working by passing strings, but I'd like to use the Structure within the communications.</p>
<p dir="auto">Sir Michael</p>
]]></description><link>https://community.m5stack.com/topic/7632/sizeof-structure-m5stack-core2-vs-nano</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 07:42:09 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/7632.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 21 Jun 2025 22:28:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SizeOf Structure M5Stack Core2 vs NANO on Wed, 25 Jun 2025 12:58:12 GMT]]></title><description><![CDATA[<p dir="auto">yuyun2000,</p>
<p dir="auto">I wasn't able to get my structure to pass correctly.  I ended up not only changing the double to a float, but I also changed it to another uint16_t.  There was still a 1 byte difference in the sizes of the structure (Nano was 13, M5Stack was 14).  I added the "uint8_t pad1" to the Nano so it was then 14 also.  The comms routines that I have now shows that they both return a 0 (successful) but the data doesn't come across like it does with the Nano to Nano testing.</p>
<p dir="auto">I've decided to give up on sending the Structure and just assemble the data into a String Array and send that.   I have it working as a String, but I'm changing it over to use a null terminated Character Array.</p>
<p dir="auto">Thanks Anyway, it was fun trying!</p>
<p dir="auto">Sir Michael</p>
]]></description><link>https://community.m5stack.com/post/29330</link><guid isPermaLink="true">https://community.m5stack.com/post/29330</guid><dc:creator><![CDATA[SirMichael]]></dc:creator><pubDate>Wed, 25 Jun 2025 12:58:12 GMT</pubDate></item><item><title><![CDATA[Reply to SizeOf Structure M5Stack Core2 vs NANO on Tue, 24 Jun 2025 11:52:57 GMT]]></title><description><![CDATA[<p dir="auto">yuyun2000,</p>
<p dir="auto">Thank you for the reply.  This finally explains why I've been having so much trouble.</p>
<p dir="auto">I don't need the precision of the double, so it's not a problem to change to a float.  I have tried removing the double and inserted another uint16_t, but then I had a 1 byte difference.  That was a close as I had gotten so far.</p>
<p dir="auto">I'll have to study your response more and try to understand it to get it to work, hopefully today.<br />
Again, my sincere thanks...</p>
<p dir="auto">Sir Michael</p>
]]></description><link>https://community.m5stack.com/post/29311</link><guid isPermaLink="true">https://community.m5stack.com/post/29311</guid><dc:creator><![CDATA[SirMichael]]></dc:creator><pubDate>Tue, 24 Jun 2025 11:52:57 GMT</pubDate></item><item><title><![CDATA[Reply to SizeOf Structure M5Stack Core2 vs NANO on Mon, 23 Jun 2025 08:04:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sirmichael" aria-label="Profile: SirMichael">@<bdi>SirMichael</bdi></a><br />
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:</p>
<p dir="auto">Why the Size Differs</p>
<p dir="auto">Data Alignment:</p>
<p dir="auto">The ESP32 (Core2) aligns data to 4-byte boundaries by default for performance reasons, while the AVR (NANO) does not.<br />
This causes the compiler to insert padding bytes between struct members to meet alignment requirements.</p>
<p dir="auto">Double Precision (double):</p>
<p dir="auto">On AVR, double is typically 4 bytes (same as float).<br />
On ESP32, double is 8 bytes (true IEEE 754 double precision).<br />
This alone explains part of the size difference.</p>
<p dir="auto">Padding:</p>
<p dir="auto">The ESP32 compiler may insert padding after uint16_t members to align the double to an 8-byte boundary.</p>
<p dir="auto">How to Fix It<br />
To ensure the struct has the same size and layout on both platforms, you can:</p>
<p dir="auto">Use #pragma pack:</p>
<p dir="auto">Force the compiler to use 1-byte alignment (no padding) for the struct:#pragma pack(push, 1)  // Disable padding<br />
struct AmpStruct {<br />
uint8_t bMode;<br />
uint16_t iBand;<br />
float dVolts;       // Use float instead of double for consistency<br />
uint16_t iAmpTemp;<br />
uint16_t iFanOutput;<br />
uint16_t iFwdPower;<br />
uint16_t iRefPower;<br />
};<br />
#pragma pack(pop)     // Restore default alignment</p>
<p dir="auto">Note: Replace double with float to ensure consistency (4 bytes on both platforms).</p>
<p dir="auto">Manually Pad the Struct:</p>
<p dir="auto">If you must use double, manually add padding to match the ESP32 layout:struct AmpStruct {<br />
uint8_t bMode;<br />
uint8_t _pad1;      // Manual padding<br />
uint16_t iBand;<br />
double dVolts;<br />
uint16_t iAmpTemp;<br />
uint16_t iFanOutput;<br />
uint16_t iFwdPower;<br />
uint16_t iRefPower;<br />
};</p>
<p dir="auto">Use Serialization:</p>
<p dir="auto">Instead of sending the raw struct, serialize it into a byte array with fixed sizes:uint8_t buffer[24];  // Adjust size as needed<br />
buffer[0] = AmpData.bMode;<br />
memcpy(&amp;buffer[1], &amp;AmpData.iBand, 2);<br />
memcpy(&amp;buffer[3], &amp;AmpData.dVolts, 8);  // Or use float<br />
// ... repeat for other members<br />
Serial.write(buffer, sizeof(buffer));</p>
<p dir="auto">Key Takeaways</p>
<p dir="auto">Use float instead of double unless you need the extra precision.<br />
Force 1-byte alignment with #pragma pack to avoid padding.<br />
Test the struct size on both platforms after changes:Serial.print("Size of AmpStruct: ");<br />
Serial.println(sizeof(AmpStruct));</p>
<p dir="auto"><a href="https://chat.m5stack.com/" target="_blank" rel="noopener noreferrer nofollow ugc">https://chat.m5stack.com/</a></p>
]]></description><link>https://community.m5stack.com/post/29306</link><guid isPermaLink="true">https://community.m5stack.com/post/29306</guid><dc:creator><![CDATA[yuyun2000]]></dc:creator><pubDate>Mon, 23 Jun 2025 08:04:57 GMT</pubDate></item></channel></rss>