Good Morning,
I'm straggling to develop an application that receive data from a QSerialPort instance and copy It in a QFile but sometimes in the action to pick-up data from serial port and coping it on the hard-drive it loses data.
Below you find a semplified code version.

Qt Code:
  1. void IMUACQThread::ImuAcqLoadParam(const IMUACQ_THREAD_PARAM &ImuAcqParam)
  2. {
  3. QSerialPort serial;
  4. QFile *file,* fileNew,* fileOld;
  5. file= new QFile(this);
  6. fileNew= new QFile(this);
  7. fileOld=new QFile(this);
  8.  
  9. QByteArray InMsg;
  10. QTime time;
  11.  
  12.  
  13.  
  14. InMsg.reserve(30000);//30 KB
  15. time.start();
  16. while(!QThread::currentThread()->isInterruptionRequested() ){
  17. serial.waitForReadyRead(20);//Necessary in polling mode
  18. InMsg.append(serial.readAll());
  19.  
  20.  
  21. if((time.elapsed()>200) && (!InMsg.isEmpty()) && (file->bytesToWrite()==0)){
  22. AcqLoop++;
  23. time.restart();
  24. nByte+=InMsg.size();
  25. nByteTmp=file->write(InMsg);// It buffers data before writing
  26. InMsg.clear(); // before to restart the while loop
  27. if(nByteTmp==-1 /*&& Status*/){
  28. FileErrAccoured++;
  29. emit ComError("IMUAcq Thread Error",file->errorString());
  30. }
  31.  
  32. }
  33.  
  34. } //End While loop
  35.  
  36. }
  37.  
  38. }
To copy to clipboard, switch view to plain text mode 

This function belongs to a Qthread instance.
I buffer Data in a QByteArray "InMsg" at most every 20[ms] and it stores on QFile every 200[ms].
I made decision to store data on a File every 200[ms] to not overload the Hard-Drive (because HD must menage 4 QThread instances equal to this at the same time).
I would like to ask if the decision to Buffer data in "InMsg" instead of storing it directly on the QFile is a good decision or is it a waste of resources?
Do you think i should use the code "file= new QFile(this);" to make the file to belong to the new created QThread or Should I avoid the usage of "this"?
Unfortunatly documentation of QThread is not complitly clear.
Any comments or suggestions are appreciated.
Regards
Andrea