Usings Unions and be able to change individual members

Hi,
I'm working with structs and unions to easily get data from a serial port and translated it to understandable data. Ihave made a union to do this:

union WH24Pconvertor
{
  struct
  {
    byte FC: 8;
    byte SC: 8;
    byte DIR: 8;
    byte DIR8: 1;
    byte FIX1: 1;
    byte FIX2: 1;
    byte WSP8: 1;
    byte LowBattery: 1;
    byte TMP8: 3;
    byte TMP: 8;
    byte HM: 8;
    byte WSP: 8;
    byte GUST: 8;
    byte RAIN8: 8;
    byte RAIN: 8;
    byte UV8: 8;
    byte UV: 8;
    byte LIGHT16: 8;
    byte LIGHT8: 8;
    byte LIGHT: 8;
    byte CRC: 8;
    byte CHKSUM: 8;
    byte PRESSURE16: 8;
    byte PRESSURE8: 8;
    byte PRESSURE: 8;
    byte CHKSUM2: 8;
  } WH24PDataFormat;
  uint8_t data[21];
};

I have just 2 problems. I can't change just 1 variable (GUST for instance) or all data will get corrupt (Union). Must I predefine a struct first and make a union of this predefined struct and data[21] for instance? So I can only change the struct or data? I need to declare an extra struct for this in my routines, but the ESP8266 has enough memory probably.
My second problem I want to strip some variables from this struct and repack it in a smaller struct. So a struct without FC(begin byte 0x24), SC, CRC, CHKSUM and CHKSUM2. Can I make some kind of union that puts the exact named variables in the other exactly same variables but in another struct (without CRC and CHKSUM)? So stripping variables from the struct above. I will write this data to a SD card.
Thanks or reading my message. I hope you can help me and make some suggestions.

Best regards,
Mark

I think the simplest way to do this will be just a copy desired members to the new structure.

Why do you think that? Please provide an example that demonstrates it.

This is correct. It it not allowed to change values in the struct and read read this change back from data or vice versa.

See this page for a comprehensive explanation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.