gpt4 book ai didi

c - 使用 memcpy 将多个变量存储到缓冲区

转载 作者:行者123 更新时间:2023-11-30 15:18:41 24 4
gpt4 key购买 nike

我对 Arduino 非常很陌生,我找不到任何方法来执行以下操作。我有一个 typedef 结构,它填充多个值:

void loop() {
accel.process();
AccelerationReading accelData;
accelData.xAxis = accel.xAxis;
accelData.yAxis = accel.yAxis;
accelData.zAxis = accel.zAxis;
//Irrelevant code after this
}

填充结构后,我将其memcpy放入缓冲区中:

uint8_t buffer[6];
memcpy((void*)buffer, (void*)&accelData, 6);
现在这工作正常,但我还需要从两个模拟端口读取值,并且这两个值也需要添加到 memcpy 中。填充两个变量的代码:

//New variables
int shortflexposition;
int longflexposition;
//Filling up the variables.
shortflexposition = analogRead(shortflexpin);
longflexposition = analogRead(longflexpin);

accelData 包含 6 个字节的原始值,现在我想在这些字节之后添加shortflexposition 和longflexposition。变量shortflexposition和longflexposition中的值始终在0和1023之间。我应该如何在 memcpy 中添加其他变量?我尝试过例如:

uint8_t buffer[8];
memcpy((void*)buffer, (void*)&accelData + shortflexposition, 8);

但这似乎是向彼此添加值,而不是将它们添加到下一个字节或其他内容。那么,如何使用 memcpy 在下一个字节位置添加新变量?

谢谢!延特

更新:这是对 Lundin 代码的尝试..

typedef union
{
struct s
{
AccelerationReading accelData;
uint16_t shortflexposition;
uint16_t longflexposition;
};
uint8_t raw_data [sizeof(struct s)];
} my_data;

void setup() {
// Bean Serial is at a fixed baud rate. Changing the value in Serial.begin() has no effect.
Serial.begin();
accel.init();
}

void loop() {
accel.process();
AccelerationReading accelData;
my_data data;
data.accelData.xAxis = accel.xAxis;
data.accelData.yAxis = accel.yAxis;
data.accelData.zAxis = accel.zAxis;

//Get flex readings
int shortflexposition;
int longflexposition;

//Read the position of the flex sensor (0 to 1023?)
data.shortflexposition = analogRead(shortflexpin);
data.longflexposition = analogRead(longflexpin);
my_data some_other_data = data;

uint8_t buffer[6];
memcpy((void*)buffer, (void*)&accelData, 6);
Bean.setScratchData(1,buffer,6);
Bean.sleep(1000);
}

但出现以下错误:

GetReader.ino: In function 'void loop()':
GetReader.ino:54:8: error: 'union loop()::my_data' has no member named 'accelData'
GetReader.ino:55:8: error: 'union loop()::my_data' has no member named 'accelData'
GetReader.ino:56:8: error: 'union loop()::my_data' has no member named 'accelData'
GetReader.ino:63:8: error: 'union loop()::my_data' has no member named 'shortflexposition'
GetReader.ino:64:8: error: 'union loop()::my_data' has no member named 'longflexposition'

最佳答案

首先,如果要添加两个 16 位数字 == 4 字节,那么将缓冲区大小增加 2 字节是没有任何意义的。我建议你做这样的事情:

typedef union
{
struct s
{
AccelerationReading accelData;
uint16_t shortflexposition;
uint16_t longflexposition;
};
uint8_t raw_data [sizeof(struct s)];
} my_data;


// use whatever compile-time assert you have available to check for padding:
static_assert(sizeof(my_data) == sizeof(AccelerationReading) + 2*sizeof(uint16_t),
"Unwanted padding detected.");


my_data data;
data.accelData.xAxis = x;
data.accelData.yAxis = y;
data.accelData.ZAxis = z;
data.shortflexposition = something;
data.longflexposition = something;

my_data some_other_data = data; // equivalent of memcpy

如果您需要逐字节访问数据,只需使用 data.raw_data[i] 即可。

<小时/>

如果您使用旧的编译器,则必须将代码更改为:

typedef union
{
struct s
{
AccelerationReading accelData;
uint16_t shortflexposition;
uint16_t longflexposition;
} my_compiler_is_old;
uint8_t raw_data [sizeof(struct s)];
} my_data;

data.my_compiler_is_old.accelData.xAxis = x;
data.my_compiler_is_old.accelData.yAxis = y;
...

关于c - 使用 memcpy 将多个变量存储到缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com