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

📄 madsoft.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : ADSOFT.CPP                                                          *
 * Description    : Demo program for analog input function with software triggering     *
 * Boards Supp.   : PCL-818 series/816/1800/812PG/711B, MIC-2718, PCM-3718              *
 *                  PCI-1710/1713, PCL-813B, Demo board,                                *
 *                  ADAM-4011/4011D/4012/4014D/4018/4018M/5018/4017/4013/5017/4016      *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_AIVoltageIn                     *
 *                  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;
    float  fVoltage[32];
	DEVFEATURES     DevFeatures;    // structure for device features
    DEVCONFIG_AI    DevCfg;         // structure for DEVCONFIG_AI table
    PT_MAIConfig        ptMAIConfig;        // structure for MAIConfig table
    PT_MAIVoltageIn     ptMAIVoltageIn;     // structure for MAIVoltageIn table
    PT_MAIVoltageInExp  ptMAIVoltageInExp;  // structure for MAIVoltageInExp table
    PT_DeviceGetFeatures  ptDevFeatures;    // structure for DeviceGetFeatures
    PT_AIGetConfig  ptAIGetConfig;          // structure for AIGetConfig table
	USHORT      usStartChan;                    // start channel
	USHORT      usNumChan  ;                      // Number of channel
	USHORT      usGainCode[32];                     // varied input range array
	USHORT		usExpFlag=0;
	USHORT      usDasChan[32];					    // das Channel array
	USHORT      usGainIndex[32];                    // varied input range index
	USHORT      usExpChan[32];                      // varied expansion chan
	USHORT      usExpChanTemp[32];                  // varied for PCLD-788 use
	USHORT      usBoardID [16];                     // varied board ID for 788
	int			i;



    //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);

    printf("Input Start Channel: ");
    scanf("%d", &usStartChan);

    printf("Input Channel Number: ");
    scanf("%d", &usNumChan);


    //Step 3: Open device
    dwErrCde = DRV_DeviceOpen(lDevNum, &lDriverHandle);   
    if (dwErrCde != SUCCESS)
    {
        ErrorHandler(dwErrCde);
        DRV_DeviceClose((LONG far *)&lDriverHandle);
       printf("Program terminated!\n");

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

    //Step 4: Config device
    ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
    ptDevFeatures.size = sizeof(DEVFEATURES);
    if ((dwErrCde = DRV_DeviceGetFeatures(lDriverHandle,
        (LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
    {
		ErrorStop(&lDriverHandle, dwErrCde);
		return;
    }

    //
    // configures the gain for the specifed analog input channel
    //
    ptAIGetConfig.buffer = (LPDEVCONFIG_AI)&DevCfg;
    ptAIGetConfig.size = sizeof(DEVCONFIG_AI);

    if ((dwErrCde = DRV_AIGetConfig(lDriverHandle,
        (LPT_AIGetConfig)&ptAIGetConfig)) != SUCCESS)
    {
		ErrorStop(&lDriverHandle, dwErrCde);
		return;
    }

    // check start and stop channel if expansion board ?
    usExpFlag = 0;
    for (i = usStartChan ; i < usStartChan+usNumChan ; i++)
    {
        if (DevCfg.Daughter[i&0xf].dwBoardID != 0)
            usExpFlag = 1;
    }

    if(usExpFlag == 0)
    {
       ptMAIConfig.NumChan   = usNumChan;
       ptMAIConfig.StartChan = usStartChan;
       for (i=0 ; i< 32 ; i++)
         usGainCode[i] =0; 
       ptMAIConfig.GainArray = (USHORT far *) &usGainCode[usStartChan];

       if ((dwErrCde = DRV_MAIConfig(lDriverHandle,
            (LPT_MAIConfig)&ptMAIConfig)) != 0)
       {
			ErrorStop(&lDriverHandle, dwErrCde);
			return;
       }
    }

    
    // Step 5: Read data

        if(usExpFlag == 0)
        {
           ptMAIVoltageIn.NumChan   = usNumChan ;
           ptMAIVoltageIn.StartChan = usStartChan;
           ptMAIVoltageIn.GainArray = (USHORT far *) &usGainCode[usStartChan];
           ptMAIVoltageIn.TrigMode = 0;            // internal trigger
           ptMAIVoltageIn.VoltageArray = (FLOAT far *)&fVoltage[usStartChan];

           if ((dwErrCde = DRV_MAIVoltageIn(lDriverHandle,
                      (LPT_MAIVoltageIn)&ptMAIVoltageIn)) != 0)
           {
				ErrorStop(&lDriverHandle, dwErrCde);
				return;
           }

        }
        else
        {
           for (i = usStartChan ; i < usStartChan+usNumChan ; i++)
           {
              usDasChan[i] = i;
			  usGainIndex[i] = 0;

              // for PCLD-788
              if  (DevCfg.Daughter[i&0xf].dwBoardID == BD_PCLD788)
                  usExpChanTemp[i] = ((usBoardID[i] << 4) | usExpChan[i]);
              else
                  usExpChanTemp[i] = usExpChan[i];
           }

           ptMAIVoltageInExp.NumChan      = usNumChan;
           ptMAIVoltageInExp.DasChanArray = (USHORT far *)&usDasChan[usStartChan];
           ptMAIVoltageInExp.DasGainArray = (USHORT far *)&usGainIndex[usStartChan];
           ptMAIVoltageInExp.ExpChanArray = (USHORT far *)&usExpChanTemp[usStartChan];
           ptMAIVoltageInExp.VoltageArray = (FLOAT far *)&fVoltage[usStartChan];

           if ((dwErrCde = DRV_MAIVoltageInExp(lDriverHandle,
                      (LPT_MAIVoltageInExp)&ptMAIVoltageInExp)) != 0)
           {
				ErrorStop(&lDriverHandle, dwErrCde);
				return;
           }

        }
	// Step 6: Display reading data
	if(usExpFlag==0)
		printf("Read data:\n");
	else
		printf("Read data with Exp:\n");

    for (i = usStartChan ; i < usStartChan+usNumChan ; i++)
		printf("	data[%d] = %10.6f\n", i,fVoltage[i]);

    // Step 7: Close device
    dwErrCde = DRV_DeviceClose(&lDriverHandle);
	if (dwErrCde != SUCCESS)
    {
        ErrorStop(&lDriverHandle, dwErrCde);
        return;
    }

    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);

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -