⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 event.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\..\Include\driver.h"

#define ESC 0x1b

//******************************
//* Local function declaration 
//******************************
void ErrorHandler(DWORD dwErrCde);
void ErrorStop(long*, DWORD);

void main()
{
  DWORD  dwErrCde;
  ULONG  lDevNum;
  long   lDrvHandle;
  DWORD  dwStartTick, dwCurrentTick;

  // Event counting counter
  ULONG  dwIntCountDi0=0, dwIntCountDi1=0, dwIntCountDi2=0, dwIntCountDi3=0;
  ULONG  dwIntCountDi4=0, dwIntCountDi5=0, dwIntCountDi6=0, dwIntCountDi7=0;
  ULONG  dwIntCountCd=0,  dwIntCountPm=0,  dwIntCountTc=0;
  ULONG  dwIntCountDiLoBufReady=0, dwIntCountDiHiBufReady=0, dwIntCountTmd=0;
  ULONG  dwIntCountDiOver=0;
  ULONG  dwIntCountDoLoBufTrans =0;
  ULONG  dwIntCountDoHiBufTrans=0, dwIntCountDoTmd=0, dwIntCountDoUnder=0; 
  SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);

  //Step 1: Display hardware and software settings for running this example
  printf("Before running this example, please\n");
  printf("use the device installation utility to add the device.\n");

  //Step 2: Input parameters
  printf("\nPlease input parameters:");
  printf("\nDevice Number (check the device installation utility): ");
  scanf("%d", &lDevNum);

  //Step 3: Open device
  dwErrCde = DRV_DeviceOpen(lDevNum, &lDrvHandle);   
  if (dwErrCde != SUCCESS)
  {
    ErrorHandler(dwErrCde);
    printf("Program terminated!\n");

    printf("Press any key to exit....");
    getch();
    exit(1);
  }

  //
  // Event functions
  //
  PT_EnableEvent tEnableEvent;

  // Enable Timer interrupt
  tEnableEvent.EventType = ADS_EVT_TC_TIMER;
  tEnableEvent.Enabled = 1;
  tEnableEvent.Count = 1000;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  
  tEnableEvent.Count = 1;
  tEnableEvent.Enabled = 1;
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI0;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI1;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI2;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI3;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI4;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI5;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI6;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);
  tEnableEvent.EventType = ADS_EVT_INTERRUPT_DI7;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);

  dwCurrentTick = dwStartTick = GetTickCount();

  // Check Event
  PT_CheckEvent tCheckEvent;
  USHORT wEventType;
  int iKeyPress=0;
  int iCount = 0;
	
  tCheckEvent.EventType = &wEventType;
  tCheckEvent.Milliseconds = 5000;

  while( !kbhit() ) 
  {
    dwErrCde = DRV_CheckEvent(lDrvHandle, &tCheckEvent);
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");
		DRV_DeviceClose(&lDrvHandle);
		printf("Press any key to exit....");
		getch();
	    exit(1);		
	}
    switch(*tCheckEvent.EventType)
    {
    case  ADS_EVT_INTERRUPT_DI0: //Auxiliary DI channel 0 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH0  %d", ++dwIntCountDi0);
      break;

    case ADS_EVT_INTERRUPT_DI1:   //Auxiliary DI channel 1 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH1  %d", ++dwIntCountDi1);
      break;

    case ADS_EVT_INTERRUPT_DI2:   //Auxiliary DI channel 2 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH2  %d", ++dwIntCountDi2);
      break;

    case ADS_EVT_INTERRUPT_DI3:   //Auxiliary DI channel 3 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH3  %d", ++dwIntCountDi3);
      break;

    case ADS_EVT_INTERRUPT_DI4:   //Auxiliary DI channel 4 interrupt 
      printf("Event ADS_EVT_DI_INTERRUPT_CH4  %d", ++dwIntCountDi4);
      break;

    case ADS_EVT_INTERRUPT_DI5:   //Auxiliary DI channel 5 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH5  %d", ++dwIntCountDi5);
      break;

    case ADS_EVT_INTERRUPT_DI6:   //Auxiliary DI channel 6 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH6  %d", ++dwIntCountDi6);
      break;

    case ADS_EVT_INTERRUPT_DI7:   //Auxiliary DI channel 7 interrupt
      printf("Event ADS_EVT_DI_INTERRUPT_CH7  %d", ++dwIntCountDi7);
      break;
	case ADS_EVT_DEVREMOVED:
	  printf("The Device is removed, and the program will be terminated\n");
	  DRV_DeviceClose(&lDrvHandle);
	  printf("Press any key to exit....");
	  getch();
	  exit(1);		  
	  break;
	  
    }

    if(*tCheckEvent.EventType)
      printf(" (%d sec)\n", (GetTickCount() - dwStartTick)/1000);


  }
  getch();

  //Close Event
  tEnableEvent.Enabled = 0;
  DRV_EnableEvent(lDrvHandle, &tEnableEvent);

  // Step 6: Close device
  dwErrCde = DRV_DeviceClose(&lDrvHandle);

  printf("\nTest porgram finish, Press any key to exit....");
  getch();
  puts("");

}//main


//**********************************************************************
//* Function: ErrorHandler
//*           Show the error message for the corresponding error code
//* 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);
}//ErrorHandler

//**********************************************************************
//* Function:   ErrorStop
//*             Release all resource and terminate program if error occurs 
//* Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
//*             dwErrCde, IN, Error code.
//* return:     none             
//**********************************************************************/
void ErrorStop(long *pDrvHandle, DWORD dwErrCde)
{
    //Error message 
    ErrorHandler(dwErrCde);
    printf("Program terminated!\n");
    
    //Close device
    DRV_DeviceClose(pDrvHandle);
    exit(1);
}//ErrorStop

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -