(updated)Help on Serial communication between Arduino and PC!!

I have changed my post into the original state, and here is some update.

it turns out that the problem is not with Arduino(I am using 1.0 IDE) at all, I tried my program on 2 linux computer using 2 USB-TTL adaptors, the problem remains, which I think is a good thing because the problem is how to program the serial ports rather than some bugs....

Background Idea

This is a part of my semester project, so it is kinda in a hurry. The basic idea is to transfer all the sensor data from Arduino to Raspberry Pi through UART, and then goes to PC through WIFI(dongle), I also send commands to RPi and then goes to Arduino via UART. And you are right, I need to define my own mini protocol, which is described below,

There are 3 frame types, sensor data, request data, and ACK, they all have fixed length, so no need for end marking stuff. The sensor data has these data elements, others are much more simpler. byte tag; byte length; unsigned long count; unsigned short int intData[8]; float floatData[5]; byte verification;


Well, I have already programmed the protocol into Arduino, since it has problems, I have to make some small tests, so you will not find the protocol in the code below, it is simply a test to transfer 20 short int data. When I tried to send 3 int data, the send and receive seemed to work perfectly, everything is correct, but if I send 20 int data, the first 7 bytes were just missing on the receiver side(still don't know where is the problem).

Now, I know the code I wrote is really really crappy(I adapt from C into C++), which I don't really want put it up here to embarrass myself. But in case anyone want to find the problem, it is in my GDrive: [https://docs.google.com/open?id=0B-PlSu8oC8g-Q2RELWwxSUpxTk0]

So anyone care to introduce some similar examples on how to transfer the entire instance of a class(I didn't use struct because I need some methods), through the serial port ??

For example, how to send this via UART ?? and receive it on the other side properly ??

Thank you sooooo much !!

class SensorData
    byte tag;
    byte length;
    unsigned long count;
    unsigned short int intData[8];
    float floatData[5];
    byte verification;

The first question I have is why you are using a class, rather than a struct?

The answer to how to send that data is related to marshaling/unmarshaling. That is the process for sending an object from one computer to another AS AN OBJECT. So, the question is really whether that is what you want to do. Or, is it the data in the object that you want to send?

You need to show some code that creates and populates an instance of that class (or struct, which is what it should be), and define how you want to marshal the data. The receiver needs to unmarshall the data the same way.

Endianness and differing type sizes can play havoc with marshaling/unmarshaling data. Which, of course, is why this is part of your project - to see if you have considered these issues and developed a plan to deal with them.

cosailer: For example, how to send this via UART ?? and receive it on the other side properly ??

I would serialize it. That is, convert it into something else, like just a string with (say) commas between each number. There is no guarantee that an int on the receiving end will be the same size as an int on the sending end, for example, or that floats have the same format internally.