私は、PC上のプログラムにユニオンを使用して構造体を送信するArnuino用のプログラムを作成しました。構造体は整数にする必要がありますが、正しい出力が得られません。PC上のプログラムは、シリアル接続用のブーストライブラリを利用します。また、64ビットでビルドおよびコンパイルされます(vs2010を使用)。
ユニオン内に単一の整数変数がある場合、コードは機能します。しかし、ユニオンを使用した構造体は機能しません。1つの整数のみがデータを取得し、そのデータは間違っています。
私は64ビット(pc)と32ビット(Ardunio)の問題を抱えていますか?そして、誰もがこれで私を助けることができます。前もって感謝します。
PCコードスニペット(シリアル設定は省略):
union packed{
struct test{
unsigned int data;
unsigned int data2;
} struc;
unsigned char bytes[8];
}SerialPacked;
SerialPacked.struc.data = 0;
SerialPacked.struc.data2 = 0;
cout << "Data before: " << SerialPacked.struc.data << endl;
cout << "Data2 before: " << SerialPacked.struc.data2 << endl;
read(port,buffer((unsigned char*)&SerialPacked.bytes[0], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[1], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[2], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[3], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[4], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[5], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[6], 1));
read(port,buffer((unsigned char*)&SerialPacked.bytes[7], 1));
cout << "Data after: " << SerialPacked.struc.data << endl;
cout << "Data2 after: " << SerialPacked.struc.data2 << endl;
Arduinoコード:
int ledPin = 13;
union packed{
struct test{
unsigned int data;
unsigned int data2;
}struc;
unsigned char bytes[8];
}
SerialPacked;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
SerialPacked.struc.data = 0;
SerialPacked.struc.data2 = 0;
};
void loop() {
while(1){
digitalWrite(ledPin,HIGH);
SerialPacked.struc.data = SerialPacked.struc.data + 1;
SerialPacked.struc.data2 = SerialPacked.struc.data2 + 1;;
for(int i=0;i <8; i++){
Serial.write(SerialPacked.bytes[i]);
};
digitalWrite(ledPin,LOW);
delay(1000);
};
}