Important: Please read the Qt Code of Conduct -

Query Related to Waiting Timer Class

  • Task: To implement modem with the help of Qt

    Aim: To implement a timer class which can help in receiving data from modem for a specific period of time only (say 10 seconds)[while keeping GUI in active state ( no hanging state)] .
    If data is received in between the time period then move to next function else give timeout error !

    Present Status :
    the pseudo code is as follows :

    Command 1 of Modem sent;
    Start Main Timer of 10 seconds ; //Main Timer

    startTimer2(1); //calltimerEvent after each 1 msec

    if(modem response received in TimerEvent)
    {call slot2 immdiately;}
    { call slot1;
    //Call slot1 recurrsively, until10 seconds main timer not over

    if(10 seconds main timer is over)
    {quit the loop and give time out message; }


    *Mainwindow:: timerEvent(QTimerEvent e)
    {Take modem response continuously;

    Slot2 ()
    Send 2nd command of modem;

    Query : Any timer type which can replace Main Timer?

  • @SHUBHAM-SINGH-RAO said in Query Related to Waiting Timer Class:

    Not sure, the aim says that you want to receive the data in specific time period, while the first command of slot1() sends something to modem. Why do you want to call slot1() recursively? I am not aware about how UART works, can simplify it for me.

  • @Maaz-Momin yeah i explain it!
    Steps how modem will work :
    Step-1 : Qt will send first send command to Modem
    Step-2 : Modem will give response as a reply to that command
    Step-3 : Now I have to wait for a specific time period (say 10 seconds) maximum to receive the response. If I get response within 10 seconds I will move to next command, else I will quit this slot.

    I want to call slot1() recursively , because I have to wait for a maximum time of 10 seconds in order to receive response else I will quit.

    UART has nothing to do with this. I have removed it infact!

  • The response of modem will be received in some other function i believe as slot (assume "messageReceiveSlot").

    In that case,

    1. Create a global timer.
    2. When slot1() is called
      a) send command to modem.
      b) Set timer interval to 10sec and singleShot property to true.
    3. On message received before timeout stop your timer and call slot2() OR 4) On timeOut give time out message and disconnect your messageReceiveSlot

    If my assumption is incorrect then let me know.

  • @Maaz-Momin Thanks.
    I have implemented a different logic. I have extracted the real time values of second and stored that in some variable(let k) And then I have called the same function recursively till real time second value is not greater than (k+10).
    Once real time seconds value exceeds (K+10), I will declare timeout.

  • How do you extract real time values of second?

  • @Maaz-Momin quite simple.

    QTime time =QTime::currentTime();
    QString second_time= time.toString("ss"); // store seconds value in string
    secondvalue=second_time.toInt(); // converted string into int

  • Is this code written in slot1? Because at the end it is working somewhat similar to QTimer which is already provided to you by QT.

  • @Maaz-Momin yeah i have written it in the slot.And it is the use of QTimer Class only.

  • Can you paste your code here. I want to have a look. In my opinion you are making this even more complex.

  • @Maaz-Momin ok
    here it is :

     int secondvalue=0;
     int secondsCompare=0;
     int onlyOnce=0;
      void MainWindow::on_pushButton_clicked()
            QTime time =QTime::currentTime();
            QString second_time= time.toString("ss");
    {  secondsCompare=secondvalue;  // second value stored in secondsCompare
       if(secondvalue == (secondsCompare+10))
         slot2(); //call timeout slot
            sendModem Command1;
       int Response_returnValue=0;
       Response_returnValue= return from modem analysis function 
       if(Response_returnValue==1) //for ok response
               slot3();  //go for next commad
       else  // response not received
         on_pushButton_clicked();  // call recursively
     void MainWindow::slot2) 
    send command2;
    void MainWindow::slot3)  
    show timeout message in label;

  • Code can be improved for performance. But if you are fine then it's good. Because this for sure, will call MainWindow::on_pushButton_clicked() a lot of time.

    Edit: Also if it works can you mark it as SOLVED.

Log in to reply