locked
Using Timer at regular intervals in Windows XP in Visual C++ RRS feed

  • Question

  • I am using Visual C++ in Windows XP. I want to transmit a serial data via (RS 232 or 422) at regular intervals. I had tried Sleep() Command as well as SetTimer () function. I want to transmit the data at regular intervals. But when i am transmitting the data, it is not at regular intervals. Sometimes it is much higher than the specified. Is there any other method to invoke a timer at regular intervals. Can anyone suggest some other method to obtain transmission at regular intervals.
    Thursday, December 3, 2009 3:32 AM

Answers

  • The multimedia timer will probably give you better results.  See timeSetEvent.
    • Marked as answer by Nancy Shao Thursday, December 10, 2009 7:53 AM
    Thursday, December 3, 2009 3:44 AM
  • Hi Rajeev C,

     

    There are lots of ways to set time at regular intervals as following shows:

     

    ·         SetTimer()

    ·         Sleep(): the unit is ms, but the precision is low

    ·         COleDataTime class and COleDateTimeSpan class

    ·         GetTickCount()

    ·         DWORD timeGetTime(void)

    ·         timeSetEvent()

    ·         QueryPerformanceFrequency() and QueryPerformanceCouter() function

     

    The precision is more and more high according to down order. I would suggest you use the last three methods, they are all high precision.

     

    Best Regards,

    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Thursday, December 10, 2009 7:53 AM
    Friday, December 4, 2009 6:19 AM

All replies

  • The multimedia timer will probably give you better results.  See timeSetEvent.
    • Marked as answer by Nancy Shao Thursday, December 10, 2009 7:53 AM
    Thursday, December 3, 2009 3:44 AM
  • Hi Rajeev C,

     

    There are lots of ways to set time at regular intervals as following shows:

     

    ·         SetTimer()

    ·         Sleep(): the unit is ms, but the precision is low

    ·         COleDataTime class and COleDateTimeSpan class

    ·         GetTickCount()

    ·         DWORD timeGetTime(void)

    ·         timeSetEvent()

    ·         QueryPerformanceFrequency() and QueryPerformanceCouter() function

     

    The precision is more and more high according to down order. I would suggest you use the last three methods, they are all high precision.

     

    Best Regards,

    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Thursday, December 10, 2009 7:53 AM
    Friday, December 4, 2009 6:19 AM
  • Waitable timers also should be considered.

    CreateWaitableTimer/WaitForSingleObject...

    http://msdn.microsoft.com/en-us/library/ms682492%28VS.85%29.aspx

    Good Luck!
    Friday, December 4, 2009 12:44 PM
  • One other thing to consider, is: You are not programming in a real-time environment.  While some of the later methods Nancy suggests will get you good average regularity, there can and will be a lot of jitter, especially for small units of time.

    I find even with timeSetEvent, if I am shooting for 20ms intervals, over a 2 second period I my event will correctly fire 100 times, but there will be a lot of jitter in the timings e.g.

    25 ms
    24 ms
    11 ms
    20 ms
    100 ms
    0 ms
    0 ms
    0 ms
    0 ms
    22 ms
    18 ms
    etc.

    Friday, December 4, 2009 8:13 PM