Thread: Timing measurements on windows

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    Question Timing measurements on windows

    Hi
    I am writing an application on Windows XP which send/receive data from an embedded device on a USB port.

    When I receive data from the embedded device, it must arrive in set time limits. I realize that windows is not an RTOS, but still there must be some mechanism to measure timings e.g. after after I have sent a packet to my embedded device, some mechanism to count how many milliseconds have passed since I sent the packet.

    Could anybody please give me directions on how can I achieve this? Basically I am interested in knowing what constructs/facilities Windows provides to measure "timer" type features.

    Thanks
    Samie

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several functions of different levels of portability:
    1. clock() - generic function that returns fairly precise timings.
    2. QueryPerformanceCounter/QueryPerformanceCounterFrequency
    3. RDTSC processor instruction.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is also timeGetTime() which returns a very exact millisecond clock (I used it to create a loop that looped for exactlly 1s).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    What sort of timer resolution were you looking for? (that determines what timer to use)

    The thing to remember about MS OS's is the OS message queue can ignore timer msgs (and paint msgs are concatinated).

    For this reason timers are not overly accurate.

    Quote Originally Posted by Elysia View Post
    There is also timeGetTime() which returns a very exact millisecond clock (I used it to create a loop that looped for exactlly 1s).
    Minimum resolution is >5ms.

    I would use QueryPerformanceCounter/QueryPerformanceCounterFrequency (if available).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I am writing an application on Windows XP which send/receive data from an embedded device on a USB port
    You may want to check out USB Isochronous Transfer to send/receive data from your device on a USB port. If this is what you need, then download Windows DDK and check out the ISOUSB sample.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    QueryPerformanceCounter() is the best solution here IMO. timeGetTime() is not accurate.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can also set the resolution for timeGetTime; default resolution on Windows 95 is one ms!
    Anyway, try which solution works best.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    QueryPerformanceCounter is much more precise than 1ms, it's at the very least capable of milliseconds, and on modern machines it often uses better timers, such as PM timer, APIC timer or some other high-res timer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > after after I have sent a packet to my embedded device, some mechanism
    > to count how many milliseconds have passed since I sent the packet.
    Are you attempting some kind of 'time-based' flow control to regulate how much data you send to the device?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I actually read (perhaps mistakenly) the original post to want to do some sort of performance measurement. But other options are of course possible - like so many posts here, the "purpose of the question" is not explained, and we have to guess what is the right solution to the question.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    Thanks to all of you for your replies and suggestions.

    >Are you attempting some kind of 'time-based' flow control to regulate how much data you send >to the device?


    No. It is not time-based flow control that I am trying to attempt .

    What I am trying to do is that my application thread on Windows expects 32 KB of data from the embedded device on USB link, every 250 milliseconds. If I dont get this much data in 250 milliseconds, I know there is something wrong and I take measures for it.

    In an embedded environment, I could create a timer and let it generate an interrupt after 250 ms and in the ISR for this check if I have received the expected number of bytes. If not, take action. If yes, things are going ok.

    However, to do this on Windows, I wasn't too sure what to do for this and hence the reason for the question. It looks like I may have to try TimerProc() type functionality.

    Thanks
    Samie

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How do you do the "receive from USB" function? Are you using a file-read, or some other interface?

    I'm thinking that you want to do a WaitForSingleObject() with a timeout of 250ms.


    At the same time, I guess you would need to read the data no matter what - just that if it arrives late (or there isn't enough data there) you may want to discard the actual data. So perhaps you need to figure out what happens when/if data is late.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could also use Sleep to sleep for 250ms. This will not take any cpu power, and it's also easier than WaitForSingleObject so it might be a better idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You could also use Sleep to sleep for 250ms. This will not take any cpu power, and it's also easier than WaitForSingleObject so it might be a better idea.
    But that means that the application CAN'T process any data that comes in during this time. And it's only "easier" because you have to type less. A Sleep() is actually implemented as a WaitForSingleObject() [or some varation of that], where it's waiting for a Timer Event.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's why we usually have threads.
    If it's a Window app, you need a thread anyway since the main thread needs to process messages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone using Windows 7?
    By Sharke in forum General Discussions
    Replies: 60
    Last Post: 07-12-2009, 08:05 AM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM