简体   繁体   中英

Windows serial port timing?

I need to measure the time between character events on a serial port, preferably under Windows 7.
I have read in various places, statements like " Windows will not provide greater resolution than 10ms" , but I have not been able to find out what that really means.
Is the problem that the OS will not deliver the events with greater accuracy, or is it that the timing functions (for measuring) used to be that "poor"? (I'm thinking GetTickCount)

If the latter, then I guess I should be able to use something like QueryPerformanceCounter for good-enough measuring, but if the events aren't delivered with similar accuracy, that obviously won't help.

I think the 10 ms time is due to the task switching. Even if your timing application is the only user task running, there are still a bunch of system tasks that might get switched in.

On top of that, I'm guessing that the time between receiving a character on the port and receiving a DataReceived event (or whatever software notification you want) is highly variable. The UART chip may not interrupt the CPU until it feels like it (usually when its FIFO is full or some timeout expires), then the kernel may decide to take some time before sending that interrupt information on to your application. So even though you can use a high resolution timer in your software, you would probably be at the mercy of hardware/kernel level variability.

What you can use to get a much higher timing resolution is a Stopwatch. This is in System.Diagnostics, and its Elapsed field (a Timespan) has a resolution of 100 nanoseconds (I hope that's accurate enough !). I use C++ Builder and often put in code something like this :

#include <Diagnostics.hpp>

TStopwatch SW;
TTimeSpan TS;
SW = TStopwatch::StartNew();
// Do something we need timed here ...
SW.Stop();
TS = SW.Elapsed;
Caption="That took "+String(TS.Minutes)+" Minutes, "+String(TS.Seconds)+" Seconds."+String(TS.Milliseconds)+" mS.";

For more info, here's a link to an MSDN page about this :

https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(VS.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM