I am exchanging structs over serial connection, in C++, between my linux machine (x64) and a micro controller (Arduino Uno). The struct is basically as follows for now.
struct __attribute__ ((packed)) packet
{
uint8_t data[12];
}
For numbers smaller than 32, sending and reading is fine on the micro controller but larger numbers are not the same. Like 32, becomes 96, 33 becomes 97 and so forth.
packet my_packet;
for (int i = 0; i < 12; ++i)
{
my_packet.data[i] = i;
}
if(write(fd, &my_packet, sizeof(packet))>0)
{
printf("received:\n");
}
else
{
printf("Error writing\n");
}
On the reading side,
int ret = read(fd, &my_packet_rev, sizeof(my_packet_rev));
if( ret < 0)
{
printf("Error reading\n");
exit(0);
}
else if (ret == 0 )
{
printf("No response\n");
}
else
{
printf("received:\n");
for (int i = 0; i < 12; ++i)
{
printf("%02X, ", my_packet_rev.data[i]);
}
printf("\n");
}
Input:
1E 1F 20 21 22 23 24 25 26 27 28 29
Output:
1E 1F 60 61 62 63 64 65 66 67 68 69
What might I be doing wrong?