I have a structure that holds a bunch of data, mostly unsigned ints and unsigned longs. I'd like to send all of this to another arduino.
Does anyone know if the arduino compiler guarantees the memory layout to be the same? AFAIK C (at least C99 compliant C) would but I don't know about C++.
In other words, if I declare a struct:
struct
{
unsigned long lastUpdated;
unsigned int AHin, AHout;
unsigned long Amsecin, Amsecout;
unsigned int Vmin, Vmax;
unsigned int Vavg;
unsigned int Imin, Imax;
unsigned char Tmin, Tmax;
unsigned char chargeMin, chargeMax;
unsigned long CnowmSec;
unsigned int socLoad, socCalc, socSense;
} history;
can I send this out a comm port as a binary blob and receive it on the other end as a binary blob and stick it into an identical structure? (With appropriate error checking and such...)
You can't just send a struct "through a comm port". Serial data can only be sent out byte by byte, so if you want to send an entire struct, you should modify the struct to use a union with the appropriate sized byte array.
That's the one I'm using for my RS485 protocol. Pretty neat and easy to use. Lots easier than modbus.
The alignment is guaranteed in C99 spec. But I'm not sure about C++.
Arrch:
Thanks for the union idea. I have not thought this all the way through and I'll try that. I was going to just stick a pointer to the struct and a "sizeof" for the length to send.
I was going to just stick a pointer to the struct and a "sizeof" for the length to send.
That'll work too, you just need to cast the pointer appropriately. It would be nice to get a bit of error handling though - start and end of packet, CRC or checksum.