----------------------original post-------------------------------------------------------
I want to transfer a pack of data from arduino to PC, which has multiple data types.
However, when I try to receive the data from the PC side, sometimes data seems to be lost.
below is the code I used on Mega, it worked perfectly.
#pragma pack (2)
class SensorData
{
public:
byte tag;
byte length;
int intData[3];
SensorData()
{}
void readSensor()
{
tag = 11;
length = 22;
for(int i = 0; i < 3 ; i++)
{
intData[i] = i;
}
}
};
class SensorData SD0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
SD0.readSensor();
Serial.write((byte *)&SD0, sizeof(SD0));
Serial.flush();
delay(100);
}
On the PC side, I used a loop to print whatever I receive out:
while((nread = read(fd, BUFF, 50)) > 0)
{
cout << ">> ";
for(int i = 0; i < 50; i++)
{
cout << (int)BUFF[i] << "-";
}
cout << endl;
The problem is, if you change intData[3] into intData[20], to add more data in the data structure
some data are some how lost, output is shown below:[more can be found here:
https://docs.google.com/open?id=0B-PlSu8oC8g-U0NrT3MyNXk3UWc]
>> 0-4-0-5-0-6-0-7-0-8-0-9-0-10-0-11-0-12-0-13-0-14-0-15-0-16-0-17-0-18-0-19-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-
>> 11-4-0-5-0-6-0-7-0-8-0-9-0-10-0-11-0-12-0-13-0-14-0-15-0-16-0-17-0-18-0-19-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-
where the correct output should be:
>> 11-22-1-0-2-0-3-0-4-0-5-0-6-0-7-0-8-0-9-0-10-0-11-0-12-0-13-0-14-0-15-0-16-0-17-0-18-0-19-0-0-0-0-0-0-0-0-0-0-0-
some help, please?? >_<