synchronous serial communication over USB

After figuring out how to transmit data to the computer and read it with LabVIEW (here) I went ahead and built a function that will transmit all the wanted data (in this case- the values recieved from MPU9150, and results of the strapdown calculations):

void printResults()
{
    Serial.print("<raw_gyro_x>"); Serial.print("<"); Serial.print(gyro_raw[0]); Serial.print(">");
    Serial.print("<raw_gyro_y>"); Serial.print("<"); Serial.print(gyro_raw[1]); Serial.print(">");
    Serial.print("<raw_gyro_z>"); Serial.print("<"); Serial.print(gyro_raw[2]); Serial.print(">");

    Serial.print("<raw_acc_x>"); Serial.print("<"); Serial.print(acc_raw[0]); Serial.print(">");
    Serial.print("<raw_acc_y>"); Serial.print("<"); Serial.print(acc_raw[1]); Serial.print(">");
    Serial.print("<raw_acc_z>"); Serial.print("<"); Serial.print(acc_raw[2]); Serial.print(">");

    Serial.print("<raw_mag_x>"); Serial.print("<"); Serial.print(mag_raw[0]); Serial.print(">");
    Serial.print("<raw_mag_y>"); Serial.print("<"); Serial.print(mag_raw[1]); Serial.print(">");
    Serial.print("<raw_mag_z>"); Serial.print("<"); Serial.print(mag_raw[2]); Serial.print(">");

    Serial.print("<mean_gyro_x>"); Serial.print("<"); Serial.print(gyro_in[0]); Serial.print(">");
    Serial.print("<mean_gyro_y>"); Serial.print("<"); Serial.print(gyro_in[1]); Serial.print(">");
    Serial.print("<mean_gyro_z>"); Serial.print("<"); Serial.print(gyro_in[2]); Serial.print(">");

    Serial.print("<mean_acc_x>"); Serial.print("<"); Serial.print(acc_in[0]); Serial.print(">");
    Serial.print("<mean_acc_y>"); Serial.print("<"); Serial.print(acc_in[1]); Serial.print(">");
    Serial.print("<mean_acc_z>"); Serial.print("<"); Serial.print(acc_in[2]); Serial.print(">");

  /*Serial.println("<mean_mag_x"); Serial.print("<"); Serial.println(mag_raw[0]); Serial.print(">");
    Serial.println("<mean_mag_y"); Serial.print("<"); Serial.println(mag_raw[1]); Serial.print(">");
    Serial.println("<mean_mag_z"); Serial.print("<"); Serial.println(mag_raw[2]); Serial.print(">");*/

    Serial.println("<att_p>"); Serial.print("<"); Serial.println(att[0]/3.14*180); Serial.print(">");
    Serial.println("<att_t>"); Serial.print("<"); Serial.println(att[1]/3.14*180); Serial.print(">");
    Serial.println("<att_f>"); Serial.print("<"); Serial.println(att[2]/3.14*180); Serial.print(">");

    Serial.println("<acc_n>"); Serial.print("<"); Serial.println(acc[0]); Serial.print(">");
    Serial.println("<acc_e>"); Serial.print("<"); Serial.println(acc[1]); Serial.print(">");
    Serial.println("<acc_d>"); Serial.print("<"); Serial.println(acc[2]); Serial.print(">");

    Serial.println("<vel_n>"); Serial.print("<"); Serial.println(vel[0]); Serial.print(">");
    Serial.println("<vel_e>"); Serial.print("<"); Serial.println(vel[1]); Serial.print(">");
    Serial.println("<vel_d>"); Serial.print("<"); Serial.println(vel[2]); Serial.print(">");
}

I then built a LabVIEW VI that will display the received data:

now, everything works great, but it occurred to me that I have no control over the speed of communication, so if the arduino is transmitting faster then the LabVIEW VI is receiving, I'll start gating outdated data, and mybe after a while even fill the buffer.

How can I solve this problem and make sure I always get up-to-date data?
Are there libraries that will use more advanced (synchronous) serial communication protocols over the usb?

Thank you for your input!

but it occurred to me that I have no control over the speed of communication,

Sure you do. Either the Arduino sends data and then loops waiting for a "ready to receive" message from the VI, or the Arduino does not send anything until it receives a "ready to receive" message from the VI.

I'll start gating outdated data, and mybe after a while even fill the buffer.

How can the data be outdated when it is being streamed live? Typically, a PC should be able to receive data faster than the Arduino can send it. The Arduino certainly can read data faster than it can be sent.

Are there libraries that will use more advanced (synchronous) serial communication protocols over the usb?

Yes, but they are not necessary in your case.

PaulS:
Sure you do. Either the Arduino sends data and then loops waiting for a "ready to receive" message from the VI, or the Arduino does not send anything until it receives a "ready to receive" message from the VI.

I actually didn't think of adding a "ready to receive" message- the VI was passive and didn't send any output at all. That will solve the problem though.

PaulS:
How can the data be outdated when it is being streamed live? Typically, a PC should be able to receive data faster than the Arduino can send it. The Arduino certainly can read data faster than it can be sent.

I was thinking If the VI lags (or if it's way slower then the arduino scatch for some reason) the unread messages from the Arduino will pile up in the buffer (won't they?) then when the VI is back online it will read the older messages before getting to the last message, instead of just forgeting about the missed data. does that make sense?

Anyway adding the "ready to receive" part should gurentee that I'm getting what I want. Thanks for the tip :slight_smile: