Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading and writing into a text file at the same time
Forum Updated to NodeBB v4.3 + New Features

Reading and writing into a text file at the same time

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anfedres
    wrote on last edited by
    #1

    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.

    J.HilkJ 1 Reply Last reply
    0
    • A anfedres

      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.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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);


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      A 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @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);

        A Offline
        A Offline
        anfedres
        wrote on last edited by
        #3

        @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.

        J.HilkJ 1 Reply Last reply
        0
        • A anfedres

          @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.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Small improvement over @J-Hilk suggestions:

            signals:
              void newLineRead(const QByteArray& newData);
            

            This will avoid useless copies.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved