📄 di_int_scan.cpp
字号:
/*
****************************************************************************************
* Program : DI_INT_SCAN.CPP *
* Description : Demo program for digital input function with interrupt event *
* Boards Supp. : *
* APIs used : DRV_DeviceOpen, *
* DRV_DeviceClose, *
* DRV_GetErrorMessage, *
* AdxDioEnableEventAndSpecifyDiPorts, *
* AdxDioDisableEvent, *
* AdxDioGetLatestEventDiPortsState, *
* DRV_CheckEvent *
* Revision : 1.00 *
* Date : 2/24/2005 Advantech Co., Ltd. *
****************************************************************************************
*/
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\include\driver.h"
/******************************
* struct definition *
******************************/
typedef struct _DEV_INFO{
LONG lDevHandle; //Device handle
//Device Properties
ULONG ulDiChannelCount; //DI Channel Count of device. read-only.
ULONG ulDiPortCount; //DI port Count. read-only.
BYTE * pDiIntSupportedChannel; //bit-map array, each bit indicates whether a
//channel support DI interrupt or not. read-only.
//Experiment Setting
ULONG ulDiIntEnabledChannel; //User selected Channel which DI Interrupt will be enabled.
ULONG ulScanStart; //User specified port range to scan when the DI Interrupt occurs.
ULONG ulScanCount;
} DEV_INFO, *PDEV_INFO, *LPDEV_INFO;
/******************************
* Forward declaration *
******************************/
void PrintNote(void);
BOOL OpenDevice ( LONG * pDevHandle );
BOOL GetDeviceInfo ( LPDEV_INFO pDev );
BOOL SetupDiInterrupt ( LPDEV_INFO pDev );
BOOL CheckDiInterrupt ( LPDEV_INFO pDev );
void FreeResource ( LPDEV_INFO pDev );
void ErrorHandler(DWORD dwErrCde);
void PrintErrorMsg (LPCSTR prefix, LPCSTR msg);
int main(int argc, char* argv[])
{
DEV_INFO dev = {0};
dev.ulDiIntEnabledChannel = -1; //Default value, No channel was selected by user.
//Step 1: Display hardware and software settings for running this example
PrintNote();
//Step 2: Open Device
if ( !OpenDevice( &dev.lDevHandle ) )
{
return -1;
}
//Step 3: Get the device info
if ( !GetDeviceInfo( &dev ) )
{
FreeResource( &dev );
return -1;
}
//Step 4: Setup DI Interrupt
if ( !SetupDiInterrupt( &dev ) )
{
FreeResource( &dev );
return -1;
}
// Step 5: Check DI Interrupt
CheckDiInterrupt( &dev );
// Step 6: Free Resource
FreeResource( &dev );
return 0;
}
/**********************************************************************
* Function: ErrorHandler
* Show the error message for the corresponding error code
* input: dwErrCde, IN, Error code
* return: none
**********************************************************************/
void ErrorHandler(DWORD dwErrCde)
{
char szErrMsg[MaxErrMsgLen];
char szErrPrefix[MaxErrMsgLen];
//Get Error Message
DRV_GetErrorMessage(dwErrCde, szErrMsg);
//Build Error message prefix
sprintf( szErrPrefix, "Error(%d): ", dwErrCde & 0xffff );
PrintErrorMsg( szErrPrefix, szErrMsg );
}
/**********************************************************************
* Function: PrintErrorMsg
* Show the error message
* input: prefix[in] -- prefix of the error message, if any.
msg[in] -- error message.
* return: none
**********************************************************************/
void PrintErrorMsg(LPCSTR prefix, LPCSTR msg)
{
if ( prefix != NULL )
{
printf("\n%s%s", prefix, msg);
}
else
{
printf("\n%s", msg);
}
printf("\nPress any key to continue....");
getch();
}
/**********************************************************************
* Function: PrintNote
* print note information
* Paramaters: none
*
* return: none
**********************************************************************/
void PrintNote( )
{
printf("\n**********************************************************");
printf("\nDemo program for digital input function with interrupt");
printf("\nevent. Before running this example, please use the device "
"\ninstallation utility to add the device.");
printf("\n**********************************************************\n");
}
/**********************************************************************
* Function: Open the Device according the Device Number
*
* Paramaters: pDrvHandle[OUT] -- Device handle opened.
*
* return: TRUE for SUCCESS, FALSE for failure.
**********************************************************************/
BOOL OpenDevice( LONG * pDevHandle )
{
ULONG lDevNum;
DWORD dwErrCde;
//Get the user selected device number
printf("\nPlease input parameters:");
printf("\nDevice Number (check the device installation utility): ");
scanf("%d", &lDevNum);
//Open device
dwErrCde = DRV_DeviceOpen(lDevNum, pDevHandle);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
return FALSE;
}
return TRUE;
}
/**********************************************************************
* Function: Get the Device information about DI
*
* Paramaters: pDevInfo[in] -- pointer to DEV_INFO struct.
*
* return: TRUE for SUCCESS, FALSE for failure.
**********************************************************************/
BOOL GetDeviceInfo( LPDEV_INFO pDev )
{
ULONG ulBufSize;
DWORD dwError;
ULONG i,j;
if ( pDev->lDevHandle == NULL )
{
printf("\nInvalidate Device Handle!");
return FALSE;
}
// Get DI port Count.
ulBufSize = sizeof( pDev->ulDiPortCount );
dwError = DRV_DeviceGetProperty(
pDev->lDevHandle,
CFG_DiPortCount,
&pDev->ulDiPortCount,
&ulBufSize );
if ( dwError != SUCCESS )
{
PrintErrorMsg(NULL,"The device has no DI port.");
return FALSE;
}
pDev->ulDiChannelCount = pDev->ulDiPortCount * 8;
// Get DI Channels which support DI Interrupt
// At first get the buffer size needed.
ulBufSize = 0;
dwError = DRV_DeviceGetProperty(
pDev->lDevHandle,
CFG_DiInterruptSupportedChannel,
NULL,
&ulBufSize );
if ( dwError != SUCCESS )
{
PrintErrorMsg(NULL, "The device doesn't support 'DI Interrupt' function!");
return FALSE;
}
// Allocate memory for CFG_DiInterruptSupportedChannel
pDev->pDiIntSupportedChannel = (BYTE*)malloc( ulBufSize );
if ( pDev->pDiIntSupportedChannel == NULL )
{
PrintErrorMsg(NULL,"Not enough memory!");
return FALSE;
}
memset( pDev->pDiIntSupportedChannel, 0, ulBufSize );
// Get DI channels which support DI Interrupt again.
dwError = DRV_DeviceGetProperty(
pDev->lDevHandle,
CFG_DiInterruptSupportedChannel,
pDev->pDiIntSupportedChannel,
&ulBufSize );
if ( dwError != SUCCESS )
{
ErrorHandler( dwError );
return FALSE;
}
// Display device information
printf("\n**********************************************************");
printf("\nDI Information:");
printf("\nDI Port Count: %d", pDev->ulDiPortCount);
printf("\nDI Channels support DI Interrupt Function: ");
for( i = 0; i < pDev->ulDiPortCount; ++i )
{
if ( pDev->pDiIntSupportedChannel[i] != 0 )
{
for ( j = 0; j < 8; ++j )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -