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. serialport communication through Qthread
Qt 6.11 is out! See what's new in the release blog

serialport communication through Qthread

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 4 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.
  • S Offline
    S Offline
    sanjay13
    wrote on last edited by
    #1

    void Recivingdatata::run()
    {
    while (!stopped) {

        while (m_serial->waitForReadyRead(15))
        {
            QByteArray data;
            data.append(m_serial->readAll());
            qDebug() <<data.size()<<"receive___size";
    
            {
                QMutexLocker locker(&dataMutex);
                dataQueue.enqueue(data);
            }
    
            // Emit signal for asynchronous data processing
            emit dataReceived(QList<QByteArray>() << data);
            data.clear();
    
    
            msleep(15); // Reduced sleep duration to 1 millisecond
        }
    }
    

    }
    This is my code now i need to reduce msleep to 3 ms and waitforreadyread also to 3ms but if i do im not getting data properly

    Ronel_qtmasterR 1 Reply Last reply
    0
    • S sanjay13

      void Recivingdatata::run()
      {
      while (!stopped) {

          while (m_serial->waitForReadyRead(15))
          {
              QByteArray data;
              data.append(m_serial->readAll());
              qDebug() <<data.size()<<"receive___size";
      
              {
                  QMutexLocker locker(&dataMutex);
                  dataQueue.enqueue(data);
              }
      
              // Emit signal for asynchronous data processing
              emit dataReceived(QList<QByteArray>() << data);
              data.clear();
      
      
              msleep(15); // Reduced sleep duration to 1 millisecond
          }
      }
      

      }
      This is my code now i need to reduce msleep to 3 ms and waitforreadyread also to 3ms but if i do im not getting data properly

      Ronel_qtmasterR Offline
      Ronel_qtmasterR Offline
      Ronel_qtmaster
      wrote on last edited by
      #2

      @sanjay13 You do not need QThread to read data from serial port since the main thread can support it.it is only in case that a task is capable of making the main thread freeze of slow down that you can use thread

      S 1 Reply Last reply
      0
      • Ronel_qtmasterR Ronel_qtmaster

        @sanjay13 You do not need QThread to read data from serial port since the main thread can support it.it is only in case that a task is capable of making the main thread freeze of slow down that you can use thread

        S Offline
        S Offline
        sanjay13
        wrote on last edited by
        #3

        @Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.

        C Ronel_qtmasterR 2 Replies Last reply
        0
        • S sanjay13

          @Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @sanjay13 The only problem description we have is, "the receiving is not properly happening." Since your requirement can be met entirely without a separate thread, delays, or timeouts it is hard to justify fixing this logic. Why have you decided you need to do this?

          S 1 Reply Last reply
          0
          • S sanjay13

            @Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.

            Ronel_qtmasterR Offline
            Ronel_qtmasterR Offline
            Ronel_qtmaster
            wrote on last edited by
            #5

            @sanjay13 okay 12 bytes then why you need a timer?just create a serilport object , open the port in read Mode, and every time data is present, you have the signal readyRead().So you does not need a timer here

            S 1 Reply Last reply
            1
            • Ronel_qtmasterR Ronel_qtmaster

              @sanjay13 okay 12 bytes then why you need a timer?just create a serilport object , open the port in read Mode, and every time data is present, you have the signal readyRead().So you does not need a timer here

              S Offline
              S Offline
              sanjay13
              wrote on last edited by
              #6

              @Ronel_qtmaster no i need to read 12byts of data every 3ms from serialport it works correctly for 15 ms wait time but when i try for 3ms the data is getting appended before or after like im getting 24 byts sometime 48 ,36,0 like that i need 12 byts every 3ms

              @sanjay13 said in serialport communication through Qthread:

              @Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.

              Ronel_qtmasterR 1 Reply Last reply
              0
              • C ChrisW67

                @sanjay13 The only problem description we have is, "the receiving is not properly happening." Since your requirement can be met entirely without a separate thread, delays, or timeouts it is hard to justify fixing this logic. Why have you decided you need to do this?

                S Offline
                S Offline
                sanjay13
                wrote on last edited by
                #7

                @ChrisW67 needed by the company

                1 Reply Last reply
                0
                • S sanjay13

                  @Ronel_qtmaster no i need to read 12byts of data every 3ms from serialport it works correctly for 15 ms wait time but when i try for 3ms the data is getting appended before or after like im getting 24 byts sometime 48 ,36,0 like that i need 12 byts every 3ms

                  @sanjay13 said in serialport communication through Qthread:

                  @Ronel_qtmaster i have tried with timer of 3ms but the receiving is not properly happening i need to read 12 bytes of data from serialport that's my requirement.

                  Ronel_qtmasterR Offline
                  Ronel_qtmasterR Offline
                  Ronel_qtmaster
                  wrote on last edited by Ronel_qtmaster
                  #8

                  @sanjay13 then you do not need QThread here.Take a timer, after 3ms second read the data.When you read pause the timer for exemple.After your readings, restart it

                  S 1 Reply Last reply
                  0
                  • Ronel_qtmasterR Ronel_qtmaster

                    @sanjay13 then you do not need QThread here.Take a timer, after 3ms second read the data.When you read pause the timer for exemple.After your readings, restart it

                    S Offline
                    S Offline
                    sanjay13
                    wrote on last edited by
                    #9

                    @Ronel_qtmaster yess i have also used timer with 3ms but the issue is the data appending before or after like i said sometimes 12 bytes is coming sometimes 36 ,48,0,60 like that its coming but im sending 12bytes of data every 3ms in receiving i have this issue i dont know why.

                    This is not only happening im also sending 12bytes of data to serialport plotting it in graph and this receving part is the issue now

                    C 1 Reply Last reply
                    0
                    • S sanjay13

                      @Ronel_qtmaster yess i have also used timer with 3ms but the issue is the data appending before or after like i said sometimes 12 bytes is coming sometimes 36 ,48,0,60 like that its coming but im sending 12bytes of data every 3ms in receiving i have this issue i dont know why.

                      This is not only happening im also sending 12bytes of data to serialport plotting it in graph and this receving part is the issue now

                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by
                      #10

                      @sanjay13 Receiving data that arrives from the serial port happens when data is received, not to some schedule of consumption you are trying to impose. At 9600 baud you might receive two or three bytes in 3ms, or you might receive none. At 115200 baud you could receive up to 30-ish, or none. It is unlikely you will receive precisely 12 bytes every 3 ms with asynchronous serial.

                      Consuming that data happens at a different rate, in your case you insist that is 12 bytes every 3 ms.

                      Since the two rates do no match you must decouple one from the other.

                      S 1 Reply Last reply
                      1
                      • C ChrisW67

                        @sanjay13 Receiving data that arrives from the serial port happens when data is received, not to some schedule of consumption you are trying to impose. At 9600 baud you might receive two or three bytes in 3ms, or you might receive none. At 115200 baud you could receive up to 30-ish, or none. It is unlikely you will receive precisely 12 bytes every 3 ms with asynchronous serial.

                        Consuming that data happens at a different rate, in your case you insist that is 12 bytes every 3 ms.

                        Since the two rates do no match you must decouple one from the other.

                        S Offline
                        S Offline
                        sanjay13
                        wrote on last edited by
                        #11

                        @ChrisW67 the baud rate here in my code is 921600

                        C 1 Reply Last reply
                        0
                        • S sanjay13

                          @ChrisW67 the baud rate here in my code is 921600

                          C Offline
                          C Offline
                          ChrisW67
                          wrote on last edited by
                          #12

                          @sanjay13 said in serialport communication through Qthread:

                          the baud rate here in my code is 921600

                          So what? This just makes the receive/consume disconnect even greater (approx 92160 bytes/sec received versus 4000 bytes/sec) . Did you consider anything else written in this thread?

                          S 1 Reply Last reply
                          0
                          • C ChrisW67

                            @sanjay13 said in serialport communication through Qthread:

                            the baud rate here in my code is 921600

                            So what? This just makes the receive/consume disconnect even greater (approx 92160 bytes/sec received versus 4000 bytes/sec) . Did you consider anything else written in this thread?

                            S Offline
                            S Offline
                            sanjay13
                            wrote on last edited by
                            #13

                            @ChrisW67 is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data

                            Christian EhrlicherC C 2 Replies Last reply
                            0
                            • S sanjay13

                              @ChrisW67 is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data

                              Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @sanjay13 said in serialport communication through Qthread:

                              is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data

                              You can - QSerialPort will not loose any data. There is absolutely no need to use a QThread for such a task here.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              S 1 Reply Last reply
                              1
                              • Christian EhrlicherC Christian Ehrlicher

                                @sanjay13 said in serialport communication through Qthread:

                                is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data

                                You can - QSerialPort will not loose any data. There is absolutely no need to use a QThread for such a task here.

                                S Offline
                                S Offline
                                sanjay13
                                wrote on last edited by
                                #15

                                @Christian-Ehrlicher ok then how to do this every 3ms i need to receive data from serialport (12 byts)data

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • S sanjay13

                                  @Christian-Ehrlicher ok then how to do this every 3ms i need to receive data from serialport (12 byts)data

                                  Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @sanjay13 As already explained you simply have to connect to the readyRead() signal and in the slot read all data with readAll(). You won't have / get a dedicated 3ms read pattern (and this has nothing to do with Qt).

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  S 1 Reply Last reply
                                  1
                                  • S sanjay13

                                    @ChrisW67 is that not possible to receive 12 bytes of data for every 3ms in timer or thread without losing data

                                    C Offline
                                    C Offline
                                    ChrisW67
                                    wrote on last edited by ChrisW67
                                    #17

                                    @sanjay13 Of course it is possible. Your serial source may be sending far more than that and trying to handle it with a busy-wait polling approach is unnecessary.

                                    Here is a basic (inefficient) example:

                                    #include <QCoreApplication>
                                    #include <QObject>
                                    #include <QSerialPort>
                                    #include <QTimer>
                                    #include <QDebug>
                                    
                                    class SerialHandler: public QObject {
                                        Q_OBJECT
                                    public:
                                        SerialHandler(QObject *p = nullptr)
                                            : QObject(p)
                                            , m_serial(nullptr)
                                            , m_timer(nullptr)
                                        {
                                            m_serial = new QSerialPort(this);
                                            connect(m_serial, &QSerialPort::readyRead, this, &SerialHandler::receiveData);
                                            initSerial();
                                    
                                            m_timer = new QTimer(this);
                                            connect(m_timer, &QTimer::timeout, this, &SerialHandler::sendData);
                                            m_timer->start(3); // this will only be approximate
                                        }
                                        ~SerialHandler() {}
                                    
                                    signals:
                                        void dataForProcessing(const QByteArray &data);
                                    
                                    
                                    private:
                                        void initSerial() {
                                            // setup serial port
                                        }
                                    
                                    private slots:
                                        void receiveData() {
                                            m_buffer+= m_serial->readAll();
                                        }
                                    
                                        void sendData() {
                                            if (m_buffer.length() >= 12) {
                                                const QByteArray data = m_buffer.left(12);
                                                m_buffer = m_buffer.mid(12);
                                                emit dataForProcessing(data);
                                            }
                                        }
                                    
                                    private:
                                        QSerialPort *m_serial;
                                        QTimer *m_timer;
                                        QByteArray m_buffer;  
                                        // ^^ used to decouple reception rate from consumption rate.
                                    };
                                    
                                    int main(int argc, char *argv[])
                                    {
                                        QCoreApplication a(argc, argv);
                                        SerialHandler serial;
                                        return a.exec();
                                    }
                                    #include "main.moc"
                                    

                                    The buffer in the example will grow indefinitely if more than ~4000 bytes are received every second.

                                    1 Reply Last reply
                                    1
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @sanjay13 As already explained you simply have to connect to the readyRead() signal and in the slot read all data with readAll(). You won't have / get a dedicated 3ms read pattern (and this has nothing to do with Qt).

                                      S Offline
                                      S Offline
                                      sanjay13
                                      wrote on last edited by
                                      #18

                                      @Christian-Ehrlicher no i have requirement like that im sending 12 bytes of data every 3ms so i have receive it in 3 ms

                                      C Christian EhrlicherC 2 Replies Last reply
                                      0
                                      • S sanjay13

                                        @Christian-Ehrlicher no i have requirement like that im sending 12 bytes of data every 3ms so i have receive it in 3 ms

                                        C Offline
                                        C Offline
                                        ChrisW67
                                        wrote on last edited by
                                        #19

                                        @sanjay13 You cannot control what you receive. It either arrives or it does not.

                                        1 Reply Last reply
                                        0
                                        • S sanjay13

                                          @Christian-Ehrlicher no i have requirement like that im sending 12 bytes of data every 3ms so i have receive it in 3 ms

                                          Christian EhrlicherC Online
                                          Christian EhrlicherC Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @sanjay13 Then use a real time operating system and not windows or common linux. Your requirement is not achievable with them.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          S 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