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

📄 dev_remove.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : DEV_REMOVE.CPP                                                           *
 * Description    : Demo program for monitor device removal with ADS_EVT_DEVREMOVED event        *
 * Boards Supp.   : USB series                                                                    *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage,                *
 *                  DRV_EnableEvent, DRV_CheckEvent                                     *
 * Revision       : 1.00                                                                *
 * Date           : 7/8/2005                   Advantech Co., Ltd.                      *
 ****************************************************************************************
 */
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>

#include "..\..\..\include\driver.h"

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

void main()
{
    DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;

    USHORT	usEventType;
	PT_CheckEvent ptCheckEvent;

    //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
	//The ADS_EVT_DEVREMOVED is enabled automatically when the device is opened.
	//
    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: Check event by DRV_CheckEvent in while loop, exit at pressing
    //         any key. If usEventType, return form DRV_CheckEvent, is 
	//		   ADS_EVT_DEVREMOVED, please call DRV_DeviceClose to free device handle.
    //
	ptCheckEvent.EventType = &usEventType;
	ptCheckEvent.Milliseconds = 300;
	
    printf("\nWaiting USB device ejection or 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_DEVREMOVED )
            {   
		        printf("\nUSB device is removed!");
				printf("\nWe should call DRV_DeviceClose() to free device handle!");

				printf("\n\nWaiting device to be closed...");

				// Step 6: 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("\n\nDevice is closed!");
				break;
				
            }
        }
    }
	
	if (lDriverHandle!= NULL)
	{
		DRV_DeviceClose(&lDriverHandle);
	}
    printf("\nPress any key to exit....");
    getch();
	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 + -