windows – 需要产生稳定的10mSec中断
发布时间:2020-12-14 05:36:19 所属栏目:Windows 来源:网络整理
导读:我有一个应用程序,我需要在 Windows 7/32位计算机(也将同时运行其他应用程序)上以10毫秒的速率(100赫兹)运行.此中断可能会有一些最迟(100uSec)响应,但不能长时间漂移.我有一个程序,我已加载并使用NtSetTimerResolution将计时器设置为10毫秒分辨率,然后使用Cr
我有一个应用程序,我需要在
Windows 7/32位计算机(也将同时运行其他应用程序)上以10毫秒的速率(100赫兹)运行.此中断可能会有一些最迟(100uSec)响应,但不能长时间漂移.我有一个程序,我已加载并使用NtSetTimerResolution将计时器设置为10毫秒分辨率,然后使用CreateTimerQueue / CreateTimereQueueTimer函数创建一个计时器,其中一个回调例程切换GPIO引脚(暂时) – 这会产生预期的方波,只要我不对系统做任何其他事情.当我开始其他几个过程时,方波的精确度就会消失.有没有办法在定时器中断上获得更高的优先级(或者是否有我可以使用的另一个定时器),这将产生更稳定的输出(可能是SMI)?我的代码如下所示,使用Windows DDK的x86检查构建环境构建,并从具有管理员权限的命令shell运行:
/* Abstract: Simple console test app for a 10mSec timer interrupt service Enviroment: Administrator Mode */ /* INCLUDES */ #include <windows.h> #include <winioctl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #include <strsafe.h> #include <stdlib.h> #include <stdio.h> #include <winsock2.h> #include <mswsock.h> #pragma warning(disable:4127) // condition expression is constant FARPROC pNtQueryTimerResolution; FARPROC pNtSetTimerResolution; static HANDLE NTDLLModuleHandle; static HINSTANCE hInpOutDll; typedef void ( __stdcall *lpOut32 )( short,short ); typedef short ( __stdcall *lpInp32 )( short ); typedef BOOL ( __stdcall *lpIsInpOutDriverOpen )( void ); //Some global function pointers (messy but fine for an example) lpOut32 gfpOut32; lpInp32 gfpInp32; lpIsInpOutDriverOpen gfpIsInpOutDriverOpen; void CALLBACK TimerProc(void* lpParameter,BOOLEAN TimerOrWaitFired); // MAIN VOID __cdecl main( void ) { ULONG ulMinRes = 0; ULONG ulMaxRes = 0; ULONG ulCurRes = 0; HANDLE phNewQueue; HANDLE phNewTimer; phNewQueue = CreateTimerQueue( ); NTDLLModuleHandle = LoadLibrary( "NTDLL.DLL" ); if( NULL == NTDLLModuleHandle ) { return; } // Get the function pointers,pNtQueryTimerResolution = GetProcAddress( NTDLLModuleHandle,"NtQueryTimerResolution" ); pNtSetTimerResolution = GetProcAddress( NTDLLModuleHandle,"NtSetTimerResolution" ); if( ( pNtQueryTimerResolution == NULL ) || ( pNtSetTimerResolution == NULL ) ) { printf( "unable to link to ddlnnnnnn" ); return; } pNtQueryTimerResolution( &ulMinRes,&ulMaxRes,&ulCurRes ); printf( "MMR: %d %d %dn",ulMinRes,ulMaxRes,ulCurRes ); ulMaxRes = 100000; pNtSetTimerResolution( ulMaxRes,TRUE,&ulCurRes ); pNtQueryTimerResolution( &ulMinRes,ulCurRes ); //Dynamically load the DLL at runtime (not linked at compile time) hInpOutDll = LoadLibrary( "InpOut32.DLL" ); if( hInpOutDll != NULL ) { gfpOut32 = ( lpOut32 )GetProcAddress( hInpOutDll,"Out32" ); gfpInp32 = ( lpInp32 )GetProcAddress( hInpOutDll,"Inp32" ); gfpIsInpOutDriverOpen = ( lpIsInpOutDriverOpen )GetProcAddress( hInpOutDll,"IsInpOutDriverOpen" ); if( gfpIsInpOutDriverOpen( ) ) { gfpOut32( 0xA01,0x00 ); } else { printf( "unable to create timer systemnnnnnn" ); return; } } CreateTimerQueueTimer( &phNewTimer,phNewQueue,TimerProc,NULL,10,WT_EXECUTEINTIMERTHREAD ); do { Sleep( 1 ); } while( TRUE ); } void CALLBACK TimerProc(void* lpParameter,BOOLEAN TimerOrWaitFired) { WORD wData; UNREFERENCED_PARAMETER ( lpParameter ); UNREFERENCED_PARAMETER ( TimerOrWaitFired ); wData = gfpInp32( 0xA00 ); wData++; gfpOut32( 0xA00,wData ); } 解决方法
您可以使用
SetThreadPriority 为关键线程赋予优先权.在这种情况下,您可能需要显式创建一个线程并使用
CreateWaitableTimerEx ,SetWaitableTimerEx 和
WaitForSingleObjectEx 而不是
CreateTimerQueueTimer .确保关键线程永远不会在等待之间执行太长时间,否则Windows可能会停止正常工作.
如果最大延迟为100微秒,这可能还不够.您可能需要使用 即使这可能还不够. Windows不是一个实时操作系统,它可能无法让它做你想要的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在Windows上获取视频捕获设备(网络摄像头)列表? (C )
- windows-8.1 – 我可以在Windows 8.1中下载和使用Microsoft
- windows – 用于并行工作的CMD实例的信号量
- Windows Server 2016-管理站点复制(二)
- Windows – Qt4自定义窗口框架,如Office 2007?
- Microsoft SQL Server 2008 – 日期
- windows – 在FOR循环中使用什么分隔符来读取行?
- windows-vista – 每个应用程序禁用Vista UAC,或者在没有提
- 有什么像/ proc for windows
- buildbot在Windows上作为服务
推荐文章
站长推荐
- 从Windows上的Emacs中的“grep-find”运行时,ack
- windows-server-2008 – Windows备份到网络共享(
- 如何在Windows下创建一个md文件
- Windows的cmd命令提示符窗口命令大全
- windows-8 – 如何通过CLI或AHK在Windows 8中打开
- 新建Microsoft Word文档
- Windows防火墙的设置允许Docker for Windows共享
- wcf – Azure AppFabric和Windows Server AppFab
- 无法更改Windows Server 2012 R2系统时间
- Powershell获取Exchange 邮箱用户配额
热点阅读