Chat freely about anything...

User avatar
By martinayotte
#25574 You're welcome, Scargill ! ;)
Of course, using "packed" attribute brings some penalties into the code, because it needs to fit those 8 bits back into the 16 bits (or 32 bits, depending of processor), but if the goal isn't code speed, but network packet size efficiency, that's the way to do it. (BTW, the is also the "align" attribute which can be relevant in some cases)
User avatar
By scargill
#25587 In this case it is compatibility - the function copies an entire struct just as a block of memory from one processor to the other and hence obviously the size and relevant positions of the internal components within the struct need to be identical.

Thanks.

Pete.
User avatar
By eriksl
#25636 If you're going to pass data around between processors, epecially if both have a different architecture, don't use a binary representation (i.e. a struct). This is only one of the hazards you will face. Another one is byte and bit ordering, different optimisations on different platforms, etc.

Alternatives:
- use plain ASCII
- use an array of bytes (which most of the time will work) and fill of them with explicit byte operations, something like this:

uint8_t exchange_packet[16];
time_t ticks = time(0);
exchange_packet[0] = (ticks & 0xff000000) >> 24;
exchange_packet[1] = (ticks & 0x00ff0000) >> 16;
exchange_packet[2] = (ticks & 0x0000ff00) >> 8;
exchange_packet[3] = (ticks & 0x000000ff) >> 0;
User avatar
By scargill
#25639 Well as it happens, after doing all of that I decided on a better way. It just so happens I'd written a half decent parser for the ESP in C and thought I'd give it a try on the Arduino - it worked - so now I'm sending things like {fred:1; brian:45} - however I got a working version on the blog thanks to help here and elsewhere and it's there if anyone needs it.