[英]copying data to data structure using memcpy
I'm trying to read data from EEPROM, and I have three structs. 我正在尝试从EEPROM读取数据,并且有三种结构。
typedef struct
{
fract32 MechCoilPhiBase; // Mech Angle Table
fract32 MechCoilPhi3rd; // Mech Angle Table
fract32 PhiSaltwater; // Saltwater Table
UINT16 d;
UINT16 crc;
} ChannelData_T;
typedef struct
{
UINT32 reHarmonic;
UINT32 reFundamental;
UINT32 imgHarmonic;
UINT32 imgFundamental;
UINT16 crc;
} CoilBoard_T;
// mechanic angles and salt water angles of coil stored in coil-eeprom
typedef struct
{
ChannelData_T channel[NUM_CHANNELS];
CoilBoard_T coilboard;
// UINT32 gCoilSerialNumber;
// UINT32 gInversSerialNumber;
} Coil_Eeprom_Data_T;
I'm trying to read the data, but the size is not a power of 2, I tried to padding the data, but the struct is not filled correctly. 我正在尝试读取数据,但是大小不是2的幂,我试图填充数据,但是结构未正确填充。
I'm using the following code to read the data from the buffer, and fill it with the struct. 我正在使用以下代码从缓冲区读取数据,并用结构填充它。 For example the crc variable is 0, and its not read correctly from the buffer.
例如,crc变量为0,并且不能从缓冲区正确读取。
here is how I copy the data to the buffer 这是我将数据复制到缓冲区的方法
memcpy( (void*) &CoilEepromData, (const void*) &EepromCoil.aRxData[0], sizeof(Coil_Eeprom_Data_T) );
extern volatile Coil_Eeprom_Data_T CoilEepromData;
extern volatile Eeprom_Coil_T EepromCoil; // control struct for the coil-eeprom
typedef struct
{
UINT8 crcValueOut;
UINT8 crcValueIn;
UINT8 pageAddress;
UINT8 dataLength;
UINT8 bytesToTransmit;
UINT8 bytesWritten;
UINT8 bytesToReceive;
UINT8 bytesRead;
UINT8 errorCount;
bool bWriteSucceed:1;
bool bStartup:1;
bool bReadingStarted:1;
UINT8 aTxData[COIL_SPI_BUFFER_SIZE];
UINT8 aRxData[COIL_SPI_BUFFER_SIZE];
} Eeprom_Coil_T;
No idea what your comment about powers of two means, if that's a requirement you have to make it clearer. 不知道您对“二的幂”的评论意味着什么,如果这是一个要求,则必须使其更清楚。
Also, most casts to/from void *
in C are not necessary, you shouldn't do them "just to be safe". 另外,大多数在C中从
void *
强制转换/从强制void *
强制转换的操作都是不必要的,您不应“为了安全起见”进行操作。 It's hard to understand from your posted code why the casts are needed. 从您发布的代码中很难理解为什么需要强制转换。
Finally, remember that structures are values too, you can use plain old assignment: 最后,请记住结构也是值,您可以使用简单的旧赋值:
CoilEepromData.channel[0] = EepromCoil.aRxData[0];
CoilEepromData.channel[1] = EepromCoil.aRxData[1];
CoilEepromData.channel[2] = EepromCoil.aRxData[2];
The compiler might well optimize that into a single memcpy()
call, but this is much better since it's more readable and easie to get right. 编译器可能会将其优化为单个
memcpy()
调用,但这要好得多,因为它更易读,更容易正确。 You might want to put it in a loop to lessen the risk for indexing typos. 您可能希望将其放在一个循环中,以减少索引错误的风险。
If you really want to use memcpy()
, here's how: 如果您真的想使用
memcpy()
,则方法如下:
memcpy(&CoilEepromData.channel[0], &EepromCoil.aRxData[0], sizeof CoilEepromData.channel[0]);
This uses sizeof
on the destination variable, not on a type. 这在目标变量而不是类型上使用
sizeof
。 This is a bit safer. 这样比较安全。 Again, this would do well in a loop:
同样,这在循环中会很好:
for(size_t i = 0; i < sizeof CoilEepromData.channel / sizeof CoilEepromData.channel[0]; ++i)
memcpy(&CoilEepromData.channel[i], &EepromCoil.aRxData[i], sizeof CoilEepromData.channel[i]);
The sizeof
in the second part of the for
header is to avoid hard-coding the array length. for
标题的第二部分中的sizeof
是为了避免硬编码数组长度。 This is a bit scary since it requires that the length of both source and destination arrays be the same, of course. 当然,这有点吓人,因为它要求源数组和目标数组的长度都相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.