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 5.6k 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 sanjay13

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

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    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 Offline
            Christian EhrlicherC Offline
            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
            • Christian EhrlicherC Christian Ehrlicher

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

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

              @Christian-Ehrlicher im doing this in windows
              @ChrisW67 the output for the code you have provided
              Time: 14043 ms
              count: 1
              12 sendingg___size
              Time: 14053 ms
              count: 2
              12 receive___size
              0 receive___size
              0 receive___size
              0 receive___size
              0 receive___size
              0 receive___size
              0 receive___size

              like this after some time it is 12 receive then it becomes 0

              C 1 Reply Last reply
              0
              • S sanjay13

                @Christian-Ehrlicher im doing this in windows
                @ChrisW67 the output for the code you have provided
                Time: 14043 ms
                count: 1
                12 sendingg___size
                Time: 14053 ms
                count: 2
                12 receive___size
                0 receive___size
                0 receive___size
                0 receive___size
                0 receive___size
                0 receive___size
                0 receive___size

                like this after some time it is 12 receive then it becomes 0

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

                @sanjay13 My code does not output anything.

                S 1 Reply Last reply
                0
                • C ChrisW67

                  @sanjay13 My code does not output anything.

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

                  @ChrisW67 i have used the connect part and cheched with qdebug

                  Time: 8164 ms
                  count: 1
                  12 receive___size
                  12 sendingg___size
                  Time: 8177 ms
                  count: 2
                  12 receive___size
                  12 sendingg___size
                  Time: 8189 ms
                  count: 3
                  12 receive___size
                  12 sendingg___size
                  Time: 8204 ms
                  count: 4
                  12 receive___size
                  12 sendingg___size
                  this is the output for 15,ms it is working perfectly but i need this for 3ms

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • S sanjay13

                    @ChrisW67 i have used the connect part and cheched with qdebug

                    Time: 8164 ms
                    count: 1
                    12 receive___size
                    12 sendingg___size
                    Time: 8177 ms
                    count: 2
                    12 receive___size
                    12 sendingg___size
                    Time: 8189 ms
                    count: 3
                    12 receive___size
                    12 sendingg___size
                    Time: 8204 ms
                    count: 4
                    12 receive___size
                    12 sendingg___size
                    this is the output for 15,ms it is working perfectly but i need this for 3ms

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #24

                    Sometimes I wonder about the ignorancy of people here. As we already said many times - your requirement is not achievable - neither with Qt nor with windows. There are so many buffers invovlved and the normal time slices / multitasking is doing the rest. Use a real-time operating system if you have this requiremnt. You will never be able to to something with windows and it's also not even needed in any case (except satisfying the paper with the requirement).

                    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

                      Sometimes I wonder about the ignorancy of people here. As we already said many times - your requirement is not achievable - neither with Qt nor with windows. There are so many buffers invovlved and the normal time slices / multitasking is doing the rest. Use a real-time operating system if you have this requiremnt. You will never be able to to something with windows and it's also not even needed in any case (except satisfying the paper with the requirement).

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

                      @Christian-Ehrlicher ok thankyou for your reply.

                      1 Reply Last reply
                      0
                      • S sanjay13 has marked this topic as solved on

                      • Login

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