Timing measurements on windows

This is a discussion on Timing measurements on windows within the Windows Programming forums, part of the Platform Specific Boards category; Hi I am writing an application on Windows XP which send/receive data from an embedded device on a USB port. ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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,856
    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,059
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    QueryPerformanceCounter() is the best solution here IMO. timeGetTime() is not accurate.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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.

Page 1 of 2 12 LastLast
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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21