📄 addmabm.cpp
字号:
/*
****************************************************************************************
* Program : ADDMABM.CPP *
* Description : Demo program for analog input function with DMA triggering *
* Revision : 1.00 *
* Date : 7/8/1999 Advantech Co., Ltd. *
****************************************************************************************
*/
// This Demo program is rewrited by Huang, Yingsong in July, 2003
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\include\driver.h"
//Static variable defined
static USHORT gwEvtFlag = 1; // Event flag
static USHORT gwCyclicCount= 0; // Number of cyclic count
static USHORT gwCyclicMode = 0; // Cyclic or non-cyclic mode
static ULONG gulConvNum = 200000;//40960; // conversion number
static USHORT gwDataType=1;
static USHORT ReorderGainCode[64];
static USHORT gwStartChl = 0, gwChlNum = 1; // scan channels
static DEVFEATURES DevFeatures; // structure for device features
static PT_FAIDmaExStart ptFAIDmaExStart; // FAIIntStart table
static PT_FAITransfer ptFAITransfer; // FAITransfer table
static PT_FAICheck ptFAICheck; // FAICheck table
static PT_DeviceGetFeatures ptDevFeatures; // Devfeatures table
static PT_EnableEvent ptEnableEvent; // Enable event
static PT_CheckEvent ptCheckEvent; // Check event
static BOOL bThreadloop = FALSE;
static BOOL bThreadFlag = FALSE; //the flag of the thread
// Buffer defined
HGLOBAL hUserBuf, hDisplayBuf;
// Error variable defined
char szErrMsg[80];
DWORD dwErrCde;
LONG ErrCde;
long lDriverHandle; //DriverHandle defined
DWORD dwDeviceNum;
#define MAX_DEVICES 100
/******************
* Local function declare
******************/
void ErrorHandler(DWORD dwErrCde);
void ErrorStop(long*, DWORD);
//Thread function declare:
void UserThread(LPVOID );
// Event functions declare:
void adInterruptEvent();
void adBufChangeEvent(int );
void adOverrunEvent();
void adTerminateEvent(int );
//Cyclic mode function declare
void GetStatus(int );
void Stop(int );
void main()
{
ULONG lDevNum = 0;
USHORT usChan = 0;
int i;
short gnNumOfDevices;
short nOutEntries;
DEVLIST DeviceList[MAX_DEVICES ];
// long lDmaBuf; //DMA buffer defined
// PT_FAIDmaStart ptFAIDmaStart;
HANDLE hThreadHandle;
DWORD dwThreadID;
//Step 1: Display hardware and software settings for running this example
printf("\n\t\t\tADDMABM CONSOLE MODE SAMPLE v2.0\n\n");
printf("Before running this example,\n");
printf("please use the device installation utility to add the device on Device Number.\n\n");
// Add type of PC Laboratory Card
ErrCde = DRV_DeviceGetList(&DeviceList[0], MAX_DEVICES, &nOutEntries);
// Return the number of devices which you install in the system using
// Device Installation
ErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
printf("This is the installed device list:\n");
for (i = 0; i < gnNumOfDevices; i++)
{
printf(" %3.3d %s\n",DeviceList[i].dwDeviceNum,DeviceList[i].szDeviceName);
}
//Step 2: Input parameters
fflush(stdin);
printf("\nPlease input parameters-----------------");
printf("\nDevice Number: ");
scanf("%d", &dwDeviceNum);
fflush(stdin);
printf("Input Start Channel: ");
scanf("%d", &gwStartChl);
fflush(stdin);
printf("Input Scan Channel Number: ");
scanf("%d", &gwChlNum);
fflush(stdin);
printf("Cyclic model? (noncyclic:0, cyclic:1):");
scanf("%d", &gwCyclicMode);
fflush(stdin);
printf("\nSampling counts, should large than FIFO size [ > 2048] \nand the multiple of 2: "); //Modified by Huang,Yingsong 7/24/03
scanf("%d", &gulConvNum);
fflush(stdin);
while (gulConvNum<=2048 || gulConvNum%2!=0) {
printf("Wrong input, the input value should greater than 2048 \nand the multiple of 2, input again:");
scanf("%d", &gulConvNum);
fflush(stdin);
}
//Step 3: Open Device
ErrCde = DRV_DeviceOpen(dwDeviceNum, (LONG far *)&lDriverHandle);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
// Step 4: Get device features
ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
ptDevFeatures.size = sizeof(DEVFEATURES);
if ((ErrCde = DRV_DeviceGetFeatures(lDriverHandle,
(LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
// Step 5: Allocate memory for Voltage data or Raw data
//********Version 2.0B*******************
// As with DMA transfers, it's better to start the buffer at
// page boundary(4096 Bytes). If the buffers used for the
// DMA operation is aligned on addresses of page boundary, user
// can get exact page change event(just set the Count of
// PT_EnableEvent to 2048, it means 1 page). Once Interrupt
// event occurs, it means 2048 samples(4096 byes) are available.
//
// It is why we changed the memory allocation policy from
// Solution A to solution B at Version 2.0B.
//
// At the same time, the buffer that receives the samples must
// be a multiple of 4096 bypes(2048 samples). In cycle mode, if
// you are interested in buffer change event, the minimal buffer
// is 8192 bytes(4096 samples).
//****************************************
// Solution A:
if((hUserBuf=(FLOAT far *)GlobalAlloc(GHND,
sizeof(FLOAT) * gulConvNum)) == 0)
{
ErrorHandler(dwErrCde);
//Close device
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
exit(0);
return;
}
/*
// Solution B:
// Win9x seems don't support the routine well.
// so it's recommended only to use it at WinNT/2K.
hUserBuf = VirtualAlloc( NULL, // region to reserve or commit
2 * gulConvNum, // size of region
MEM_COMMIT|MEM_RESERVE, // type of allocation
PAGE_READWRITE ); // type of access protection
*/
// End of solution B.
if((hDisplayBuf=(FLOAT far *)GlobalAlloc(GHND,
sizeof(FLOAT) * gulConvNum)) == 0)
{
VirtualFree(
hUserBuf, // base address of block
2 * gulConvNum, // bytes of committed pages
MEM_DECOMMIT| MEM_RELEASE); // decommit the pages
DRV_DeviceClose((LONG far *)&lDriverHandle);
return ;
}
ptFAITransfer.DataBuffer = (FLOAT far *)GlobalLock(hDisplayBuf);
// Step 6: Enable event feature
ptEnableEvent.EventType |= ADS_EVT_BUFCHANGE | ADS_EVT_TERMINATED
| ADS_EVT_OVERRUN | ADS_EVT_INTERRUPT;
ptEnableEvent.Enabled = gwEvtFlag;
ptEnableEvent.Count = (USHORT)(1 * 2048);
if ((ErrCde = DRV_EnableEvent(lDriverHandle,
(LPT_EnableEvent)&ptEnableEvent)) != 0)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
for (i=0 ; i < 64 ; i++)
{
ReorderGainCode[i] = 0;
}
// Step 7: Start DMA transfer
ptFAIDmaExStart.TrigSrc = 0;
ptFAIDmaExStart.TrigMode = 0;
ptFAIDmaExStart.ClockSrc = 0;
ptFAIDmaExStart.TrigEdge = 0;
ptFAIDmaExStart.SRCType = 0;
ptFAIDmaExStart.CyclicMode = gwCyclicMode;
ptFAIDmaExStart.TrigVol = 0;
ptFAIDmaExStart.StartChan = gwStartChl;
ptFAIDmaExStart.NumChans = gwChlNum;
ptFAIDmaExStart.ulDelayCnt = 2;
ptFAIDmaExStart.SampleRate = 250000;
ptFAIDmaExStart.GainList = &ReorderGainCode[0];
ptFAIDmaExStart.count = gulConvNum;
ptFAIDmaExStart.buffer0 = (USHORT *)hUserBuf;
if ((ErrCde = DRV_FAIDmaExStart(lDriverHandle,
(LPT_FAIDmaExStart)&ptFAIDmaExStart)) != 0)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
//Step8: Start the Thread
if (gwEvtFlag)
{
hThreadHandle = CreateThread(0, 0,
(LPTHREAD_START_ROUTINE) UserThread,
&gulConvNum, 0, (LPDWORD)&dwThreadID);
bThreadFlag = TRUE;
bThreadloop = TRUE;
}
Sleep(0);
bThreadFlag = true;
//Step9: Wait untill the DMA transmition completed or Ueser's command
if(gwCyclicMode)
{
while(!kbhit())
{
Stop(gulConvNum);
}
}
getch();
if(bThreadFlag)
Stop(gulConvNum);
// Step 10: Close device
dwErrCde = DRV_DeviceClose(&lDriverHandle);
getch();
CloseHandle(hThreadHandle);
}
/**********************************************************************
* Function: ErrorHandler
* Show the error message of corresponding errcode
* 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);
}
/**********************************************************************
* Function: ErrorStop
* Close all resource and terminate program if
* error occur in running transfer.
* Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
* dwErrCde, IN, Error code.
* plDMABuf, IN, Address of DMA buffer allocated for transfer.
* pUserBuf, IN, Address of User buffer.
* return: none
**********************************************************************/
void ErrorStop(long *pDrvHandle, DWORD dwErrCde)//, long *plDMABuf, void**pUserBuf)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -