Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. How to use QTimer in Qt Creator to get data from an external peripheral device for a specific time interval only?
QtWS25 Last Chance

How to use QTimer in Qt Creator to get data from an external peripheral device for a specific time interval only?

Scheduled Pinned Locked Moved Unsolved India
11 Posts 3 Posters 3.0k 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
    SHUBHAM SINGH RAO
    wrote on last edited by SHUBHAM SINGH RAO
    #1

    Aim : I m working on Qt Creator and want to interface Modem with my main programme For that, I need fetch the data from modem for a specific time interval (say 3 seconds) only and after that I have to stop receiving data.

    Present Status 05/11/18 :
    I want to execute a blocking method through which I can wait (say 3 seconds) for data . If I receive data in 3 seconds, I will move ahead for some another task else I will quit. Is there any method except threading?
    I am trying to do it with QEventLoop but no success achieved so far.
    Kindly suggest any helpful method or any relevant example?

    My Work: I tried to implement it using

    QTimer::singleShot(3000,this,SLOT(SLOT1()))

    But the limitation with it: It calls the SLOT1 after 3000 msec, but this is not what I want.

    My requirement is to use timer in order to fetch data for 3 seconds only and then stop.

    aha_1980A 1 Reply Last reply
    0
    • S SHUBHAM SINGH RAO

      Aim : I m working on Qt Creator and want to interface Modem with my main programme For that, I need fetch the data from modem for a specific time interval (say 3 seconds) only and after that I have to stop receiving data.

      Present Status 05/11/18 :
      I want to execute a blocking method through which I can wait (say 3 seconds) for data . If I receive data in 3 seconds, I will move ahead for some another task else I will quit. Is there any method except threading?
      I am trying to do it with QEventLoop but no success achieved so far.
      Kindly suggest any helpful method or any relevant example?

      My Work: I tried to implement it using

      QTimer::singleShot(3000,this,SLOT(SLOT1()))

      But the limitation with it: It calls the SLOT1 after 3000 msec, but this is not what I want.

      My requirement is to use timer in order to fetch data for 3 seconds only and then stop.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @SHUBHAM-SINGH-RAO the question is: how do you fetch the data? in a loop? then QElapsedTimer could help you.

      Or with signals? Then your SLOT() would stop the fetching.

      As long as this is unclear, its hard to help.

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        also, how is that question different from: https://forum.qt.io/topic/96190/timer-problem ?

        Qt has to stay free or it will die.

        S 1 Reply Last reply
        1
        • aha_1980A aha_1980

          also, how is that question different from: https://forum.qt.io/topic/96190/timer-problem ?

          S Offline
          S Offline
          SHUBHAM SINGH RAO
          wrote on last edited by
          #4

          @aha_1980 i m giving a detailed explanation of my problem in this post

          aha_1980A 1 Reply Last reply
          0
          • S SHUBHAM SINGH RAO

            @aha_1980 i m giving a detailed explanation of my problem in this post

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @SHUBHAM-SINGH-RAO

            1. this is no reason to create a new topic
            2. important information is still missing

            Qt has to stay free or it will die.

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

              Hi,

              To add to @aha_1980, you can edit your post and add the missing information there. Click on the icon with the vertical three points.

              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
              1
              • SGaistS SGaist

                Hi,

                To add to @aha_1980, you can edit your post and add the missing information there. Click on the icon with the vertical three points.

                S Offline
                S Offline
                SHUBHAM SINGH RAO
                wrote on last edited by
                #7

                @SGaist I have edited my present status in my Question..kindly look at it!

                1 Reply Last reply
                0
                • aha_1980A aha_1980

                  @SHUBHAM-SINGH-RAO

                  1. this is no reason to create a new topic
                  2. important information is still missing
                  S Offline
                  S Offline
                  SHUBHAM SINGH RAO
                  wrote on last edited by
                  #8

                  @aha_1980 @SGaist I have edited my present status in my Question..kindly look at it!

                  aha_1980A 1 Reply Last reply
                  0
                  • S SHUBHAM SINGH RAO

                    @aha_1980 @SGaist I have edited my present status in my Question..kindly look at it!

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @SHUBHAM-SINGH-RAO there is no way outside the blockin method. the timeout has to be implemented within that method.

                    Sorry, but if you don't show some relevant code, thats all I can tell you.

                    Regards

                    Qt has to stay free or it will die.

                    S 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @SHUBHAM-SINGH-RAO there is no way outside the blockin method. the timeout has to be implemented within that method.

                      Sorry, but if you don't show some relevant code, thats all I can tell you.

                      Regards

                      S Offline
                      S Offline
                      SHUBHAM SINGH RAO
                      wrote on last edited by SHUBHAM SINGH RAO
                      #10

                      @aha_1980 I am attaching a similar code to test concept
                      I have used network access manager but I want to take serial data instead.

                      mainwindow.cpp

                      void MainWindow::on_pushButton_clicked()
                      {
                      ui->label_2->setText("Clear replies ");
                      if(value==0)
                      {
                      ui->label->setText("clicked");
                      value=1;
                      }
                      else if(value==1)
                      {

                          ui->label->setText("unclicked");
                          value=0;
                      }
                      
                      somefunction();
                      

                      }

                      void MainWindow::somefunction()
                      {
                      QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                      QEventLoop loop;
                      QTimer timer;

                      timer.setSingleShot(true);
                      connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
                      connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
                      
                      timer.start(10000);
                      manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                      loop.exec();
                      
                      if (timer.isActive())
                      {
                           ui->label_2->setText("Reply recieved");
                      }
                      else
                      ui->label_2->setText("No reply from client");
                      

                      }

                      void MainWindow::on_pushButton_2_clicked()
                      {
                      if(value2==0)
                      {
                      ui->label_3->setText("clicked");
                      value2=1;
                      }
                      else if(value2==1)
                      {

                          ui->label_3->setText("unclicked");
                          value2=0;
                      }
                      

                      }

                      mainwindow.ui0_1541408100925_screenShot.png

                      aha_1980A 1 Reply Last reply
                      0
                      • S SHUBHAM SINGH RAO

                        @aha_1980 I am attaching a similar code to test concept
                        I have used network access manager but I want to take serial data instead.

                        mainwindow.cpp

                        void MainWindow::on_pushButton_clicked()
                        {
                        ui->label_2->setText("Clear replies ");
                        if(value==0)
                        {
                        ui->label->setText("clicked");
                        value=1;
                        }
                        else if(value==1)
                        {

                            ui->label->setText("unclicked");
                            value=0;
                        }
                        
                        somefunction();
                        

                        }

                        void MainWindow::somefunction()
                        {
                        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                        QEventLoop loop;
                        QTimer timer;

                        timer.setSingleShot(true);
                        connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
                        connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
                        
                        timer.start(10000);
                        manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                        loop.exec();
                        
                        if (timer.isActive())
                        {
                             ui->label_2->setText("Reply recieved");
                        }
                        else
                        ui->label_2->setText("No reply from client");
                        

                        }

                        void MainWindow::on_pushButton_2_clicked()
                        {
                        if(value2==0)
                        {
                        ui->label_3->setText("clicked");
                        value2=1;
                        }
                        else if(value2==1)
                        {

                            ui->label_3->setText("unclicked");
                            value2=0;
                        }
                        

                        }

                        mainwindow.ui0_1541408100925_screenShot.png

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @SHUBHAM-SINGH-RAO said in How to use QTimer in Qt Creator to get data from an external peripheral device for a specific time interval only?:

                        timer.start(10000);
                        manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                        loop.exec();

                        Don't do this! Local event loops are advanced stuff, and can easily lead to problems.

                        Use QNetworkRequest as it is designed with signals&slots. In your timeout slot, you can close() or abort() the QNetworkReply, like described here: https://stackoverflow.com/questions/14535502/how-to-stop-qnetworkaccessmanager-from-getting-a-reply-c

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1

                        • Login

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