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. qextserialport write and progress bar

qextserialport write and progress bar

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 3.9k Views
  • 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
    another_one
    wrote on 25 Apr 2023, 15:33 last edited by
    #1

    Hi !

    Can anybode help in my question about how to use progress bar while getting qextserialport writing?

    I do file transfer via serial port and I need to see the progress of it
    To do file transfer I use Qbytearray as data and "port->write()" as data transfer function.
    And in this way I do not know how to control data transfer to increment till end the progress bar.

    Thus, please help!

    J 1 Reply Last reply 25 Apr 2023, 15:46
    0
    • A another_one
      25 Apr 2023, 15:33

      Hi !

      Can anybode help in my question about how to use progress bar while getting qextserialport writing?

      I do file transfer via serial port and I need to see the progress of it
      To do file transfer I use Qbytearray as data and "port->write()" as data transfer function.
      And in this way I do not know how to control data transfer to increment till end the progress bar.

      Thus, please help!

      J Offline
      J Offline
      JonB
      wrote on 25 Apr 2023, 15:46 last edited by
      #2

      @another_one
      If you do a single write() of the whole of the data you may have a problem.

      If you do many write()s of portions of the data, and you know how big the total data will be and can count how much you have sent so far, then you can update a progress bar correspondingly after each write().

      A 1 Reply Last reply 6 Jun 2023, 18:00
      1
      • J JonB
        25 Apr 2023, 15:46

        @another_one
        If you do a single write() of the whole of the data you may have a problem.

        If you do many write()s of portions of the data, and you know how big the total data will be and can count how much you have sent so far, then you can update a progress bar correspondingly after each write().

        A Offline
        A Offline
        another_one
        wrote on 6 Jun 2023, 18:00 last edited by
        #3

        @JonB
        Hello!
        Can anybody prompt why after compilation project on CenOS the progress bar window is black screen?

        And is it necessary do initializtion of QbyteArray before doing this:

         QByteArray data = port->readAll();
        

        And in case when QByteArray announced as global in public section of MainWindow class and used after as buffer between File reading and trasnferring?

        Thanks!

        S 1 Reply Last reply 6 Jun 2023, 18:44
        0
        • A another_one
          6 Jun 2023, 18:00

          @JonB
          Hello!
          Can anybody prompt why after compilation project on CenOS the progress bar window is black screen?

          And is it necessary do initializtion of QbyteArray before doing this:

           QByteArray data = port->readAll();
          

          And in case when QByteArray announced as global in public section of MainWindow class and used after as buffer between File reading and trasnferring?

          Thanks!

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 6 Jun 2023, 18:44 last edited by
          #4

          Hi,

          Did you take into account the remarks of @JonB ? If not, then there lies your issue. If you send a big file in one big chunk, the event loop will be blocked until all data was sent and thus cannot refresh the UI.

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

          A 1 Reply Last reply 7 Jun 2023, 12:48
          0
          • S SGaist
            6 Jun 2023, 18:44

            Hi,

            Did you take into account the remarks of @JonB ? If not, then there lies your issue. If you send a big file in one big chunk, the event loop will be blocked until all data was sent and thus cannot refresh the UI.

            A Offline
            A Offline
            another_one
            wrote on 7 Jun 2023, 12:48 last edited by
            #5

            @SGaist

            Thank you for your replay!
            Now I see that
            But I still do not undrestand how to do right portion of data..

            S 1 Reply Last reply 7 Jun 2023, 19:18
            0
            • A another_one
              7 Jun 2023, 12:48

              @SGaist

              Thank you for your replay!
              Now I see that
              But I still do not undrestand how to do right portion of data..

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 7 Jun 2023, 19:18 last edited by
              #6

              It's up to you split the file size into some reasonable size. You could use 4k blocks for example.

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

              A 1 Reply Last reply 8 Jun 2023, 12:42
              0
              • S SGaist
                7 Jun 2023, 19:18

                It's up to you split the file size into some reasonable size. You could use 4k blocks for example.

                A Offline
                A Offline
                another_one
                wrote on 8 Jun 2023, 12:42 last edited by another_one 6 Aug 2023, 12:44
                #7

                @SGaist
                Thanks, I did that but still in CentOS instead of progress bar in window I see black screen

                 QByteArray y;   
                  QByteArray arr;   
                if (!file.open(QIODevice::ReadOnly))	
                 {
                	 QMessageBox::information(this, tr("can't open"),
                	 file.errorString());
                	 return;
                 }
                 else
                  {
                				 arr.resize(fls);
                				 for(unsigned long i; i<fls;i++)
                				 {arr = 0;}
                				 arr = file.readAll();
                				 while (!file.atEnd());
                				 file.close();
                
                  }
                 bar->setRange(1, fls); 
                 unsigned long i;
                 for(i=0;i<fls;i++)
                 {
                  bar->setValue(i);
                  y = arr.mid(i, 1 );     
                  port->write(y);
                }
                bar->setValue(fls);
                
                J 1 Reply Last reply 8 Jun 2023, 13:39
                0
                • A another_one
                  8 Jun 2023, 12:42

                  @SGaist
                  Thanks, I did that but still in CentOS instead of progress bar in window I see black screen

                   QByteArray y;   
                    QByteArray arr;   
                  if (!file.open(QIODevice::ReadOnly))	
                   {
                  	 QMessageBox::information(this, tr("can't open"),
                  	 file.errorString());
                  	 return;
                   }
                   else
                    {
                  				 arr.resize(fls);
                  				 for(unsigned long i; i<fls;i++)
                  				 {arr = 0;}
                  				 arr = file.readAll();
                  				 while (!file.atEnd());
                  				 file.close();
                  
                    }
                   bar->setRange(1, fls); 
                   unsigned long i;
                   for(i=0;i<fls;i++)
                   {
                    bar->setValue(i);
                    y = arr.mid(i, 1 );     
                    port->write(y);
                  }
                  bar->setValue(fls);
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Jun 2023, 13:39 last edited by
                  #8

                  @another_one
                  I have no idea whether you have a CentOS issue with physically showing the progress bar. But you do realise that with your code you likely won't see any progress at all, you will simply see the bar at 100% complete at the end? The UI will not be updated during your for loop calling bar->setValue(i) because (so far as I know) port->write() doesn't yield for that, it won't be refreshed till after the bar->setValue(fls).

                  You can test the progress bar drawing with a simple QTimer() timing out, say, once per second and causing in increment; not your code with a for loop or port->write(). that would tell you whether the CentOS progress bar drawing is working.

                  A 1 Reply Last reply 9 Jun 2023, 09:33
                  0
                  • J JonB
                    8 Jun 2023, 13:39

                    @another_one
                    I have no idea whether you have a CentOS issue with physically showing the progress bar. But you do realise that with your code you likely won't see any progress at all, you will simply see the bar at 100% complete at the end? The UI will not be updated during your for loop calling bar->setValue(i) because (so far as I know) port->write() doesn't yield for that, it won't be refreshed till after the bar->setValue(fls).

                    You can test the progress bar drawing with a simple QTimer() timing out, say, once per second and causing in increment; not your code with a for loop or port->write(). that would tell you whether the CentOS progress bar drawing is working.

                    A Offline
                    A Offline
                    another_one
                    wrote on 9 Jun 2023, 09:33 last edited by
                    #9

                    @JonB
                    sorry, i forgot to say what this code on Ws works well
                    I just compile it in cent os and all is work except progress bar bag with black screen instead

                    jsulmJ 1 Reply Last reply 9 Jun 2023, 11:31
                    0
                    • A another_one
                      9 Jun 2023, 09:33

                      @JonB
                      sorry, i forgot to say what this code on Ws works well
                      I just compile it in cent os and all is work except progress bar bag with black screen instead

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on 9 Jun 2023, 11:31 last edited by
                      #10

                      @another_one Your current code can't work. As long as the for loop calling bar->setValue(i) is running Qt event loop is blocked and the progress bar will not update or even be black or whatever.
                      You could call https://doc.qt.io/qt-6/qcoreapplication.html#processEvents inside same loop, but that is a ugly work around. Proper solution in this case would be to move the serial port handling into another thread and emit a signal from that thread every time you want to update the progress bar.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply 9 Jun 2023, 17:44
                      0
                      • jsulmJ jsulm
                        9 Jun 2023, 11:31

                        @another_one Your current code can't work. As long as the for loop calling bar->setValue(i) is running Qt event loop is blocked and the progress bar will not update or even be black or whatever.
                        You could call https://doc.qt.io/qt-6/qcoreapplication.html#processEvents inside same loop, but that is a ugly work around. Proper solution in this case would be to move the serial port handling into another thread and emit a signal from that thread every time you want to update the progress bar.

                        A Offline
                        A Offline
                        another_one
                        wrote on 9 Jun 2023, 17:44 last edited by
                        #11

                        @jsulm
                        Thank you for your advice!
                        I already use serial port handling but only when I read:

                           connect(port, SIGNAL(readyRead()), this, SLOT(readData()));
                        

                        And here I use readyRead() emiting
                        In my byte by byte writing case, can I use bytesWritten(one byte) to increment, in thread, the progress bar ?

                        J 1 Reply Last reply 9 Jun 2023, 17:58
                        0
                        • A another_one
                          9 Jun 2023, 17:44

                          @jsulm
                          Thank you for your advice!
                          I already use serial port handling but only when I read:

                             connect(port, SIGNAL(readyRead()), this, SLOT(readData()));
                          

                          And here I use readyRead() emiting
                          In my byte by byte writing case, can I use bytesWritten(one byte) to increment, in thread, the progress bar ?

                          J Offline
                          J Offline
                          JonB
                          wrote on 9 Jun 2023, 17:58 last edited by
                          #12

                          @another_one

                          to increment, in thread, the progress bar ?

                          If you use another thread to do the serial port stuff you must not directly update the progress bar, you must send a signal from the serial thread to the main UI thread, like @jsulm wrote.

                          A 1 Reply Last reply 15 Jun 2023, 12:03
                          0
                          • J JonB
                            9 Jun 2023, 17:58

                            @another_one

                            to increment, in thread, the progress bar ?

                            If you use another thread to do the serial port stuff you must not directly update the progress bar, you must send a signal from the serial thread to the main UI thread, like @jsulm wrote.

                            A Offline
                            A Offline
                            another_one
                            wrote on 15 Jun 2023, 12:03 last edited by
                            #13

                            @JonB
                            Thank you, now I undestand for what I should do it
                            But can't imagine the construction itself
                            Can anybody give me an example?, please

                            J 1 Reply Last reply 15 Jun 2023, 12:10
                            0
                            • A another_one
                              15 Jun 2023, 12:03

                              @JonB
                              Thank you, now I undestand for what I should do it
                              But can't imagine the construction itself
                              Can anybody give me an example?, please

                              J Offline
                              J Offline
                              JonB
                              wrote on 15 Jun 2023, 12:10 last edited by
                              #14

                              @another_one
                              What is there to say? Above you have code to connect() signal and slot, you need to do a connect here too. The https://doc.qt.io/qt-6/signalsandslots.html#a-small-example is enough to follow for your case.

                              A 1 Reply Last reply 16 Jun 2023, 12:19
                              0
                              • J JonB
                                15 Jun 2023, 12:10

                                @another_one
                                What is there to say? Above you have code to connect() signal and slot, you need to do a connect here too. The https://doc.qt.io/qt-6/signalsandslots.html#a-small-example is enough to follow for your case.

                                A Offline
                                A Offline
                                another_one
                                wrote on 16 Jun 2023, 12:19 last edited by
                                #15

                                @JonB

                                Thanks!
                                Ive done another thread but it got worse
                                First I set global variable of progress bar/ data iteration

                                unsigned long ii = 0;
                                

                                After Ive done the link:

                                 connect(port, SIGNAL(bytesWritten(qint64)), this, SLOT(writeData(qint64)));
                                void monMK::writeData(qint64)
                                {
                                bar->setValue(ii);
                                 qDebug() << "ii progress bar value" << ii;
                                }
                                void mM::on_open_folder_clicked()
                                {// from button clicked
                                ....
                                ....
                                ....
                                
                                   bar = new QProgressDialog(this);
                                
                                           port->write(dataArr4);
                                           bar->setLabelText(tr("loading..."));
                                           bar->setCancelButtonText(tr("&Cancel"));
                                          
                                           bar->setMinimumDuration(0); 
                                           bar->setWindowTitle(tr("Loading"));
                                           bar->setVisible(1);
                                           qApp->processEvents();
                                           bar->setValue(1);
                                           bar->setRange(1, fls-1); // where fls size of File stored
                                        
                                           for(ii=0;ii<fls;ii++)
                                           {
                                            y = arr.mid(ii, 1 );     /
                                            port->write(y,1);
                                            //port->waitForBytesWritten(1);
                                           }
                                ....
                                }
                                

                                As result I see progress bar appering and stopping on 0 %
                                While in debug i see:
                                "
                                ii progress bar value 0
                                // several time waiting
                                ii progress bar value 77457
                                "
                                So please help!

                                jsulmJ 1 Reply Last reply 16 Jun 2023, 12:52
                                0
                                • A another_one
                                  16 Jun 2023, 12:19

                                  @JonB

                                  Thanks!
                                  Ive done another thread but it got worse
                                  First I set global variable of progress bar/ data iteration

                                  unsigned long ii = 0;
                                  

                                  After Ive done the link:

                                   connect(port, SIGNAL(bytesWritten(qint64)), this, SLOT(writeData(qint64)));
                                  void monMK::writeData(qint64)
                                  {
                                  bar->setValue(ii);
                                   qDebug() << "ii progress bar value" << ii;
                                  }
                                  void mM::on_open_folder_clicked()
                                  {// from button clicked
                                  ....
                                  ....
                                  ....
                                  
                                     bar = new QProgressDialog(this);
                                  
                                             port->write(dataArr4);
                                             bar->setLabelText(tr("loading..."));
                                             bar->setCancelButtonText(tr("&Cancel"));
                                            
                                             bar->setMinimumDuration(0); 
                                             bar->setWindowTitle(tr("Loading"));
                                             bar->setVisible(1);
                                             qApp->processEvents();
                                             bar->setValue(1);
                                             bar->setRange(1, fls-1); // where fls size of File stored
                                          
                                             for(ii=0;ii<fls;ii++)
                                             {
                                              y = arr.mid(ii, 1 );     /
                                              port->write(y,1);
                                              //port->waitForBytesWritten(1);
                                             }
                                  ....
                                  }
                                  

                                  As result I see progress bar appering and stopping on 0 %
                                  While in debug i see:
                                  "
                                  ii progress bar value 0
                                  // several time waiting
                                  ii progress bar value 77457
                                  "
                                  So please help!

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 16 Jun 2023, 12:52 last edited by
                                  #16

                                  @another_one What is the size of the file?
                                  There is no need for any global variables, simply use the parameter of your slot:

                                  void monMK::writeData(qint64 ii)
                                  {
                                   bar->setValue(ii);
                                   qDebug() << "ii progress bar value" << ii;
                                  }
                                  

                                  You should stop using old connect syntax.
                                  The actual issue is that you write the data in one blocking loop inside on_open_folder_clicked() - this blocks the Qt event loop.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  A 1 Reply Last reply 19 Jun 2023, 09:37
                                  1
                                  • jsulmJ jsulm
                                    16 Jun 2023, 12:52

                                    @another_one What is the size of the file?
                                    There is no need for any global variables, simply use the parameter of your slot:

                                    void monMK::writeData(qint64 ii)
                                    {
                                     bar->setValue(ii);
                                     qDebug() << "ii progress bar value" << ii;
                                    }
                                    

                                    You should stop using old connect syntax.
                                    The actual issue is that you write the data in one blocking loop inside on_open_folder_clicked() - this blocks the Qt event loop.

                                    A Offline
                                    A Offline
                                    another_one
                                    wrote on 19 Jun 2023, 09:37 last edited by another_one
                                    #17

                                    @jsulm said in qextserialport write and progress bar:
                                    What is the size of the file?*
                                    Thank you for your replay!
                                    The size of File is 77457 Bytes
                                    How can I transfer ii value from "button clicked" to slot writeData?, via port->waitForBytesWritten(ii);?
                                    You should stop using old connect syntax.
                                    The actual issue is that you write the data in one blocking loop inside
                                    on_open_folder_clicked() - this blocks the Qt event loop.*
                                    Sorry, here, again, I didnt undestand
                                    How can I fdo write data(write the File) in non-blocking state?, Can you give me an example how its possible to do unblocked file writing to qextserialport?, please
                                    Cause I didnt undestand how "Code of Conduct" can help in this case
                                    Thanks in advance!

                                    jsulmJ 1 Reply Last reply 19 Jun 2023, 09:52
                                    0
                                    • A another_one
                                      19 Jun 2023, 09:37

                                      @jsulm said in qextserialport write and progress bar:
                                      What is the size of the file?*
                                      Thank you for your replay!
                                      The size of File is 77457 Bytes
                                      How can I transfer ii value from "button clicked" to slot writeData?, via port->waitForBytesWritten(ii);?
                                      You should stop using old connect syntax.
                                      The actual issue is that you write the data in one blocking loop inside
                                      on_open_folder_clicked() - this blocks the Qt event loop.*
                                      Sorry, here, again, I didnt undestand
                                      How can I fdo write data(write the File) in non-blocking state?, Can you give me an example how its possible to do unblocked file writing to qextserialport?, please
                                      Cause I didnt undestand how "Code of Conduct" can help in this case
                                      Thanks in advance!

                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 19 Jun 2023, 09:52 last edited by
                                      #18

                                      @another_one Please take a look at this example application: https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      A 1 Reply Last reply 19 Jun 2023, 17:41
                                      0
                                      • jsulmJ jsulm
                                        19 Jun 2023, 09:52

                                        @another_one Please take a look at this example application: https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html

                                        A Offline
                                        A Offline
                                        another_one
                                        wrote on 19 Jun 2023, 17:41 last edited by
                                        #19

                                        @jsulm

                                        Thank you for this example!
                                        It' seems to be what I need, but I wanted to clarify about progress bar using in SerialPortWriter class, - can I increment progress bar in :handleBytesWritten ??

                                        Thank you!

                                        S 1 Reply Last reply 19 Jun 2023, 18:12
                                        0
                                        • A another_one
                                          19 Jun 2023, 17:41

                                          @jsulm

                                          Thank you for this example!
                                          It' seems to be what I need, but I wanted to clarify about progress bar using in SerialPortWriter class, - can I increment progress bar in :handleBytesWritten ??

                                          Thank you!

                                          S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 19 Jun 2023, 18:12 last edited by
                                          #20

                                          @another_one emit a signal from that class and connect that one to the progress bar. That way you avoid leaking GUI knowledge in your business logic class.

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

                                          A 1 Reply Last reply 21 Jun 2023, 18:28
                                          2

                                          • Login

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