简体   繁体   English

Windows串行端口计时?

[英]Windows serial port timing?

I need to measure the time between character events on a serial port, preferably under Windows 7. 我需要测量串行端口上字符事件之间的时间,最好是在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. 我曾在不同的地方阅读,语句,如“Windows将不会提供超过10ms更高的分辨率”,但我一直没能找出什么是真正的意思。
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) (我在想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. 如果是后者,那么我想我应该可以使用QueryPerformanceCounter之类的东西来进行足够的测量,但是如果事件的传递精度不高,那显然没有帮助。

I think the 10 ms time is due to the task switching. 我认为10毫秒的时间归因于任务切换。 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. 最重要的是,我猜测从端口上接收字符到接收DataReceived事件(或所需的任何软件通知)之间的时间是高度可变的。 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. UART芯片可能不会中断CPU,直到感觉到为止(通常在其FIFO已满或某些超时到期时),然后内核可能决定花一些时间才能将中断信息发送到您的应用程序。 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 !). 这在System.Diagnostics中,并且它的Elapsed字段(Timespan)的分辨率为100纳秒(我希望这足够准确!)。 I use C++ Builder and often put in code something like this : 我使用C ++ Builder并经常在代码中输入以下内容:

#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 : 有关更多信息,这是有关此的MSDN页面的链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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