Transfer a struct's data to an external struct via serial? 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,&amp;dataRequest,sizeof(dataRequest));

  //send each item of struct, now contained in payload array
  for(int i=0;i &lt; 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&lt;sizeof(dataRequest);i++)
    {
      uint8_t incomingByte = Serial.read();
      incomingPayload[i] = incomingByte;
    }
  }

  //set array values to structs
  for (int i= 0;i &lt; 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.
I think you are over-complicating it to send two bytes. Just send them as straight ASCII with a newline after, and decode it on the receiving end.

eg.

42,87


However if the question was just an example of something much more complex, then you may need to do more work. You need to have the receiving end know when a packet of data starts, and ends, and preferably have some validation that it wasn't corrupted.  If you want to pursue that idea, see my thread about sending data via RS485 which has a library that does that sort of thing.