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

📄 diint.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : DIINT.CPP (for PCI-1758 only)                                                          *
 * Description    : Demo program for digital input function with interrupt event        *
 * Boards Supp.   :                                                                     *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage,                *
 *                  DRV_EnableEvent, DRV_CheckEvent                                     *
 * Revision       : 1.00                                                                *
 * Date           : 6/2/2004                   Advantech Co., Ltd.                      *
 ****************************************************************************************
 */
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>

#include "..\..\..\..\include\driver.h"
#define  MAX_DEVICES  100

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

void main()
{
    DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;
    int    i;
    DWORD  dwEventCount;
	BOOL   bPrint;
	BYTE   DITrigger[16];
	ULONG  ulDataLength;

    USHORT	usEventType;
	PT_EnableEvent EventSetting;
	PT_CheckEvent ptCheckEvent;
    short gnNumOfDevices;
    short nOutEntries; 
	DEVLIST DeviceList[MAX_DEVICES ];
	USHORT usChan;
	USHORT usPort;
    USHORT usMaxPort;
	DEVFEATURES devFeatures;
	PT_DeviceGetFeatures  ptDevGetFeatures;
	int    Sel;
    double dfiltercount;

    //Step 1: Display hardware and software settings for running this example
	printf("---------------Demo the Digital Input with Interrupt Event for PCI-1758----------\n");
    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);
    }
	
    //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, &lDriverHandle);   
    if (dwErrCde != SUCCESS)
    {
        ErrorHandler(dwErrCde);
        printf("Program terminated!\n");

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

	// Step 4: Read the features of the device
    ptDevGetFeatures.buffer = &devFeatures;
	ptDevGetFeatures.size = sizeof(DEVFEATURES);
    dwErrCde = DRV_DeviceGetFeatures(lDriverHandle,(LPT_DeviceGetFeatures)&ptDevGetFeatures);
    if (dwErrCde != SUCCESS)
    {
        ErrorStop(&lDriverHandle, dwErrCde);
        return;
    }
	usMaxPort = devFeatures.usMaxDIChl/8 - 1;
	printf("\rPlease input port:(0~%d)", usMaxPort);
	scanf("%d", &usPort);
	
	printf("\rInput Channel:(0 to 7)");
	scanf("%d", &usChan);
	
	//Step 5 Set the trigger and filter funcations
	//set the channel rising trigger 
	printf("\rEnable trigger mode: Rising trigger (1 enalbed, 0 not enabled)?");
	scanf("%d",&Sel);
	if ( Sel == 1)
	{
		ulDataLength = sizeof(BYTE) ;
		DITrigger[usPort]=0xff; //port 0 all rising trigger
		dwErrCde = DRV_DeviceSetProperty(lDriverHandle, CFG_DiTriggerEnableRisingPort0+usPort,
										&(DITrigger[usPort]), ulDataLength);
           
		if ( dwErrCde != SUCCESS )
		{
			ErrorHandler(dwErrCde);
			printf("Program terminated!\n");
			
			printf("Press any key to exit....");
			getch();
			exit(1);
		}
	}
	
	//set the channel falling trigger
	printf("\rEnable trigger mode: Falling trigger (1 enalbed, 0 not enabled)?");
	scanf("%d",&Sel);
	if (Sel == 1)
	{
		ulDataLength = sizeof(BYTE) ;
		DITrigger[usPort]=0xff; //port 0 all rising trigger
		dwErrCde = DRV_DeviceSetProperty(lDriverHandle, CFG_DiTriggerEnableFallingPort0+usPort,
			&(DITrigger[usPort]), ulDataLength);
		
		if ( dwErrCde != SUCCESS )
		{
			ErrorHandler(dwErrCde);
			printf("Program terminated!\n");
			
			printf("Press any key to exit....");
			getch();
			exit(1);
		}
	}
	//set the channel filter funcation
	printf("\rEnable filter funcation (1 enalbed, 0 not enabled)?");
	scanf("%d",&Sel);
	if (Sel == 1)
	{
		ulDataLength = sizeof(BYTE) ;
		DITrigger[usPort]=0xff; //port 0 all rising trigger
		dwErrCde = DRV_DeviceSetProperty(lDriverHandle, CFG_DiFilterEnablePort0+usPort,
			&(DITrigger[usPort]), ulDataLength);
		
		if ( dwErrCde != SUCCESS )
		{
			ErrorHandler(dwErrCde);
			printf("Program terminated!\n");
			
			printf("Press any key to exit....");
			getch();
			exit(1);
		}

		//set the filter counter
		printf("\rSet filter counter, for example 100 (100*0.2us = 50KHz ):");
		scanf("%lf",&dfiltercount);
		dfiltercount = 10000.0/(dfiltercount*2.0);
		ulDataLength = sizeof(BYTE) ;
		dwErrCde = DRV_DeviceSetProperty(lDriverHandle, CFG_DiFilterIntervalCounter,
				&dfiltercount, ulDataLength);
			
		if ( dwErrCde != SUCCESS )
		{
			ErrorHandler(dwErrCde);
			printf("Program terminated!\n");
			
			printf("Press any key to exit....");
			getch();
			exit(1);
		}
	}
	
    // Step 6: Enable DI of channel with interrupt by DRV_EnableEvent
	EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + usChan + usPort * 8;   // event type: DI with interrupt 
	EventSetting.Enabled = true;      // event DI with interrupt
	EventSetting.Count = 1;           // each interrupt generates an event
	
	dwEventCount = 0;
	dwErrCde = DRV_EnableEvent( lDriverHandle, &EventSetting );
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");
		
		printf("Press any key to exit....");
		getch();
		exit(1);
	}

    // Step 7: Check event by DRV_CheckEvent in while loop, exit at pressing
    //         any key. If DRV_CheckEvent returns successful, increases
    //         dwEventCount
	ptCheckEvent.EventType = &usEventType;
	ptCheckEvent.Milliseconds = 300;
	
    printf("\nWaiting DI interrupt signal.\n");
    printf("Press any key to exit the waiting loop....\n");
    while (!kbhit())
    {
		dwErrCde = DRV_CheckEvent( lDriverHandle, &ptCheckEvent );

        //Check function calling successful 
		if (dwErrCde == SUCCESS)
        {
            //Check desired event generation
			if (usEventType == (ADS_EVT_INTERRUPT_DI0 + usChan + usPort*8))
			{   
				dwEventCount++;
				bPrint = true;
			}
			if (bPrint)
			{
				printf("\rEvent count:  %lu",dwEventCount);
				bPrint = false;
			}
        }
    }
    if (kbhit()) getch();
    
    // Step 8: Disable event by DRV_EnableEvent
	EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + usChan + usPort*8;   // event type: DI with interrupt 
	EventSetting.Enabled = false;      // event DI with interrupt
	EventSetting.Count = 1;           // each interrupt generates an event
	dwErrCde = DRV_EnableEvent( lDriverHandle, &EventSetting );
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");
		
		printf("Press any key to exit....");
		getch();
		exit(1);
	}


    // Step 9: Close device
    dwErrCde = DRV_DeviceClose(&lDriverHandle);
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");

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

    printf("\nPress any key to exit....");
    getch();

}//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 + -