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

📄 diint.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : DIINT.CPP                                                           *
 * 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           : 7/8/1999                   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;
	USHORT usPEnable,usPValue,usSEnable;
	USHORT usPEnable1,usPValue1,usSEnable1;
    short gnNumOfDevices;
    short nOutEntries;
	int   i;
    DWORD  dwSatusCount=0,dwMatchCount=0, dwIntCount=0;
	PT_EnableEventEx      Zero = {0};

 
	DEVLIST DeviceList[MAX_DEVICES ];

    USHORT	usEventType;
	PT_EnableEvent EventSetting;
	PT_CheckEvent ptCheckEvent;
    PT_EnableEventEx  ptEnableEventEx;     // Enable event

    //Step 1: Display hardware and software settings for running this example
    printf("\n\t\tDigital Input with Interrupt Event Sample for PCI-1753\n\n");
    printf("Before running this example, please\n");
    printf("use the device installation utility to add the device.\n\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);
    printf("\nPattern Match Enable Port A0 Mask(Hex):");
    scanf("%x", &usPEnable);
    printf("Pattern Match Port A0 Value(Hex):");
    scanf("%x", &usPValue);
    printf("\nPattern Match Enable Port A4 Mask(Hex):");
    scanf("%x", &usPEnable1);
    printf("Pattern Match Port A4 Value(Hex):");
    scanf("%x", &usPValue1);
    printf("\nState Change Enable Port B0 Mask(Hex):");
    scanf("%x", &usSEnable);
    printf("State Change Enable Port B4 Mask(Hex):");
    scanf("%x", &usSEnable1);

    usPEnable = usPEnable+(usPEnable1<<8);
    usPValue = usPValue+(usPValue1<<8);
    usSEnable = usSEnable+(usSEnable1<<8);
    //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: Enable Pattern & Status with interrupt by DRV_EnableEventEx

    // Enable Pattern Match event feature
	ptEnableEventEx.Pattern.usEventType = ADS_EVT_PATTERNMATCH;
	ptEnableEventEx.Pattern.usEventEnable = TRUE;
	ptEnableEventEx.Pattern.usCount   = 1;
	ptEnableEventEx.Pattern.usEnable = usPEnable;
	ptEnableEventEx.Pattern.usValue = usPValue;
	dwErrCde = DRV_EnableEventEx(lDriverHandle,
							(LPT_EnableEventEx)&ptEnableEventEx);
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");

        printf("Press any key to exit....");
        getch();
        exit(1);
    }
    // Enable Status Change event feature
	ptEnableEventEx.Status.usEventType = ADS_EVT_STATUSCHANGE;
	ptEnableEventEx.Status.usEventEnable = TRUE;
	ptEnableEventEx.Status.usCount   = 1;
	ptEnableEventEx.Status.usEnable = usSEnable;
	dwErrCde = DRV_EnableEventEx(lDriverHandle,
							(LPT_EnableEventEx)&ptEnableEventEx);
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");

        printf("Press any key to exit....");
        getch();
        exit(1);
    }
    // Step 5: Enable DI with interrupt by DRV_EnableEvent
    EventSetting.EventType = ADS_EVT_INTERRUPT;   // event type: DI with interrupt 
    EventSetting.Enabled = true;      // 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 6: 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....");
    while (!kbhit())
    {
		dwErrCde = DRV_CheckEvent( lDriverHandle, &ptCheckEvent );
        //Check function calling successful 
		if (dwErrCde == SUCCESS)
        {
            //Check desired event generation
            if (usEventType == ADS_EVT_INTERRUPT )
            {   
                dwIntCount++;
            }
			// Process pattern match event
			if (usEventType == ADS_EVT_PATTERNMATCH)
			{
				dwMatchCount++;
			}

			// Process status change event
			if (usEventType == ADS_EVT_STATUSCHANGE)
			{
				dwSatusCount++;
			}
		    printf("\nInterrupt Count = %d; Pattern Match count = %d; Status Change  count = %d",
					dwIntCount,dwMatchCount,dwSatusCount);

		}
		else
		{	
			ErrorHandler(dwErrCde);
			printf("Program terminated!\n");

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

    // Step 7: Disable event by DRV_EnableEvent

    ptEnableEventEx = Zero;
    dwErrCde = DRV_EnableEventEx(lDriverHandle,(LPT_EnableEventEx)&ptEnableEventEx);


	EventSetting.EventType = ADS_EVT_INTERRUPT;   // 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 8: Close device
    dwErrCde = DRV_DeviceClose(&lDriverHandle);
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");

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

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