CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Microsecond Timer C++ Linux

    Hi,

    I'm using Ubuntu SDK - QT C++ dialog based application.
    I like to do a "DataCapture()" process for every 100 microsecond. Execution time for "DataCapture()" function is maximum 50 microseconds. so i was called the function at every 100 microseconds. Note : only processing this method, no other process in my project.

    C++ QTimer concept not support, code as follows, It takes different time sequence like 89, 70, 90, 100, 210 microseconds.
    Code:
    QTimer Camtimer = new QTimer(this);
    connect(Camtimer, SIGNAL(timeout()), this, SLOT(DataCapture()));
    Camtimer->start();
    so, go with this conccept - Timer function code for Linux and Windows
    I like to run micro second process, so code edited as follows,
    Code:
    int start_timer(int mSec, void (*timer_func_handler)(void))
    {
         //edited for start timer at 0.1 milli sec. that is 100 micro sec
         mSec = 0;
         timervalue.it_value.tv_sec = mSec;
         timervalue.it_value.tv_usec = 100;
         timervalue.it_interval.tv_sec = mSec;
         timervalue.it_interval.tv_usec = 100;
    }
    called like
    Code:
    int processcnt = 0;
    void timer_handler(void)
    {   
       DataCapture();
       processcnt++;
    }
    if(start_timer(0, &timer_handler)) //microsec timer call
    {
         qDebug() << "timer error";
    }
    while(1)
    {
            if(processcnt > 10000)
            {
                stop_timer();
                processcnt = 0;
                break;
           }
    }
    I have a doubt, am i chosen a correct path?
    i like to quit the timer when processcnt reach 10000. But this method not quit properly at all time.
    That means, Application running continuously even processcnt >10000 .

    Any suggestion.
    Regards,

    SaraswathiSrinath

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Microsecond Timer C++ Linux

    Application running continuously even processcnt >10000
    So a break-point on stop_timer() doesn't trigger if processcnt > 10000? How do you know that within the while loop processcnt is > 10000?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: Microsecond Timer C++ Linux

    Quote Originally Posted by 2kaud View Post
    So a break-point on stop_timer() doesn't trigger if processcnt > 10000? How do you know that within the while loop processcnt is > 10000?
    i was just print the processcnt like below,
    Code:
    void timer_handler(void)
    {   
       DataCapture();
       processcnt++;
       qDebug() << "processcnt : " << processcnt; 
    }
    I confirm one more detail, processcnt reached the set limit count ( that is, > 10000 ),
    that time while loop was not executed. so the timer also not quit properly.

    Code:
    void Dialog::on_Process1_clicked() //Acquisition
    {
        if(start_timer(0, &timer_handler)) //sec, microsec, func
        {
            qDebug() << "timer error";
        }
    
        while(1)
        {
            if(processcnt > 10000)
            {
                stop_timer();
                processcnt = 0;
                break;
            }
        }
    }
    Regards,

    SaraswathiSrinath

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Microsecond Timer C++ Linux

    i was just print the processcnt like below,
    That is within timer_handler(), not within the while loop. What are the values of processcnt within the while loop?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: Microsecond Timer C++ Linux

    That is within timer_handler(), not within the while loop.
    Yes.

    What are the values of processcnt within the while loop?
    didn't print. so, i decided to print the value within timer_handler().

    One more doubt, I try to stop the timer while running. But i can't to stop. is possible?
    Code:
    void stop_timer(void)
    {
      timervalue.it_interval.tv_sec = 0;
      timervalue.it_interval.tv_usec = 0;
      timervalue.it_value.tv_sec = 0;
      timervalue.it_value.tv_usec = 0;
      setitimer(ITIMER_REAL, &timervalue, NULL);
      sigaction(SIGALRM, &old_handler, NULL);
    }
    Some time, While data capture, automatically quit the dialog and the message shown like "The program has unexpectedly finished.". what is the reason?
    Regards,

    SaraswathiSrinath

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured