Read a file txt
-
Hi
ok that i cant explain.Best guess is that you read data from a serial port and call save and load
pr DataReady signal and hence we see it like this.When reading from serial port, all data might not come in one go. And it kinda looks like this but
I can't understand how your text file then looks normal.
(ahh you use append so the end result looks normal )can you show the code around where you call updateGUI_sonde ?
@mrjj the code :
connect(_sonde, &sonde::gotNewData_sonde, this, &MainWindow::updateGUI_sonde);
signals:
void gotNewData_sonde(QByteArray data_sonde );//prototype du signal
void sonde::newData_sonde() {emit gotNewData_sonde(_sonde.readAll()); // lire tout les donnes dans le port serie
}
-
@J-Hilk thank you for your reply
the result :
can i send you the code by e-mail ?@LatitudeFr no need, you need to accumulate your data.
like @mrjj said, your serial port(?) doesn't notify you when all data arrived, it does when any data arrived.
and you have to store that and decide when and how all data arrived.
from what you showed, I would say your message ends with a
0x2320
aka # and space.QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); accumulatedData.clear(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } }
-
@LatitudeFr no need, you need to accumulate your data.
like @mrjj said, your serial port(?) doesn't notify you when all data arrived, it does when any data arrived.
and you have to store that and decide when and how all data arrived.
from what you showed, I would say your message ends with a
0x2320
aka # and space.QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); accumulatedData.clear(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } }
@J-Hilk it works very well thank you very much but please how I can read just the last line
-
@J-Hilk it works very well thank you very much but please how I can read just the last line
Hi
it doesn't work like that :)Say you send "This is my line" from the board.
then you get more than one read signal and data comes like
Thi
is m
y li
neso you need to add them together to make a line of it.
In this case we know we got all data when we see #there is no last line. it comes in bytes and we make a line from that.
-
Hi
it doesn't work like that :)Say you send "This is my line" from the board.
then you get more than one read signal and data comes like
Thi
is m
y li
neso you need to add them together to make a line of it.
In this case we know we got all data when we see #there is no last line. it comes in bytes and we make a line from that.
@mrjj with this code the problem is solved but I would need to read every time I press the button just the last line and not all the lines
the code :
QByteArray accumulatedData;
QByteArray expectedEnd= QByteArray::fromHex("2320");
void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
// qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
accumulatedData.append(data_sonde);if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); accumulatedData.clear();
//lire les données*************************
QStringList line ;
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qDebug()<<"File not exists";
} else {
QTextStream lire_data(&file);
while (!lire_data.atEnd()) {
line =lire_data.readLine().split(' ');
if(!line.contains(("para")))
{
if(!line.contains(("data")))
{
if(!line.contains(("#")))
{bool ok; float ph =line.at(0).toDouble(&ok); if(!ok) continue; qDebug()<<ph; ui->sonde_label->setNum(ph); qDebug() << line; } } } } file.close(); }
}
}the file.txt
the result
-
Hi
Since you now wait until you see #if(!accumulatedData.endsWith(expectedEnd)) return;
then you might want to use
file.open(QIODevice::Read | QIODevice::Truncate | QIODevice::Text);
and not
file.open(QIODevice::Append)so you only save the complete line once. Else if you test more than once and don't delete the file between,
it will have more than 1 data line /set. Like you show in the notepad image. -
Hi
Since you now wait until you see #if(!accumulatedData.endsWith(expectedEnd)) return;
then you might want to use
file.open(QIODevice::Read | QIODevice::Truncate | QIODevice::Text);
and not
file.open(QIODevice::Append)so you only save the complete line once. Else if you test more than once and don't delete the file between,
it will have more than 1 data line /set. Like you show in the notepad image.@mrjj thank your for your reply but i need file.open(QIODevice::Append) to write on the file
for the first time it reads only one data since in our file there is only one data but when I press the button for the second time it reads 2 data since we have 2 data in the file but I would need to read just the last datafor the first time
for the second time
-
@mrjj thank your for your reply but i need file.open(QIODevice::Append) to write on the file
for the first time it reads only one data since in our file there is only one data but when I press the button for the second time it reads 2 data since we have 2 data in the file but I would need to read just the last datafor the first time
for the second time
Why ?
it will only write a full line since we wait until we see #
so when your load part runs, then it can load the full line.
Using append when opening the files allows appending more to it next time.
but unless board sent many different data lines and you only want the last one, I'm not sure why
you need append ?(update) Ahh. you do want to send more than one data line and store all in a file but then later
only use the last data ? -
QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); lire_data.seek(file.size() - accumulatedData.size()); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } accumulatedData.clear(); //We now clear at the very end }
-
QByteArray accumulatedData; QByteArray expectedEnd= QByteArray::fromHex("2320"); void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return; //ecrire les données*********************** QFile file("data_sonde.txt"); QStringList line_data ; if( file.open(QIODevice::Append)) { QTextStream data(&file); data<<accumulatedData; file.close(); //lire les données************************* QStringList line ; if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"File not exists"; } else { QTextStream lire_data(&file); lire_data.seek(file.size() - accumulatedData.size()); while (!lire_data.atEnd()) { qDebug() << lire_data.readLine(); } file.close(); } } accumulatedData.clear(); //We now clear at the very end }
@J-Hilk
Cool but wont he just get the last # and not the data line ? -
@mrjj he shouldn't, wee accumulate the whole message, before we actually write:
void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return;
-
@mrjj he shouldn't, wee accumulate the whole message, before we actually write:
void MainWindow::updateGUI_sonde(QByteArray data_sonde) { qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one accumulatedData.append(data_sonde); if(!accumulatedData.endsWith(expectedEnd)) return;
@J-Hilk @mrjj thank you it works very well