📄 dasync.cpp
字号:
/*
****************************************************************************************
* Program : DASYNC.CPP *
* Description : Demo program for analog output function with synchronous output *
* Boards Supp. : PCI-1720 *
* APIs used : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage, *
* DRV_EnableSyncAO, DRV_AOCurrentOut, DRV_AOVoltageOut, *
* DRV_WriteSyncAO *
* 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 usOutType;
int nChan;
float fCurrentBuf[4] = {5.0f, 10.0f, 15.0f, 20.0f};
float fVoltageBuf[4] = {5.0f, 2.5f, 1.25f, 0.0f};
PT_AOVoltageOut tAOVoltageOut;
PT_AOCurrentOut tAOCurrentOut;
//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 Channels: 0 to 3");
printf("\nOutput Type (Current:0, Volage: 1): ");
scanf("%d", &usOutType);
if (usOutType == 0)
printf("\nOutput Values for Current: chan0-%5.3fmA, chan1-%5.3fmA, chan2-%5.3fmA, chan3-%5.3fmA",
fCurrentBuf[0], fCurrentBuf[1], fCurrentBuf[2], fCurrentBuf[3]);
else
printf("\nOutput Values for Voltage: chan0-%5.3fV, chan1-%5.3fV, chan2-%5.3fV, chan3-%5.3fV",
fVoltageBuf[0], fVoltageBuf[1], fVoltageBuf[2], fVoltageBuf[3]);
//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: Enable synchronous output by DRV_EnableSyncAO
dwErrCde = DRV_EnableSyncAO(lDriverHandle, 1);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
// Step 5: Output values by DRV_AOCurrentOut or DRV_AOVoltageOut
if (usOutType == 0)
{
// current output
for (nChan = 0; nChan < 4; nChan ++)
{
tAOCurrentOut.chan = nChan;
tAOCurrentOut.OutputValue = fCurrentBuf[nChan];
dwErrCde = DRV_AOCurrentOut(lDriverHandle,&tAOCurrentOut);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
}
}
else
{
// voltage output
for (nChan = 0; nChan < 4; nChan ++)
{
tAOVoltageOut.chan = nChan;
tAOVoltageOut.OutputValue = fVoltageBuf[nChan];
dwErrCde = DRV_AOVoltageOut(lDriverHandle,&tAOVoltageOut);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
}
}
// Step 6: Synchronous output by DRV_WriteSyncAO
dwErrCde = DRV_WriteSyncAO(lDriverHandle);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
// Step 7: Disable synchronous output by DRV_EnableSyncAO
dwErrCde = DRV_EnableSyncAO(lDriverHandle, 0);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
// Step 8: 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 + -