Data sets inside a union

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Data sets inside a union

2,479 Views
fusion2344
Contributor I

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?

Labels (1)
Tags (1)
0 Kudos
6 Replies

557 Views
CompilerGuru
NXP Employee
NXP Employee
The first 5 bytes of CommsBuffer.Data[x] are the header bytes, if you want the data bytes of the protocol without the header, that would be CommsBuffer.Protocol.Data[x].
Or maybe I miss the problem?

Daniel



0 Kudos

557 Views
fusion2344
Contributor I
Yes, you misunderstood.

I am actually asking if the structure and union defenition is correct, will it work as it is?

Then there is 2 other problems.

Problem 1:

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

Problem 2:

With normal type defined structures the editor will auto complete if I type

CommsBuffer.

and it will show the list where you can select the variables of CommsBuffer.

But with the current type defined union it doesn'y display the autocomplete list, making me think that there might be something wrong with the way I defined the union and structure.



Message Edited by fusion2344 on 2008-02-02 03:32 PM

Message Edited by fusion2344 on 2008-02-02 03:33 PM
0 Kudos

557 Views
bigmac
Specialist III
Hello,
 
I tried your code under full chip simulation, and obtained the expected result, as supported by a check of RAM contents after execution.  The array CommsBuffer.Protocol.Data definitely commenced with a value of 5, whereas CommsBuffer.Data  commenced with value zero.
 
Regards,
Mac
 
0 Kudos

557 Views
fusion2344
Contributor I
Mmmmmmh........? I wonder. I can give you a screenshot of the variables in full chip simulation.
It defenitely repeats the header section into the data section.

Do you have any comments on the other 2 problems?
0 Kudos

557 Views
Lundin
Senior Contributor IV
The union is defined correctly. Are you sure you are looking at the right values? One could easily confuse CommsBuffer.Data with CommsBuffer.Protocol.Data, for example.
0 Kudos

557 Views
CrasyCat
Specialist III
Hello
 
I did try the code snippet you have provided on CodeWarrior for HC12 V4.6.
It is working well.
 
Also CodeCompletion is working like a charm in there.
 
- Which version of CodeWarrior are you using?
To retrieve that info:
- Start CodeWarrior
- Select Help -> About Freescale CodeWarrior
- Click on "Install Products"
- CodeWarrior version used is displayed on top in the Installed Products dialog.
 
CrasyCat
0 Kudos