Hello forum!
I have a little problem with an union in Teensy 3.5. I am sending binary data packages from a Teensy 3.2 to a Teensy 3.5 via Serial communication.
I am using a union structure to unpack and extract data from the received packages.
My problem is that, if I use this structPacket:
and I extract data with this function:
everything works. But I think this is not a really good way to use the union.
Otherwise, if I use the following structPacket and extractData function, I can not read the correct values.
What is it happening?
Thank you for any suggestion!
I have a little problem with an union in Teensy 3.5. I am sending binary data packages from a Teensy 3.2 to a Teensy 3.5 via Serial communication.
I am using a union structure to unpack and extract data from the received packages.
My problem is that, if I use this structPacket:
Code:
struct structPacket
{
uint8_t Start_bytes[3];
uint8_t timestamp[4];
uint8_t cycles[4];
uint8_t encoder_1_pos[2];
uint8_t encoder_2_pos[2];
uint8_t encoder_1_omega[2];
uint8_t encoder_2_omega[2];
uint8_t load_cell_1[2];
uint8_t load_cell_2[2];
uint8_t pot_pos[2];
uint8_t pot_punto[2];
uint8_t Stop_bytes[3];
};
union unionIncoming
{
uint8_t vData[BYTE_SERIAL_INCOMING_PACKET];
structPacket Raw;
}
SERIAL_Incoming_Data;
and I extract data with this function:
Code:
void extractData(structPacket Raw)
{
TIMER = *(unsigned long *)&Raw.timestamp;
CYCLES = *(unsigned long *)&Raw.cycles;
ENC_1_pos_raw = *(int16_t *)&Raw.encoder_1_pos;
ENC_2_pos_raw = *(int16_t *)&Raw.encoder_2_pos;
ENC_1_omega_raw = *(int16_t *)&Raw.encoder_1_omega;
ENC_2_omega_raw = *(int16_t *)&Raw.encoder_2_omega;
LC_1_raw = *(int16_t *)&Raw.load_cell_1;
LC_2_raw = *(int16_t *)&Raw.load_cell_2;
POT_pos_raw = *(int16_t *)&Raw.pot_pos;
POT_punto_raw = *(int16_t *)&Raw.pot_punto;
}
everything works. But I think this is not a really good way to use the union.
Otherwise, if I use the following structPacket and extractData function, I can not read the correct values.
Code:
struct structPacket
{
uint8_t Start_bytes[3];
unsigned long timestamp;
unsigned long cycles;
int16_t encoder_1_pos;
int16_t encoder_2_pos;
int16_t encoder_1_omega;
int16_t encoder_2_omega;
int16_t load_cell_1;
int16_t load_cell_2;
int16_t pot_pos;
int16_t pot_punto;
uint8_t Stop_bytes[3];
};
Code:
ENC_1_pos_raw = (int16_t)Raw.encoder_1_pos;
ENC_2_pos_raw = (int16_t)Raw.encoder_2_pos;
ENC_1_omega_raw = (int16_t)Raw.encoder_1_omega;
ENC_2_omega_raw = (int16_t)Raw.encoder_2_omega;
LC_1_raw = (int16_t)Raw.load_cell_1;
LC_2_raw = (int16_t)Raw.load_cell_2;
POT_pos_raw = (int16_t)Raw.pot_pos;
POT_punto_raw = (int16_t)Raw.pot_punto;
What is it happening?
Thank you for any suggestion!