Reading from SerialPort and write to txt file.
-
I want to read data from COM port, then write to txt file and display them. But the data I received is incomplete. Here my code:
bool finished(false);
QByteArray datas = serialPort->readAll();
if(datas.size()>0)
{
if(file.isOpen())
{
QTextStream in(&file);
in<<datas;
file.close();
finished=true;
}
}
else {
QMessageBox::warning(this, "Cảnh báo"+ file.errorString());
return;
}
if(finished){
doc();
}doc() is a function that displays file data.
-
@Rika readAll() will give you what currently is available (what was received so far). This code will not work as it is now. You either use a loop with waitForReadyRead(), read then and add the data to a buffer. Or you use signals slots to read when there is something to read. Also, how do you know that you got all the data? Do you have some protocol?
You should take a look at the examples: https://doc.qt.io/qt-5/qtserialport-examples.html