This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Structures inside a union

I did read some threads on the forum but could not solve my problem.

I want to make a union of to data sets (a structure and an array).

My code is as follows:

#define COMMS_BUFFER_SIZE 80
#define PROTOCOL_HEADER_SIZE 5
#define PROTOCOL_DATA_SIZE (COMMS_BUFFER_SIZE - PROTOCOL_HEADER_SIZE)

typedef struct { u8 StartByte; u8 Route; u8 Command; u8 Length; u8 HCS; u8 Data[PROTOCOL_DATA_SIZE];
}TProtocol;

typedef union { TProtocol Protocol; u8 Data[COMMS_BUFFER_SIZE];
}TCommsBuffer;

extern TCommsBuffer CommsBuffer;

I want to access the contents of "TCommsBuffer" in the protocol format (strucrure) or as a array.

CommsBuffer.Data[x] = x;
CommsBuffer.Protocol.StartByte = x;

But it gives me problems.

When I read data into the Protocol structure, it repeats the contents of the 1st 5 bytes of the protocol structure into its data section.

Is this the correct way to define the union?

Parents
  • You don't understand my problem. I never said I want to access data with an offset. I said that I get repeated data inside my structure.

    Example:

    Say I do the following, fill the CommsBuffer.Data with sequential numbers:

    for (Loop = 0; Loop < COMMS_BUFFER_SIZE; Loop ++) { CommsBuffer.Data[Loop] = Loop;
    }

    Then if I read the contents of the CommsBuffer.Protocol structure

    P1 = CommsBuffer.Protocol.StartByte;
    P2 = CommsBuffer.Protocol.Route;
    P3 = CommsBuffer.Protocol.Command;
    P4 = CommsBuffer.Protocol.Length;
    P5 = CommsBuffer.Protocol.HCS;
    P6 = CommsBuffer.Protocol.Data[0];
    P7 = CommsBuffer.Protocol.Data[1];
    P8 = CommsBuffer.Protocol.Data[2];
    P9 = CommsBuffer.Protocol.Data[3];
    P10 = CommsBuffer.Protocol.Data[4];
    P11 = CommsBuffer.Protocol.Data[5];
    P12 = CommsBuffer.Protocol.Data[5];
    ...

    Then the protocol header section (5 bytes) is repeated in 1st 5 bytes of the protocol data section and then only the actual protocol data. Like this:

    P1 = 0
    P2 = 1
    P3 = 2
    P4 = 3
    P5 = 4
    P6 = 0
    P7 = 1
    P8 = 2
    P9 = 3
    P10 = 4
    P11 = 10
    P12 = 11

    I read data from the UART into CommsBuffer like this:

    TCommsBuffer UART1RXBuffer;
    u8 RXBufferIndex = 0;

    //RX_Interrupt_Service_Routine

    if ((RXBufferIndex < COMMS_BUFFER_SIZE)&&(!RX_MESSAGE)) { UART1RXBuffer.Data[RXBufferIndex] = RXReg; RXBufferIndex ++;
    }

Reply
  • You don't understand my problem. I never said I want to access data with an offset. I said that I get repeated data inside my structure.

    Example:

    Say I do the following, fill the CommsBuffer.Data with sequential numbers:

    for (Loop = 0; Loop < COMMS_BUFFER_SIZE; Loop ++) { CommsBuffer.Data[Loop] = Loop;
    }

    Then if I read the contents of the CommsBuffer.Protocol structure

    P1 = CommsBuffer.Protocol.StartByte;
    P2 = CommsBuffer.Protocol.Route;
    P3 = CommsBuffer.Protocol.Command;
    P4 = CommsBuffer.Protocol.Length;
    P5 = CommsBuffer.Protocol.HCS;
    P6 = CommsBuffer.Protocol.Data[0];
    P7 = CommsBuffer.Protocol.Data[1];
    P8 = CommsBuffer.Protocol.Data[2];
    P9 = CommsBuffer.Protocol.Data[3];
    P10 = CommsBuffer.Protocol.Data[4];
    P11 = CommsBuffer.Protocol.Data[5];
    P12 = CommsBuffer.Protocol.Data[5];
    ...

    Then the protocol header section (5 bytes) is repeated in 1st 5 bytes of the protocol data section and then only the actual protocol data. Like this:

    P1 = 0
    P2 = 1
    P3 = 2
    P4 = 3
    P5 = 4
    P6 = 0
    P7 = 1
    P8 = 2
    P9 = 3
    P10 = 4
    P11 = 10
    P12 = 11

    I read data from the UART into CommsBuffer like this:

    TCommsBuffer UART1RXBuffer;
    u8 RXBufferIndex = 0;

    //RX_Interrupt_Service_Routine

    if ((RXBufferIndex < COMMS_BUFFER_SIZE)&&(!RX_MESSAGE)) { UART1RXBuffer.Data[RXBufferIndex] = RXReg; RXBufferIndex ++;
    }

Children