PDA

View Full Version : How to copy data from QserialPort to Qfile



Andrea81
26th August 2015, 11:59
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.


void IMUACQThread::ImuAcqLoadParam(const IMUACQ_THREAD_PARAM &ImuAcqParam)
{
QSerialPort serial;
QFile *file,* fileNew,* fileOld;
file= new QFile(this);
fileNew= new QFile(this);
fileOld=new QFile(this);

QByteArray InMsg;
QTime time;



InMsg.reserve(30000);//30 KB
time.start();
while(!QThread::currentThread()->isInterruptionRequested() ){
serial.waitForReadyRead(20);//Necessary in polling mode
InMsg.append(serial.readAll());


if((time.elapsed()>200) && (!InMsg.isEmpty()) && (file->bytesToWrite()==0)){
AcqLoop++;
time.restart();
nByte+=InMsg.size();
nByteTmp=file->write(InMsg);// It buffers data before writing
InMsg.clear(); // before to restart the while loop
if(nByteTmp==-1 /*&& Status*/){
FileErrAccoured++;
emit ComError("IMUAcq Thread Error",file->errorString());
}

}

} //End While loop

}

}

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