?? tmrcntset.cpp
字號:
/*
****************************************************************************************
* Program : TmrCntSet.CPP *
* Description : Advantech Driver DEMO: Digital Input with Timer Setting *
* Revision : 1.00 *
* Date : 10/15/2003 Advantech Co., Ltd. *
****************************************************************************************
*/
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\include\driver.h"
//Static variable defined
static PT_DeviceGetFeatures ptDevFeatures; // Devfeatures table
static DEVFEATURES DevFeatures; // structure for device features
static PT_EnableEvent ptEnableEvent; // Enable event
static PT_CheckEvent ptCheckEvent; // Check event
static PT_TimerCountSetting CounterSetting;
static BOOL bThreadloop = FALSE;
static BOOL bThreadFlag = FALSE; //the flag of the thread
// Error variable defined
char szErrMsg[80];
DWORD dwErrCde;
LONG ErrCde;
long lDriverHandle; //DriverHandle defined
DWORD dwDeviceNum;
ULONG ulCounter0,ulCounter1,ulCounter2; //Counter Number
#define MAX_DEVICES 100
/******************
* Local function declare
******************/
void ErrorHandler(DWORD dwErrCde);
//Thread function declare:
void UserThread(LPVOID );
void main()
{
ULONG lDevNum = 0;
USHORT usChan = 0;
int i;
short gnNumOfDevices;
short nOutEntries;
char Exit='0';
DEVLIST DeviceList[MAX_DEVICES ];
// long lDmaBuf; //DMA buffer defined
// PT_FAIDmaStart ptFAIDmaStart;
HANDLE hThreadHandle;
DWORD dwThreadID;
//Step 1: Display hardware and software settings for running this example
printf("\n\t\t\tTMR_CNT_SET CONSOLE MODE SAMPLE v2.0\n\n");
printf("Before running this example,\n");
printf("please use the device installation utility to add the device on Device Number.\n\n");
// Add type of PC Laboratory Card
ErrCde = DRV_DeviceGetList(&DeviceList[0], MAX_DEVICES, &nOutEntries);
// Return the number of devices which you install in the system using
// Device Installation
ErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
printf("This is the installed device list:\n");
for (i = 0; i < gnNumOfDevices; i++)
{
printf(" %3.3d %s\n",DeviceList[i].dwDeviceNum,DeviceList[i].szDeviceName);
}
//Step 2: Input parameters
fflush(stdin);
printf("\nPlease input parameters-----------------");
printf("\nDevice Number: ");
scanf("%d", &dwDeviceNum);
fflush(stdin);
printf("\nTimer Number Setting---------------\n");
ulCounter0=0;
while(ulCounter0 <10)
{
printf("Counter0 Number >=10 : ");
scanf("%d", &ulCounter0);
fflush(stdin);
}
ulCounter1=0;
while(ulCounter1<10)
{
printf("Counter1 Number >=10 : ");
scanf("%d", &ulCounter1);
fflush(stdin);
}
ulCounter2=0;
while(ulCounter2<2)
{
printf("Counter2 Number >=2 : ");
scanf("%d", &ulCounter2);
fflush(stdin);
}
//Step 3: Open Device
ErrCde = DRV_DeviceOpen(dwDeviceNum, (LONG far *)&lDriverHandle);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
// Step 4: Get device features
ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
ptDevFeatures.size = sizeof(DEVFEATURES);
if ((ErrCde = DRV_DeviceGetFeatures(lDriverHandle,
(LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
ULONG CounterVal=0x00;
// Test if Supportted Device!!!
CounterSetting.counter = 2;
CounterSetting.Count = 0x0002;
dwErrCde = DRV_TimerCountSetting(lDriverHandle, &CounterSetting);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
// Real Start
// Counter Value Set
CounterSetting.counter = 2;
CounterSetting.Count = ulCounter2;
dwErrCde = DRV_TimerCountSetting(lDriverHandle, &CounterSetting);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
CounterVal = (ulCounter0 << 16) + ulCounter1;
CounterSetting.counter = 1;
CounterSetting.Count = CounterVal;
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
CounterSetting.counter = 0;
dwErrCde = DRV_TimerCountSetting(lDriverHandle, &CounterSetting);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
// Enable Event
ptEnableEvent.EventType = ADS_EVT_INTERRUPT | ADS_EVT_PORT0 | ADS_EVT_PORT1;
ptEnableEvent.Enabled = 1;
ptEnableEvent.Count = 1;
if ((ErrCde = DRV_EnableEvent(lDriverHandle,
(LPT_EnableEvent)&ptEnableEvent)) != 0)
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
//Step8: Start the Thread
hThreadHandle = CreateThread(0, 0,
(LPTHREAD_START_ROUTINE) UserThread,
NULL, 0, (LPDWORD)&dwThreadID);
bThreadFlag = TRUE;
bThreadloop = TRUE;
Sleep(0);
bThreadFlag = true;
printf("Press any key to stop....\n");
getch();
bThreadloop = false;
Sleep(100);
// Step 10: Close device
dwErrCde = DRV_DeviceClose(&lDriverHandle);
printf("\nPress any key to exit....");
getch();
CloseHandle(hThreadHandle);
}
/**********************************************************************
* Function: ErrorHandler
* Show the error message of corresponding errcode
* input: dwErrCde, IN, Error code
* return: none
**********************************************************************/
void ErrorHandler(DWORD dwErrCde)
{
char szErrMsg[180];
DRV_GetErrorMessage(dwErrCde, szErrMsg);
printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
}
/*********************************************************************
* Function: UserThread
* Parameters: pISamples, IN, the count of DMA convertion number
* Return value: none
* Purpose: Check the event and handle it
********************************************************************/
void UserThread(LPVOID pISamples)
{
USHORT usEventType;
DWORD dwStartTime;
DWORD dwCurrentTime;
DWORD dwTime;
DWORD dwEventCount = 0;
float ratio;
ptCheckEvent.EventType = &usEventType;
ptCheckEvent.Milliseconds = 1000;
dwStartTime = GetTickCount();
while ( bThreadloop )
{
if (DRV_CheckEvent( lDriverHandle, &ptCheckEvent ) == SUCCESS)
{
dwEventCount++;
}
dwCurrentTime = GetTickCount();
if ((dwTime = dwCurrentTime - dwStartTime) > 1000)
{
ratio = (float)(((float)dwEventCount / (float)dwTime) * 1000.0);
printf( "Interrupt Event count per Second: %.3f\n", ratio );
dwStartTime = GetTickCount();
dwEventCount = 0;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -