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.2k 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.
  • S Offline
    S Offline
    Sen Li
    wrote on last edited by Sen Li
    #1

    Hi,

    I am a newbie working on multi-sensors control, with each sensor passing data to one Serial Port independently.
    The aim is to retrieve data as fast as possible, which means I want to control multi-Sensors through multi-SerialPorts simultaneously and independently, instead of merging data into one Serial Port or processing the serial communications in only one loop.

    Here is my Question: is that possible in Qt to do multiple serial-threads simultaneously and independently?

    It keeps telling me
    QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread
    whenever I tried to open or close a QSerialPort if it happens in a new thread, and of course data are not received successfully.

    There must be one micro-controller between COM and sensor, while one MCU controlling multi-sensors will definitely slow down the communication speed; the best way is to realize multi-SerialPort control in Qt.

    Could anyone give me some ideas about my problem?

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

      Hi,

      How are you creating your QSerialPort objects ?
      Do you haven on sensor per physical serial port ?

      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

        Hi,

        How are you creating your QSerialPort objects ?
        Do you haven on sensor per physical serial port ?

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

        @SGaist
        Hi,

        Here is my QObject for QSerialPort control, with openSerial, closeSerial, and algorithm to control how to read data.
        When I put SenSerialControlAlgorithmObject into a thread, it keeps telling me
        QWinEventNotifier: Event notifiers cannot be enabled or disabled from another thread
        whenever it comes to the openSerial or closeSerial command, and the serial communication doesn't work.

        One Serial Port connects with one MCU to control one sensor.

        #include <QObject>
        #include <QtSerialPort/QSerialPort>
        #include <QTime>
        #include <QElapsedTimer>
        
        class SenSerialControlAlgorithmObject : public QObject
        {
            Q_OBJECT
        public:
            explicit SenSerialControlAlgorithmObject(QObject *parent = 0);
        
            QSerialPort *mySerialPort;
            QByteArray SenMotionByteArray;
            bool motionReadBegun;
            QElapsedTimer *myElapsedTimer;
        
        signals:
            void emitSerialCloseSetting();
            void emitSerialOpenedSetting();
            void emitSerialDataToConsole(QByteArray);
        
            void emitMotionTrackingStart();
            void emitMotionTrackingStop(qint64);
            void emitMotionUpdateSignal(QByteArray SenMotionByteArray, int);
        
        public slots:
            void openSerialPort(Settings mySettingInfo);
            void closeSerialPort();
            void readData();
            void handleError(QSerialPort::SerialPortError error);
        
        private:
            int motionReadBytes, delta_X, delta_Y;
        };
        #endif // SENSERIALCONTROLALGORITHMOBJECT_H
        1 Reply Last reply
        0
        • 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