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. QSerialPort with Thread miss same data
Qt 6.11 is out! See what's new in the release blog

QSerialPort with Thread miss same data

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 6 Posters 17.1k Views 2 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.
  • G Offline
    G Offline
    Gokhan
    wrote on last edited by
    #1

    Hi everyone,
    I'm using QSerialPort with a thread to avoid missing data, but I didn't. Actually, while the program is idle, so it is on the screen and PC is not performing any other process, it doesn't miss any data. However, during the program is maximized/minimized or the window is resized, it always misses some data, although using with thread. I didn't solve this problem. You can see the codes below.

    I have two classes, one is a serial port and other is mainwindow for an application. The serial port extends from QThread and this thread is started in main class' constructor.

    The MainWindow Constructor:

        SerialPort *serial= new SerialPort();
        serial->moveToThread(&cThread);
    
        connect(this , &MainWindow::finished, &cThread, &QThread::quit);
        connect(this , &MainWindow::destroyed, this, &SerialPort::deleteLater);
        connect(serial, &SerialPort::getData, this, &MainWindow::displayData);
    
        cThread.start();
    

    The Serial Port read thread:

    void SerialPort::newData()
    {
         mutex.lock();
         bArray.insert(bArray.length(),serial->readAll());
         mutex.unlock();
    
         emit getData(&bArray,&mutex);
    }
    

    The SerialPort Constructor:

        serial = new QSerialPort();
        connect(serial, &QSerialPort::readyRead, this,  &SerialPort::newData);
    
        bArray.clear();
    
        fill_serial_ports();
        portName = find_stm32_port();
    
        serial->setReadBufferSize(20*1024*1024); // it's a huge buffer.
        bArray.reserve(20*1024*1024);  // it's a huge buffer.
    
        open_serial_port();
    
    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      Hi, in order to manage your stm32 microcontroller using QThread you shouldn't use the events approach but the functions

      waitForReadyRead
      readAll

      Please have a look at the way that the stm32 microcontroller is sending data. Are you managing the buffers?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      G 1 Reply Last reply
      0
      • mrdebugM mrdebug

        Hi, in order to manage your stm32 microcontroller using QThread you shouldn't use the events approach but the functions

        waitForReadyRead
        readAll

        Please have a look at the way that the stm32 microcontroller is sending data. Are you managing the buffers?

        G Offline
        G Offline
        Gokhan
        wrote on last edited by
        #3

        @mrdebug How should I do this? We had done this reading periodically, but someone said that it's wrong method, and they said that you should do like in my previous message.

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #4

          Each one has a different way to write code.
          After hundreds of big installations managing a very lot of different microcontrollers with different protocols I suggest you to use events (connect) where you don't use QThread and viceversa.
          My installations work perfecly, without missing data, h24, without to stop for months.

          At this point please do it how you prefer. My idea was only to send an advise.

          Regards.

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gokhan
            wrote on last edited by Gokhan
            #5

            @mrdebug I also tried without QThread only using readyRead event, then it missed data too.
            I guess to say again, actually while it's idle, not missing, while only GUI's window is being resized or minimized/maximized.It looks as if the serial port is close. Please, you try this condition, even you could hold left mouse button on the windows' border and not leave, then you will see the same problem.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gokhan
              wrote on last edited by
              #6

              I have been struggling with this problem for two weeks, I wrote a bug report about this condition, Assignee said that QSerialPort never loses data, furthermore my code is wrong. I'm very confused, I can't find any solution, don't know where to make mistakes.
              https://bugreports.qt.io/browse/QTBUG-61233

              jsulmJ 1 Reply Last reply
              0
              • mrdebugM Offline
                mrdebugM Offline
                mrdebug
                wrote on last edited by
                #7

                It is not so easy to help you without to have the full source and without to know what your microcontroller does.

                Need programmers to hire?
                www.labcsp.com
                www.denisgottardello.it
                GMT+1
                Skype: mrdebug

                1 Reply Last reply
                2
                • G Gokhan

                  I have been struggling with this problem for two weeks, I wrote a bug report about this condition, Assignee said that QSerialPort never loses data, furthermore my code is wrong. I'm very confused, I can't find any solution, don't know where to make mistakes.
                  https://bugreports.qt.io/browse/QTBUG-61233

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Gokhan You should post you code without QThread. Multi threading isn't needed actually as you can use signals/slots (not events) and just makes you code more complex.

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

                  G 1 Reply Last reply
                  0
                  • mrdebugM Offline
                    mrdebugM Offline
                    mrdebug
                    wrote on last edited by
                    #9

                    Please try to download this source of an application Qt based that uses QSerialPort
                    https://www.linux-apps.com/content/show.php/QtComPort?content=142378
                    It uses event approach. Please have a look that if it can help you.
                    The application can put in bridge two serial ports in order to monitor what is passing through.

                    Need programmers to hire?
                    www.labcsp.com
                    www.denisgottardello.it
                    GMT+1
                    Skype: mrdebug

                    1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @Gokhan You should post you code without QThread. Multi threading isn't needed actually as you can use signals/slots (not events) and just makes you code more complex.

                      G Offline
                      G Offline
                      Gokhan
                      wrote on last edited by
                      #10

                      @jsulm and @mrdebug I just now tried the terminal program in examples and added only a few lines code in order to start the MCU. It too missed data.
                      If USB_Can converter's red led is on, it means that there is a missing transfer. However, while Hercules that is a serial port terminal program using, there is no missing data.
                      You can see this in below video.
                      Link : https://drive.google.com/file/d/0B7WvtYBLRY0YVFFmRzZvWWZKVFk/view

                      J.HilkJ 1 Reply Last reply
                      0
                      • mrdebugM Offline
                        mrdebugM Offline
                        mrdebug
                        wrote on last edited by
                        #11

                        I suggest you to use QtComPort and watch the output in hex format.
                        After that please post the output.
                        Which kind of protocol are you using? Is there a header, a footer, a crc...???

                        Need programmers to hire?
                        www.labcsp.com
                        www.denisgottardello.it
                        GMT+1
                        Skype: mrdebug

                        1 Reply Last reply
                        0
                        • G Gokhan

                          @jsulm and @mrdebug I just now tried the terminal program in examples and added only a few lines code in order to start the MCU. It too missed data.
                          If USB_Can converter's red led is on, it means that there is a missing transfer. However, while Hercules that is a serial port terminal program using, there is no missing data.
                          You can see this in below video.
                          Link : https://drive.google.com/file/d/0B7WvtYBLRY0YVFFmRzZvWWZKVFk/view

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

                          @Gokhan I would say that is expected behaviour. By "resizing" the gui your main thread is blocked/busy with that.

                          But the statement is still true, QSerialport does not loose data.
                          Your device probably runs in an timeout because it gets no answer from your serialport, because the old data was not collected.

                          Seems like Threading your SerialPort-communictation is the only option in this particular case.


                          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.

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

                            @Gokhan I would say that is expected behaviour. By "resizing" the gui your main thread is blocked/busy with that.

                            But the statement is still true, QSerialport does not loose data.
                            Your device probably runs in an timeout because it gets no answer from your serialport, because the old data was not collected.

                            Seems like Threading your SerialPort-communictation is the only option in this particular case.

                            G Offline
                            G Offline
                            Gokhan
                            wrote on last edited by
                            #13

                            @J.Hilk However, when I periodically read the serial port buffer via a thread for 100 ms, it returns the null data, so the serial port buffer is seeming empty and the device is giving the error again.

                            J.HilkJ 1 Reply Last reply
                            0
                            • G Gokhan

                              @J.Hilk However, when I periodically read the serial port buffer via a thread for 100 ms, it returns the null data, so the serial port buffer is seeming empty and the device is giving the error again.

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

                              @Gokhan
                              I downloaded your code,

                              take a look here and here and create your QThread, how it should be done ;-)

                              And don't use the blocking waitForXxx functions, use signal slots with connects.


                              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.

                              G 1 Reply Last reply
                              1
                              • J.HilkJ J.Hilk

                                @Gokhan
                                I downloaded your code,

                                take a look here and here and create your QThread, how it should be done ;-)

                                And don't use the blocking waitForXxx functions, use signal slots with connects.

                                G Offline
                                G Offline
                                Gokhan
                                wrote on last edited by Gokhan
                                #15

                                @J.Hilk thank you, actually, first I wrote it like in my first post, and after that, I saw that it's not working, started to try all possibilities. I guess that there is a bug in Qt, because the assignee wrote a bug about this subject.
                                https://bugreports.qt.io/browse/QTBUG-61237

                                @mrdebug I have gotten lots of errors with this Project QtComPort, I didn't compile it, guess that it needs a Linux PC.

                                1 Reply Last reply
                                0
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16
                                  • I guess that there is a bug in Qt, because the assignee wrote a bug about this subject.
                                    https://bugreports.qt.io/browse/QTBUG-61237

                                  Hi.
                                  It is not in Qt. its a windows/platform thing.
                                  It is a known thing with windows.
                                  It applies to all apps. Also those not written in Qt.
                                  https://stackoverflow.com/questions/18041622/how-do-i-stop-windows-from-blocking-the-program-during-a-window-drag-or-menu-but

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    Gokhan
                                    wrote on last edited by
                                    #17

                                    @mrjj However, another program is working well like you can see my video. Is this big problem for application? e.g my application must immediately give an error in this condition and stop the communication.
                                    What do you suggest me in order to solve this problem? I had used the LabVIEW before and developed like this application, it had worked flawlessly.

                                    mrjjM 1 Reply Last reply
                                    0
                                    • G Gokhan

                                      @mrjj However, another program is working well like you can see my video. Is this big problem for application? e.g my application must immediately give an error in this condition and stop the communication.
                                      What do you suggest me in order to solve this problem? I had used the LabVIEW before and developed like this application, it had worked flawlessly.

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Gokhan
                                      The normal solution is to use a thread.
                                      Then it wont be interrupted when dragging/resizing.

                                      1 Reply Last reply
                                      1
                                      • K Offline
                                        K Offline
                                        kuzulis
                                        Qt Champions 2020
                                        wrote on last edited by kuzulis
                                        #19

                                        @Gokhan,

                                        I have added for you an example "qsp-no-freeze-workaround-windows" which demonstrates how it should work with threads!

                                        G 1 Reply Last reply
                                        1
                                        • K kuzulis

                                          @Gokhan,

                                          I have added for you an example "qsp-no-freeze-workaround-windows" which demonstrates how it should work with threads!

                                          G Offline
                                          G Offline
                                          Gokhan
                                          wrote on last edited by
                                          #20

                                          @kuzulis Thank you very much, it's really working well. My first code that is in my first post here looks like this, but there is one different between them, it is inheritance. Is this a problem?

                                          Also, how should I use qmutex between two classes in order to syncrhronşze? It's not declared the static and global variable is not recommended for it.

                                          mrjjM 1 Reply Last reply
                                          0

                                          • Login

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