Structure memory storage

I have a structure that is in my embedded Arduino code and the Windows PC program the communicates with it. When I use the structure in the PC code I have to add #pamaga pack (push,1) before the struct and #pragma pack(pop) at the end , so the data is aligned to one byte .

I have a bug in the Arduino code that is similar to what I had before adding Pop and Push to the Windows code.

Do I have to do something similar for the Arduino code. When I write to the Table array I get values in the wrong place when I read back.

Thank you Ray.
This is the structure.

#define STEPSTABLEX 14
#define STEPSTABLEY 2

// save the current data alignment setting to the stack
// and set data alignment to 1 byte
#pragma pack(push,1)

struct ECU_Cal {
	uint8_t startChar; // $ Start character
	uint8_t PacketHi;
	uint8_t PacketLo;
	uint8_t PacketID;

	struct RealTimeData {
		uint8_t startChar; // $ Start character
		uint8_t PacketHi;
		uint8_t PacketLo;
		uint8_t PacketID;
		uint16_t map;
		uint16_t stepPos;
		uint8_t stepStopSw;
		uint16_t crcCnt;
	} data;

	struct SetupSettings {
		uint8_t startChar; // $ Start character
		uint8_t PacketHi;
		uint8_t PacketLo;
		uint8_t PacketID;
		uint16_t table[STEPSTABLEY][STEPSTABLEX];
		uint16_t crcCnt;
	}setup;

	uint16_t crcCnt;
};

//restore the previous data alignment
#pragma pack(pop)

Assuming an AVR Arduino, no since it is an 8 bit device byte alignment should be OK.

If you add a one byte filler before or after stepStopSw you would get the right alignment for the 16 bit fields in the structure. Then you may not need the pack #pragma (and get better generated code on the PC).

Or swap stepStopSw and crcCnt.

Please post a simple sketch that illustrates the problem

May be on the arduino side you can define the struct with

struct __attribute__((packed, aligned(1)) …

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