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

📄 disoft.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : DIGIN.CPP                                                           *
 * Description    : Demo program for digital input function                             *
 * Boards Supp.   : PCL-818 series/816/1800/812PG/711B, PCM-3718/3724                   *
 *                  PCI-1710/1720/1750/1751/1760, PCL-813B/725/730/722/724/731/833      *
 *                  PCL-733/720/721/723/836, MIC-2730/2732                              *
 *                  ADAM-4011/4011D/4012/4014D/4052/4053/5051/5052/5050/4050            *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_DioReadBit                      *
 *                  DRV_GetErrorMessage                                                 *
 * 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"

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

void main()
{
    DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;
    USHORT usChan;
	USHORT usState;
	USHORT usPort;
    USHORT usMaxPort;
	DEVFEATURES devFeatures;
	PT_DeviceGetFeatures  ptDevGetFeatures;
	PT_DioReadBit ptDioReadBit;

    //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
    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;
    }
	if (devFeatures.usMaxDIChl<1)
	{
		dwErrCde = DRV_DeviceClose(&lDriverHandle);
		printf("\n Device not support DI function.");		
		printf("\nPress any key to exit....");
		getch();
		exit(1);
	}
	usMaxPort = (devFeatures.usMaxDIChl+7)/8 - 1;
	printf("\nPlease input port:(0~%d)", usMaxPort);
	scanf("%d", &usPort);

	printf("\nInput Channel (0 to 7): ");
	scanf("%d", &usChan);
	   

    // Step 5: Read state from the specified channel
    ptDioReadBit.port = usPort;                        // input port: 0
    ptDioReadBit.bit = usChan;                    // input channel
    ptDioReadBit.state = &usState;  // returned data

    dwErrCde = DRV_DioReadBit(lDriverHandle, 
                     (LPT_DioReadBit)&ptDioReadBit);
    if (dwErrCde != SUCCESS)
    {
        ErrorStop(&lDriverHandle, dwErrCde);
        return;
    }

    // Step 6: Display reading data
    printf("\nReading data = %d\n", usState);

    // Step 7: Close device
    dwErrCde = DRV_DeviceClose(&lDriverHandle);

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