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. [SOLVED]Possible? Multiple QSerialPort Threads Simultaneously Independently

[SOLVED]Possible? Multiple QSerialPort Threads Simultaneously Independently

Scheduled Pinned Locked Moved General and Desktop
qserialportmultiplethreadsindependentsimultaneouslyterminal
18 Posts 3 Posters 10.3k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    How are you putting SenSerialControlAlgorithmObject in a QThread ?

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

    S 2 Replies Last reply
    0
    • SGaistS SGaist

      How are you putting SenSerialControlAlgorithmObject in a QThread ?

      S Offline
      S Offline
      Sen Li
      wrote on last edited by Sen Li
      #5

      @SGaist

      In the constructor in MainWindow of Terminal.
      The MainWindow has ui for Serial Info Setting.

          mySerialThread = new QThread();
      //    SenSerialControlAlgorithmObject ->moveToThread(mySerialThread );
          mySerialThread ->start();
      1 Reply Last reply
      0
      • SGaistS SGaist

        How are you putting SenSerialControlAlgorithmObject in a QThread ?

        S Offline
        S Offline
        Sen Li
        wrote on last edited by Sen Li
        #6

        @SGaist
        MainWindow:

        #include <QMainWindow>
        #include <QtSerialPort/QSerialPort>
        #include "SenConsole.h"
        #include "SenSettingsDialog.h"
        #include "SenSerialControlAlgorithmObject.h"
        
        class SenOpticalFlowSensorsMainWindow : public QMainWindow
        {
        private slots:
            void writeData(const QByteArray &data);
            void showDataInConsole(QByteArray data);
        
        signals:
            void emitOpenSerialOrder(SenSettingsDialog::Settings);
        
        private:
            Ui::SenOpticalFlowSensorsMainWindow *myMainWindow_ui;
            SenConsole *mySenConsole;
            SenSettingsDialog *mySenSettingsDialog;
        
            SenSettingsDialog::Settings mySettingInfo;
            SenSerialControlAlgorithmObject *myDefaulSerialObject;
        
            QThread *mySerialThread;
        };
        

        Constructor:

        SenOpticalFlowSensorsMainWindow::SenOpticalFlowSensorsMainWindow(QWidget *parent) :    QMainWindow(parent),
            myMainWindow_ui(new Ui::SenOpticalFlowSensorsMainWindow)
        {
            myMainWindow_ui->setupUi(this);
            mySenConsole = new SenConsole;
            setCentralWidget(mySenConsole);
        ////
            myDefaulSerialObject = new SenSerialControlAlgorithmObject();
        ////
            mySenSettingsDialog = new SenSettingsDialog;
            initActionsConnections();
        
            mySerialThread = new QThread();
        //    myDefaulSerialObject ->moveToThread(mySerialThread );
            mySerialThread ->start();
        }
        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by
          #7

          Just do not use the threads if you don't know how to do it.

          S 1 Reply Last reply
          0
          • K kuzulis

            Just do not use the threads if you don't know how to do it.

            S Offline
            S Offline
            Sen Li
            wrote on last edited by
            #8

            @kuzulis
            So is it impossible to do multi-Terminal in Qt?

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

              No it's not impossible, however thread programming is a complex matter where it's very very very easy to shoot yourself in the foot.

              One guess: your actual QSerialPort doesn't have a parent, so when your move myDefaultSerialObject, the serial port itself is not moved.

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

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                No it's not impossible, however thread programming is a complex matter where it's very very very easy to shoot yourself in the foot.

                One guess: your actual QSerialPort doesn't have a parent, so when your move myDefaultSerialObject, the serial port itself is not moved.

                S Offline
                S Offline
                Sen Li
                wrote on last edited by Sen Li
                #10

                @SGaist

                QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread

                I appreciate your guessing, however the application output reminder is telling something failed in another thread, which means the command of

                    if (mySerialPort->open(QIODevice::ReadWrite)) {//Here is when the hint comes out
                        emit emitSerialOpenedSetting();
                        syncNumMotionsCount = 0;
                        SenMotionByteArray.clear();
                        debugError = false;
                    } else {
                        emit emitSerialOpenErrorSetting(mySerialPort->errorString());
                    }
                

                failed in another thread instead of main thread.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sen Li
                  wrote on last edited by
                  #11

                  Although it is not allowed to open/close QSerialPort in a new thread, I can open/close it in the main thread, and push it into the new thread after it is opened.

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

                    Although it is not allowed to open/close QSerialPort in a new thread,

                    It is not true. There are no differences in from what a thread to do open/close. A main things is that the QSerialPort should be moved into another thread before than opened/closed/read/write on it and so on. Besides, this should be called in that thread to which the QSerialPort was moved (not from the main thread or any others).

                    And I do not understand what is reason to use a more than one thread.. Because you can handle a data from the several serial ports (sensors) from the one main thread "simultaneous".

                    S 1 Reply Last reply
                    0
                    • K kuzulis

                      Although it is not allowed to open/close QSerialPort in a new thread,

                      It is not true. There are no differences in from what a thread to do open/close. A main things is that the QSerialPort should be moved into another thread before than opened/closed/read/write on it and so on. Besides, this should be called in that thread to which the QSerialPort was moved (not from the main thread or any others).

                      And I do not understand what is reason to use a more than one thread.. Because you can handle a data from the several serial ports (sensors) from the one main thread "simultaneous".

                      S Offline
                      S Offline
                      Sen Li
                      wrote on last edited by
                      #13

                      @kuzulis

                      That is also what I do not understand.

                      However, the truth is:
                      QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread

                      and how will you explain this when it comes to open/close QSerialPort?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sen Li
                        wrote on last edited by
                        #14

                        It seems that, there is no way to push the Object, which contains QSerialPort, back to main thread and then close QSerialPort.

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

                          That is also what I do not understand.

                          What do you don't understand?

                          I once again will repeat: simply don't use the threads, if no reason for this! (in your case there are is no any reason)

                          QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread

                          Obviously, you do something wrong: all methods of QSerialPort shall be called from the same thread in which an QSerialPort lives.

                          It seems that, there is no way to push the Object, which contains QSerialPort, back to main thread and then close QSerialPort.

                          WTF?

                          S 1 Reply Last reply
                          0
                          • K kuzulis

                            That is also what I do not understand.

                            What do you don't understand?

                            I once again will repeat: simply don't use the threads, if no reason for this! (in your case there are is no any reason)

                            QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread

                            Obviously, you do something wrong: all methods of QSerialPort shall be called from the same thread in which an QSerialPort lives.

                            It seems that, there is no way to push the Object, which contains QSerialPort, back to main thread and then close QSerialPort.

                            WTF?

                            S Offline
                            S Offline
                            Sen Li
                            wrote on last edited by Sen Li
                            #16

                            @kuzulis
                            I don't understand what's wrong with my codes; it doesn't work anyway to open/close QSerialPort in a new thread.

                            I made it in main thread, no problem without putting the Serial Communication Control in a new thread; however, is that possible to do multiple serial threads?

                            Two reasons to do threads:

                            1. To make it as fast as possible;
                            2. Learn my bug in programming: I do not want to encounter the same bug again.
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by SGaist
                              #17

                              @Sen-Li

                              Threads are not always the way to make a program go faster. In some cases performance can be worse with threads. Qt's asynchronous nature allows to avoid usage of thread for most of the cases and generally serial port can be handled in the GUI thread without problem.

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

                              S 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @Sen-Li

                                Threads are not always the way to make a program go faster. In some cases performance can be worse with threads. Qt's asynchronous nature allows to avoid usage of thread for most of the cases and generally serial port can be handled in the GUI thread without problem.

                                S Offline
                                S Offline
                                Sen Li
                                wrote on last edited by
                                #18

                                @SGaist

                                Thank you very much for your tips!
                                I probably should give up on the serial thread. It is beyond my ability to play with that problem.

                                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