Reading and writing into a text file at the same time

  • Greetings,

    Description of the program: I have a program that is reading signals using the UART, I'm trying to plot the asynchronous data that I'm getting.


        QTextStream in(&file);
        QTextStream out(&file);
        QByteArray datas;
        QString line;

    Since the communication is asynchronous, I don't have control over the communication at all. I was trying to read the first bit of the packet, but the problem the array stored in datas doesn't come always the same size nor has the initial byte that I'm always looking (in this case would be the letter "I"). The information that I get looks something like this.


    If I wanna grab the information and plot from this array, it would be very hard for me to figure out which is the initial and last byte. But something I can do is writing this into a text file and grab the information from there. When I place in the .txt file, it looks like this.


    I thought that writing the information in the .txt file, and grabbing the information from there already formatted. My plan is to do this on the fly, while I'm streaming packets from the serial, saving them in .txt file and reading them to plot them (That's the intention of the code snippet aforementioned). However, I can't get any consistent reading from the file, it just reads the first 2 or 3 lines, after that, it just blank and the flag out.atEnd() says that it has reached the end of the file.

    The previous explanation leads to the following questions: Is there a better way to read asynchronous data and plot it? If theapproach that I'm thinking is right, is there a way to get some consistent reading and writing at the same time into the .txt file? Suggestions are highly appreciated.

  • Moderators


    that seems way to complicated, let me suggest something different:

    //in the header 
    QByteArray myData;
      void newLineRead(QByteArray newData);
    myData +=Totem_Serial.readAll();
    //Check if whole line was read and extract it 
    int i = myData.indexOf("\n\r");
        emit newLineRead(myData.mid(0,i));

    than do stuff with the bytearray you extracted via the signal newLineRead(QByteArray newData);

  • @J.Hilk Wouldn't this be a problem when the line is not complete? Let's when the mydata did not read the whole stuff. For example


    Wouldn't the previous code discard the first line just because it did not ended with \n\r? Thanks for your suggestion and help.

  • Moderators


    you would be correct, if you don't declare your QByteArray myData in the header of your class.

    Because variables declared inside a function are immediately deleted upon completion of the function.

    But, in my example myData is part of the class and will -usually- only be deleted upon class destruction.

  • Lifetime Qt Champion


    Small improvement over @J-Hilk suggestions:

      void newLineRead(const QByteArray& newData);

    This will avoid useless copies.

Log in to reply