使用 memcpy 存储多个变量以进行缓冲

Store multiple variables to buffer with memcpy

非常 是 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;
...