Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    India
    3
    11
    2263
    Loading More Posts
    • 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
      SHUBHAM SINGH RAO last edited by 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_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @SHUBHAM SINGH RAO last edited by

        @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 Reply Quote 1
        • aha_1980
          aha_1980 Lifetime Qt Champion last edited by aha_1980

          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 Reply Quote 1
          • S
            SHUBHAM SINGH RAO @aha_1980 last edited by

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

            aha_1980 1 Reply Last reply Reply Quote 0
            • aha_1980
              aha_1980 Lifetime Qt Champion @SHUBHAM SINGH RAO last edited by

              @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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 1
                • S
                  SHUBHAM SINGH RAO @SGaist last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • S
                    SHUBHAM SINGH RAO @aha_1980 last edited by

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

                    aha_1980 1 Reply Last reply Reply Quote 0
                    • aha_1980
                      aha_1980 Lifetime Qt Champion @SHUBHAM SINGH RAO last edited by

                      @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 Reply Quote 0
                      • S
                        SHUBHAM SINGH RAO @aha_1980 last edited by 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_1980 1 Reply Last reply Reply Quote 0
                        • aha_1980
                          aha_1980 Lifetime Qt Champion @SHUBHAM SINGH RAO last edited by

                          @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 Reply Quote 1
                          • First post
                            Last post