Saving incoming data from serial port to txt file
-
Hi, I need to save incoming data from my serial port into a txt file. The data is constantly coming and just stop when a button is clicked. The serial part is working but the saving into txt no. Does someone can explain it with code? Tks
@void SaveLog::saveFile(QString LogTobeSaved)
{
FileTxt->open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(FileTxt);
out<<LogTobeSaved;
FileTxt->close();
}@ -
Maybe something like this:
@
void SaveLog::saveFile(QString LogTobeSaved)
{
FileTxt->open(QIODevice::WriteOnly | QIODevice::Text);
FileTxt->write(LogTobeSaved.toLatin1());
FileTxt->close();
}
@Not sure why you need the QTextStream object.
You might want to make sure the function is only called once. If you call this with an empty string it would clear the file.
-
Without knowing what you are getting over your serial port this is a bit hard. As Rondog said, if you got a few bytes or a string, wrote them to the file then got more data you'd tromp on the previously written stuff.
QSerialPort has a function "bytesAvailable". You could look at that and continue to append your serial port data into a QByteArray or a QString. When you are ready then use the function above to write it all at once.
One last thing to think about is file write privileges. You don't really specify what FileTxt is. Is it in the writeable area? See QStandardPaths which can give you paths to writeable areas.