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

📄 daint.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : DAINT.CPP                                                           *
 * Description    : Demo program for analog output function with interrupt triggering   *
 * Boards Supp.   : PCL-1800/816                                                        *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage,                *
 *                  DRV_FAOIntStart, DRV_FAOStop, DRV_FAOScale                          *
 * Revision       : 1.00                                                                *
 * Date           : 7/8/1999                   Advantech Co., Ltd.                      *
 ****************************************************************************************
 */
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

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

/******************************
 * Local function declaration *
 ******************************/
void ErrorHandler(DWORD dwErrCde);
BOOL AllocateDataBuffer(int, void **, void**);
void FreeDataBuffer(void **pVoltageBuf, void **pBinaryBuf);
void ErrorStop(long*, DWORD, void **, void**);
void SetRealBuffer(float *lpBuf, long num, USHORT wPeriod);

void main()
{
    DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;
    USHORT usChan;
	int    nNumberOfOutputs;
    float  *pVoltageBuf;
	USHORT *pBinaryBuf;
	int    ch;

	PT_FAOIntStart     tFAOIntStart;
	PT_FAOScale        tFAOScale;

    //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("\nOutput Channel: ");
    scanf("%d", &usChan);
    nNumberOfOutputs = 500;                     // number of outputs = 500
    
    //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: Allocate memory for driver and user buffer of real voltage
    if( AllocateDataBuffer(
            nNumberOfOutputs,                   // data count
            (void**)&pVoltageBuf,               // voltage buffer allocated
            (void**)&pBinaryBuf)==false)        // binary buffer allocated
    {
        printf("Program terminated\n");
        DRV_DeviceClose(&lDriverHandle);
        
        printf("Press any key to exit....");
        getch();
        exit(1);
    }

    // Step 5: Set values into user buffer by the sine output waveform
    SetRealBuffer(pVoltageBuf, nNumberOfOutputs, nNumberOfOutputs);

    // Step 6: Call DRV_FAOScale to scale real values of voltage buffer 
    //         into binary values for driver
    tFAOScale.VoltArray = pVoltageBuf;
    tFAOScale.BinArray  = pBinaryBuf;
    tFAOScale.chan      = usChan;
    tFAOScale.count     = nNumberOfOutputs;
    dwErrCde = DRV_FAOScale(lDriverHandle, &tFAOScale);
    if (dwErrCde != SUCCESS)
    {
        ErrorStop(&lDriverHandle, dwErrCde, (void **)&pVoltageBuf, (void **)&pBinaryBuf);
        return;
    }

    // Step 7: Call DRV_FAOIntStart to start output operation
    tFAOIntStart.buffer     = (long *)pBinaryBuf;  // analog output data
    tFAOIntStart.TrigSrc    = 0;                   // 0: internal, 1: external trigger
    tFAOIntStart.SampleRate = 1000;                // output rate: 1k Hz one point
    tFAOIntStart.chan       = usChan;              // output channel
    tFAOIntStart.count      = nNumberOfOutputs;    // output count
    tFAOIntStart.cyclic     = 1;                   // 0: noncyclic, 1: cyclic mode
    dwErrCde = DRV_FAOIntStart(lDriverHandle, &tFAOIntStart);
    if (dwErrCde != SUCCESS)
    {
        ErrorStop(&lDriverHandle, dwErrCde, (void **)&pVoltageBuf, (void **)&pBinaryBuf);
        return;
    }

    // Step 8: Press any key to stop the operation by calling DRV_FAOStop
    //         You can call DRV_FAOCheck to check the operation status
    //         or you can use event functions
    printf("\nOutput sine waveform on channel %d with frequency %5.3fHz.", 
            tFAOIntStart.chan, (float)(tFAOIntStart.SampleRate)/tFAOIntStart.count);
	printf("\nPress any key to stop the operation ...");
	ch = getch();

    // Step 9: Free all buffer
	DRV_FAOStop(lDriverHandle);
    FreeDataBuffer((void**)&pVoltageBuf, (void**)&pBinaryBuf);

    // Step 10: Close device
    dwErrCde = DRV_DeviceClose(&lDriverHandle);
}//main


/**********************************************************************
 * Function:   SetRealBuffer
 *             Set output values for sine wave.
 * Paramaters: lpBuf, IN/OUT, data buffer pointer
 *             num, IN, Data count.
 *             wPeriod, IN, the points of one period
 * return:     none
 **********************************************************************/
void SetRealBuffer(float *lpBuf, long num, USHORT wPeriod)
{
    int i;
    float fMagnitude = 2.00f;      // the magnitude of output waveform
    float fOffset = 2.00f;         // the offset of output waveform

    for (i = 0; i < num; i ++)
        *(lpBuf+i) = (float)(fMagnitude * sin(6.28318*(double)i/(double)wPeriod) + fOffset);
}// SetRealBuffer

/**********************************************************************
 * Function:   AllocateDataBuffer
 *             Allocate data buffer for waveform analog output
 * Paramaters: iSamples, IN, Data count.
 *             pVoltageBuf, OUT, voltage buffer pointer.
 *             pBinaryBuf, OUT, binary data buffer. 
 * return:     TRUE - memory allocate successfully
 *             FALSE - allocate failed
 **********************************************************************/
BOOL AllocateDataBuffer(
        int   iSamples, 
        void  **pVoltageBuf, 
        void  **pBinaryBuf)
{
    // Allocate memory for real voltage
    *pVoltageBuf = malloc(iSamples * sizeof(float));
    if (pVoltageBuf == NULL) 
    {
        printf("\nError: Allocate memory error.\n");
        return(false);
    }

    // Allocate memory for binary data
    *pBinaryBuf = GlobalAlloc(GPTR, iSamples * sizeof(unsigned short));
    if (pBinaryBuf == NULL) 
    {
        free(*pVoltageBuf);
        *pVoltageBuf = NULL;
	    printf("\nError: Allocate memory error.\n");
        return(false);
    }
 
    return(true);
}//AllocateDataBuffer 

/**********************************************************************
 * Function:   FreeDataBuffer
 *             Free data buffer allocated by function AllocateDataBuffer
 * Paramaters: pVoltageBuf, IN, Address of voltage buffer pointer 
 *             pBinaryBuf, IN, Address of binary buffer pointer
 * return:     none
 **********************************************************************/
void FreeDataBuffer(void **pVoltageBuf, void **pBinaryBuf)
{
      free(*pVoltageBuf);      
      GlobalFree(*pBinaryBuf);
}//FreeDataBuffer

/**********************************************************************
 * 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.
 *             pVoltageBuf, IN, Address of voltage buffer
 *             pBinaryBuf, IN, Address of binary buffer
 * return:     none             
 **********************************************************************/
void ErrorStop(long *pDrvHandle, DWORD dwErrCde, void **pVoltageBuf, 
               void**pBinaryBuf)
{
    //Free resource 
	DRV_FAOStop(*pDrvHandle);
    FreeDataBuffer(pVoltageBuf, pBinaryBuf);

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