Using two boards of similar architecture I would like to send the contents of a struct from the sending board to the receiving board. I am using UART to transfer.
My plan was to populate an array with the starting structs data. Transfer that, than un-package the array onto the receiving boards struct.
I'm not sure this is a possible option. Unpacking the array seems sort of rigid, as I would be assigning each struct variable to the array index. It might work but I'm not really convinced I should do it this way.
Here's what my sending sketch looks like.
Send:
typedef struct NWK_DataReq_t
{
uint8_t state;
uint8_t options;
} NWK_DataReq_t;
//Instance struct
NWK_DataReq_t dataRequest;
void setup()
{
//Give it some values
dataRequest.state = 2;
dataRequest.options = 4;
Serial.begin(9600);
}
void loop()
{
//set variable array to struct length
uint8_t payload[sizeof(dataRequest)];
//copy struct to variable array
memcpy(payload,&dataRequest,sizeof(dataRequest));
//send each item of struct, now contained in payload array
for(int i=0;i < sizeof(payload);i++)
{
//Serial.print(i);
//Serial.print(":");
Serial.println(payload[i]);
}
Serial.println("");
delay(1000);
}
Receive:
typedef struct NWK_DataReq_t
{
uint8_t state;
uint8_t options;
} NWK_DataReq_t;
//Instance struct
NWK_DataReq_t dataRequest;
uint8_t incomingPayload[sizeof(dataRequest)];
void setup()
{
Serial.begin(9600);
Serial.print("Recieve started");
}
void loop()
{
if (Serial.available())
{
//add incoming data to array
for(int i=0;i<sizeof(dataRequest);i++)
{
uint8_t incomingByte = Serial.read();
incomingPayload[i] = incomingByte;
}
}
//set array values to structs
for (int i= 0;i < sizeof(dataRequest);i++)
{
structIndexPosition = incomingPayload[i];
}
}
structIndexPosition is not attached as I wasn't sure how I might index the struct, or if this is an okay approach.