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

📄 fdievent.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
//----------------------------------------------------------------------------------
// Module:  FdiEvent
//          Fast DI transferring and using waiting Event method. 
//         
//          Notice, 
//          1. It will be carefull for not make driver interrupted so frequency 
//             that will hange the system for querring too many IRQ.
//          2. Smaller input buffer and higher sampling rate will generate interrupt 
//             more rapidly.
//          3. If the sampling rate is very slow, then it will need to wait more 
//             meanful duration for Device to fill data to HF.
//----------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\Include\driver.h"
#define  MAX_DEVICES  100
#define ESC 0x1b

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

void main()
{
	int i;
	DWORD  dwErrCde, dwData, dwCount;
	ULONG  lDevNum;
	long   lDrvHandle;
	ULONG  dwFdiStatus, dwRetrieved;
	int    iChecking;
    short gnNumOfDevices;
    short nOutEntries; 
	DEVLIST DeviceList[MAX_DEVICES ];
	BYTE *pBuff8;
	USHORT *pBuff16;
	ULONG *pBuff32;
	
	ULONG  dwBuffer[8192*30];    // Fast DI data buffer size.
	USHORT wCyclic=0;          // Cyclic (1), Non-cyclic(0)

	
	// 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");
	// Add type of PC Laboratory Card
    dwErrCde = DRV_DeviceGetList(&DeviceList[0], MAX_DEVICES, &nOutEntries);
    // Return the number of devices which you install in the system using
    // Device Installation
    dwErrCde = 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);
    }
	
	
	// Input parameters
	printf("\nPlease input parameters:");
	printf("\nDevice Number (check the device installation utility): ");
	scanf("%d", &lDevNum);
	printf("\nPlease input cyclic mode(0 is Non-cyclic): ");
	scanf("%d", &wCyclic);
	
	
	// Open device
	dwErrCde = DRV_DeviceOpen(lDevNum, &lDrvHandle);   
	ErrorThenStop(&lDrvHandle, dwErrCde);
	
	//
	// Enable Events for getting events from Fast DI funcitons
	//
	PT_EnableEvent tEnableEvent;
	tEnableEvent.Enabled = 1;
	tEnableEvent.Count = 1;
	
	tEnableEvent.EventType = ADS_EVT_DI_OVERRUN;    // Overrun event 
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	tEnableEvent.EventType = ADS_EVT_DI_LOBUFREADY; // Low buffer ready event
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	tEnableEvent.EventType = ADS_EVT_DI_HIBUFREADY; // High buffer ready event.
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	tEnableEvent.EventType = ADS_EVT_DI_TERMINATED; // Fast DI function terminated
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	
	//
	// Start FDI function 
	// 
	// Get current DIO data width setting
	dwCount = sizeof(dwData);
	DRV_DeviceGetProperty(lDrvHandle, CFG_DioFdioDirection, &dwData, &dwCount);
	
	// 32DI(0)/32DO(1)/ 16 DIO(2) / 8DIO(3)
	switch( dwData )
	{
	case 3:
		dwCount = sizeof(dwBuffer);
		break;
		
	case 2:
		dwCount = sizeof(dwBuffer) / 2;
		break;
		
	default:
		dwCount = sizeof(dwBuffer) / 4;
		break;
	}
	dwErrCde = DRV_FDIStart(
		lDrvHandle,   //Device handle
		wCyclic,      //Cyclic(1)/non cyclic
		dwCount,      //Data counts 
		dwBuffer      //Data buffer.
		);
	ErrorThenStop(&lDrvHandle, dwErrCde);
	
	//
	// Check the Fast DI conversion status by event. 
	//
	PT_CheckEvent tCheckEvent;
	USHORT wEventType;
	
	tCheckEvent.EventType = &wEventType;
	tCheckEvent.Milliseconds = 500;
	
	printf("\n");
	iChecking = 1;
	dwCount = 0;
	while(iChecking)
	{
		
		DRV_CheckEvent(lDrvHandle, &tCheckEvent);
		switch(wEventType)
		{
		case  ADS_EVT_DI_LOBUFREADY:  // Driver finish the fill low buffer data 
			dwErrCde = DRV_FDICheck( lDrvHandle, &dwFdiStatus, &dwRetrieved);
			DRV_ClearFlag( lDrvHandle, ADS_EVT_DI_OVERRUN);
			printf("Lower half data buffer ready. Retrieved data count = %d\n", dwRetrieved);
			break;
			
		case ADS_EVT_DI_HIBUFREADY:
			dwErrCde = DRV_FDICheck( lDrvHandle, &dwFdiStatus, &dwRetrieved);
			DRV_ClearFlag( lDrvHandle, ADS_EVT_DI_OVERRUN);
			printf("Higher half data buffer ready. Retrieved data count = %d\n", dwRetrieved);
			break;
			
		case ADS_EVT_DI_TERMINATED:
			printf("Fast DI function terminated, exit checking event cycle.\n");
			iChecking = 0;
			break;
			
		case ADS_EVT_DI_OVERRUN:
			DRV_ClearFlag( lDrvHandle, ADS_EVT_DI_OVERRUN);
			printf("Fast DI function overrun occured (%d).\n", ++dwCount);
			DRV_ClearFlag( lDrvHandle, ADS_EVT_DI_OVERRUN);
			break;
			
		case 0:
			//No event generated at this CheckEvent duration.
			break;
			
		default:
			printf("Not presupposed event occur. Event ID = %xh\n", *tCheckEvent.EventType);
			break;
		}
		
		if(kbhit())
		{
			// Stop the fast DI function
			dwErrCde = DRV_FDIStop(lDrvHandle);
			ErrorHandler(dwErrCde);
			puts("Any key pressed.\n");
			
			if( getch() == ESC)
			{
				iChecking = 0;
			}
		}
	}
	
	//
	// Disable open Event
	//
	tEnableEvent.Enabled = 0;
	tEnableEvent.EventType = ADS_EVT_DI_OVERRUN;    // Overrun event 
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	tEnableEvent.EventType = ADS_EVT_DI_LOBUFREADY; // Low buffer ready event
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	tEnableEvent.EventType = ADS_EVT_DI_HIBUFREADY; // High buffer ready event.
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	tEnableEvent.EventType = ADS_EVT_DI_TERMINATED; // Fast DI function terminated
	DRV_EnableEvent(lDrvHandle, &tEnableEvent);
	
	// Find Pattern Match position
	ULONG dwPmValue;
	dwCount = sizeof(dwPmValue);
	DRV_DeviceGetProperty(lDrvHandle, CFG_DiPatternMatchValue, &dwPmValue, &dwCount);
	
	dwCount = sizeof(dwData);
	DRV_DeviceGetProperty(lDrvHandle, CFG_DioFdioDirection, &dwData, &dwCount);
	switch( dwData )
	{
	case 3:   //8 DIO
		 pBuff8=(BYTE *)dwBuffer;
		for(i=0; i<100; i++)
		{
			printf("Buffer[%d]=%2Xh\n", i, *(pBuff8+i));
		}
		break;
		
	case 2:   //16 DIO
		 pBuff16=(USHORT *)dwBuffer;
		for(i=0; i<100; i++)
		{
			printf("Buffer[%d]=%4Xh\n", i, pBuff16[i]);
		}
		break;
		
	case 1:   //32 DO
	case 0:   //32 DI
		 pBuff32=(ULONG *)dwBuffer;
		for(i=0; i<100; i++)
		{
			printf("Buffer[%d]=%8Xh\n", i, pBuff32[i]);
		}
		break;
	default:
		break;
	}
	
	//
	// Close device
	//
	dwErrCde = DRV_DeviceClose(&lDrvHandle);    
	ErrorHandler(dwErrCde);
	
	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];
	
	if( dwErrCde != SUCCESS)
	{
		DRV_GetErrorMessage(dwErrCde, szErrMsg);
		printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
	}
}//ErrorHandler

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

⌨️ 快捷键说明

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