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