I have written a program for the Arnuino that sends a struct witin a union to a program on the PC. The struct has to integers but i dont't get the right output. The program on the PC makes use of the boost library for the serial conection. And is build and compiled in 64bit (with vs2010).
The code works if i have a single integer variable within a union. But a struct witin a union doesn't work. Only one integer gets data and that data is wrong.
I it maby a 64 bit(pc) and 32bit(Ardunio) problem? And can anyone help me with this. Thanks in advance.
The PC code snippet (serial settings are omitted):
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;
The Arduino code:
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);
};
}