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.

    Code:

        QTextStream in(&file);
        QTextStream out(&file);
    
        QByteArray datas;
        datas+=Totem_Serial.readAll();
        in<<(QString)datas;
        
        QString line;
        line=out.readLine();
    

    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.

    I;8;05;4A;72;EB;FF;BF;FF;
    D2;F7;E3;FF;13;00;F5;FF;1F;00;40;2B;00;00;01;01\n\r
    I;9;05;4A;82;F1;FF;B3;FF;D7;F7;E9;FF;FA;FF;FF;FF;1F;00;00;3B;00;00;01;01\n\r
    I;0;05;4A;82;F2;FF;C1;FF;D7;F7;EB;FF;F5;FF;05;0
    0;1F;00;60;27;00;00;01;01
    I;1;05;4
    A;92;F5;FF;BE;FF;D5;F7;E9;FF;FF;FF;FD;FF;1F;00;00;3B;00;00;01;01\n\r

    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;8;05;4A;72;EB;FF;BF;FF;D2;F7;E3;FF;13;00;F5;FF;1F;00;40;2B;00;00;01;01
    I;9;05;4A;82;F1;FF;B3;FF;D7;F7;E9;FF;FA;FF;FF;FF;1F;00;00;3B;00;00;01;01
    I;0;05;4A;82;F2;FF;C1;FF;D7;F7;EB;FF;F5;FF;05;00;1F;00;60;27;00;00;01;01
    I;1;05;4A;92;F5;FF;BE;FF;D5;F7;E9;FF;FF;FF;FD;FF;1F;00;00;3B;00;00;01;01

    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.



  • @anfedres

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

    //in the header 
    QByteArray myData;
    
    signals:
      void newLineRead(QByteArray newData);
    
    
    //gettingData
    myData +=Totem_Serial.readAll();
    
    //Check if whole line was read and extract it 
    int i = myData.indexOf("\n\r");
    if(i>0){
        emit newLineRead(myData.mid(0,i));
        myData.remove(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

    I;1;05;4
    A;92;F5;FF;BE;FF;D5;F7;E9;FF;FF;FF;FD;FF;1F;00;00;3B;00;00;01;01\n\r

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



  • @anfedres

    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

    Hi,

    Small improvement over @J-Hilk suggestions:

    signals:
      void newLineRead(const QByteArray& newData);
    

    This will avoid useless copies.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.