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

📄 mainform.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "DisplayForm.h"
#include "GainListForm.h"
#include "MainForm.h"
#include "stdio.h"
#include "..\..\..\include\driver.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------

#define     MAX_DEVICES     100
#define min(a,b) (((a) > (b))?(b):(a))
DEVFEATURES     DevFeatures;    // structure for device features
PT_DeviceGetFeatures  ptDevFeatures;
static      PT_FAIDmaExStart   ptFAIDmaExStart;   // FAIIntStart table
static      PT_FAITransfer     ptFAITransfer;     // FAITransfer table
static      PT_FAICheck        ptFAICheck;        // FAICheck table
static      PT_EnableEvent     ptEnableEvent;     // Enable event
static      PT_CheckEvent      ptCheckEvent;      // Check event
PT_AIGetConfig  lpAIGetConfig;
DEVCONFIG_AI   lpDevConfig_AI ;
HGLOBAL     hUserBuf, hDisplayBuf;

static  DWORD  gdwPacerRate = 250000;               // pacer rate
static  ULONG  gulConvNum   = 200000;//40960;               // conversion number
USHORT gwStartChl   = 0, gwChlNum = 1;   // scan channels
static  USHORT gwDataType   = 1;                  // display type : FLOAT(1)
static  USHORT gwEvtFlag    = 1;                  // event enable(0)
static  ULONG  gwActualBufSize = 0;               // actual allocate buffer size
static  USHORT gwFifoEnable    = 0;               // Fifo disable(0)

static  USHORT gwGainList = 0;      // gain list flag
static  ULONG  gdwStartPt = 0, gdwStopPt = 99;    // display points
static  USHORT gwExtTrig  = 0;      // external or internal trigger
static  USHORT gwClockSrc = 0;		// Clock source
static  USHORT gwTrigEdge = 0;		// Trigger edge
static  USHORT gwSRCType  = 0;	   // Source type
static  FLOAT  gwTrigVol  = 0.0;		// Trigger voltage

static  USHORT gwCyclicMode = 1;    // cyclic or non-cyclic mode
static  USHORT gwTrigMode   = 0;    // Trigger Mode
static  USHORT gwIntrCount  = 1;
static  ULONG  gwCyclicCount= 1;    // number of cyclic count

static  USHORT gwActiveBuf = 0;     // return by FAICheck
static  USHORT gwOverrun   = 0;     // return by FAICheck, FAITransfer
static  USHORT gwStopped   = 0;     // return by FAICheck
static  ULONG gulRetrieved = 0;     // return by FAICheck
static  USHORT gwHalfReady = 0;     // return by FAICheck
static  ULONG gwDelayCnt   = 2;

bool    bThreadloop = true;

int      gwDevice = 0, gwSubDevice = 0;      // Device index

SHORT       gnNumOfDevices, gnNumOfSubdevices;  // number of installed devices

char        szErrMsg[80];               // Use for MESSAGEBOX function
char        szBuffer[40];				// Temperatory buffer
LRESULT     ErrCde;                     // Return error code

DEVLIST     DeviceList[MAX_DEVICES];
DEVLIST     SubDeviceList[MAX_DEVICES];
LONG   DriverHandle = (LONG)NULL;         // driver handle
FLOAT       fVoltageArray[32];

USHORT gwGainCde[64],ReorderGainCode[64];
SHORT gwGain[64];
ULONG gwMaxLogChanNum = 0;
USHORT      usGainIndx[64];
USHORT      usStartChan, usChanNum;
ULONG gulIntCounter     = 0;  // counter by adInterruptEvent
ULONG gulBfChgCounter   = 0;
ULONG gulOverrunCounter = 0;
ULONG gulOverrunTime  = 10000;  // 0 = INFINITE
ULONG gulEventCount   = 1;    
char  szCounter[40];
BOOL  bBufferChange = 1;
BOOL  bInterrupt = 1;
BOOL  bOverrun   = 1;
BOOL  bTerminate = 1;


TfrmMain *frmMain;
//---------------------------------------------------------------------------
int PhyChanToLogChan(DEVCONFIG_AI * pDevCfg,int PhyChan)
{
	int i;
	int result = 0;
        if(pDevCfg->ulChanConfig == 1)
        {
                return PhyChan;
        }
	for(i = 0; i<=(PhyChan>31?31:PhyChan);i++)
	{
		if(pDevCfg->ulChanConfig & (1<<i))
			i++;
		result++;
	}

	if(PhyChan >= 32)
	{
		for(i = 32; i<=PhyChan;i++)
		{
			if(pDevCfg->ulChanConfigEx[0] & (1<<(i-32)))
				i++;
			result++;
		}
	}
	return result - 1;
}

int GetMaxLogChanNum(DEVCONFIG_AI * pDevCfg,DEVFEATURES *pDevFeature)
{
	int i;
	int result = 0;

	if (pDevCfg->ulChanConfig == 1)
	{
		return pDevFeature->usMaxAIDiffChl;
	}

	for(i = 0; i< pDevFeature->usMaxAISiglChl ; i++)
	{
		if(i<32)
			if(pDevCfg->ulChanConfig & (1<<i))
				i++;
		else
			if(pDevCfg->ulChanConfigEx[0] & (1<<(i-32)))
				i++;
		result++;
	}
	return result;
}
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
    int         nOutEntries;

    for(int i=0;i<64;i++)
       gwGain[i]=0;


    if ((ErrCde = DRV_DeviceGetNumOfList((SHORT far *)&gnNumOfDevices)) !=
        SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        exit(0);
    }

    if (gnNumOfDevices > MAX_DEVICES)
        gnNumOfDevices = MAX_DEVICES;

    // Add type of PC Laboratory Card
    if ((ErrCde = DRV_DeviceGetList((DEVLIST far *) &DeviceList[0],
        (SHORT)gnNumOfDevices, (SHORT far *)&nOutEntries)) != (LONG)SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        exit(0);
    }

    // Here MaxEntries = nOutEntries
    for(int i = 0; i < gnNumOfDevices ; i++)
        lstDevice->Items->Add((LPSTR)DeviceList[i].szDeviceName);

    lstDevice->ItemIndex = gwDevice;
    lstDeviceClick(Sender);

    butStop->Enabled = False;

}
//---------------------------------------------------------------------------



void __fastcall TfrmMain::lstDeviceClick(TObject *Sender)
{

    USHORT i;

    cmbGain->Clear();
    gwDevice = lstDevice->ItemIndex;
    if(  gwDevice < 0)
    {
        Application->MessageBox("No installed device!", "Driver Message", MB_OK);
        return;
    }

    ErrCde = DRV_DeviceOpen(DeviceList[gwDevice].dwDeviceNum,
             (LONG far *)&DriverHandle);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);

    }

    // get device features
    ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
    ptDevFeatures.size = sizeof(DEVFEATURES);
    if ((ErrCde = DRV_DeviceGetFeatures(DriverHandle,
        (LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        DRV_DeviceClose((LONG far *)&DriverHandle);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
  
    }
    lpAIGetConfig.size = sizeof(LPDEVCONFIG_AI);
    lpAIGetConfig.buffer = &lpDevConfig_AI;
    ErrCde = DRV_AIGetConfig(DriverHandle, &lpAIGetConfig);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        DRV_DeviceClose(&DriverHandle);
        butStart->Enabled = false;
        return;

    }
    // initialize Input Range List Combobox with device features
    if (DevFeatures.usNumGain != 0)
    {

        for (i = 0; i < DevFeatures.usNumGain; i ++)
           cmbGain->Items->Add((LPSTR)DevFeatures.glGainList[i].szGainStr);

        cmbGain->ItemIndex = 0;
    }

    gwMaxLogChanNum = GetMaxLogChanNum(&lpDevConfig_AI,&DevFeatures);
    // Initialize Start Channel
    cmbChannel->Clear();
    for (i = 0 ; i < DevFeatures.usMaxAISiglChl ; i++)
    {
        itoa((int)i, szBuffer, 10);
        cmbChannel->Items->Add((LPSTR)szBuffer);
        cmbChannel->ItemIndex = 0;

    }
    char buff[20];
    sprintf(buff,"%d", 1);
    editScanNum->Text = buff ;

    // close device
    DRV_DeviceClose((LONG far *)&DriverHandle);

    // li.zhe add
    butStart->Enabled = true;
    // end li.zhe

}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::butStartClick(TObject *Sender)
{

     gwTrigVol = editTrigVolt->Text.ToDouble();
     gwDelayCnt = editDelayCount->Text.ToInt();
     gdwPacerRate = editPacerRate->Text.ToInt();
     gulConvNum = editCount->Text.ToInt();
     gwStartChl = cmbChannel->ItemIndex;
     gwChlNum =  editScanNum->Text.ToInt();

     if(radInternal->Checked)
          gwExtTrig=0;
     else
          gwExtTrig=1;
     int temp;

     int logChan = PhyChanToLogChan(&lpDevConfig_AI,usStartChan);
     for (int i=0 ; i< min(gwChlNum,gwMaxLogChanNum) ; i++)
     {
       ReorderGainCode[i] =
         DevFeatures.glGainList[gwGain[(i + logChan) % gwMaxLogChanNum]].usGainCde;
     }

    ErrCde = DRV_DeviceOpen(DeviceList[gwDevice].dwDeviceNum,
             (LONG far *)&DriverHandle);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        exit(0);
    }


      // allocate memory
     if((hUserBuf=(USHORT far *)GlobalAlloc(GHND, sizeof(USHORT) * gulConvNum)) == 0)
     {
         lstrcpy((char *) szErrMsg, "Not enough memory for buffer");
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

     // allocate memory
     if((hDisplayBuf=(FLOAT far *)GlobalAlloc(GHND, sizeof(FLOAT) * gulConvNum)) == 0)
     {
         lstrcpy((char *) szErrMsg, "Not enough memory for buffer");
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

     ptFAITransfer.DataBuffer = (FLOAT far  *)GlobalLock(hDisplayBuf);

     // enable event
        ptEnableEvent.EventType = 0;
        if( chkBufferChange->Checked )
        {
            ptEnableEvent.EventType |= ADS_EVT_BUFCHANGE;
        }
        if( chkTerminate->Checked )
        {
            ptEnableEvent.EventType |= ADS_EVT_TERMINATED;
        }
        if( chkOverrun->Checked )
        {
            ptEnableEvent.EventType |= ADS_EVT_OVERRUN;
        }
        if( chkInterrupt->Checked )
        {
            ptEnableEvent.EventType |= ADS_EVT_INTERRUPT;
        }

        ptEnableEvent.Enabled = true;
        ptEnableEvent.Count   = (USHORT)(edtEventCount->Text.ToInt() * 2048);


     if ((ErrCde = DRV_EnableEvent(DriverHandle,
         (LPT_EnableEvent)&ptEnableEvent)) != 0)
     {
         DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);

         GlobalUnlock(hUserBuf);
         GlobalFree(hUserBuf);
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

      ptFAIDmaExStart.TrigSrc	    	= gwExtTrig;
      ptFAIDmaExStart.TrigMode		= gwTrigMode;
      ptFAIDmaExStart.ClockSrc		= gwClockSrc;
      ptFAIDmaExStart.TrigEdge		= gwTrigEdge;
      ptFAIDmaExStart.SRCType	      	= gwSRCType;
      ptFAIDmaExStart.CyclicMode     	= gwCyclicMode;
      ptFAIDmaExStart.TrigVol	     	= gwTrigVol;
      ptFAIDmaExStart.StartChan		= gwStartChl;
      ptFAIDmaExStart.NumChans		= min(gwChlNum,gwMaxLogChanNum);
      ptFAIDmaExStart.ulDelayCnt     	= gwDelayCnt;
      ptFAIDmaExStart.SampleRate     	= gdwPacerRate;
      ptFAIDmaExStart.GainList		= &ReorderGainCode[0];
      ptFAIDmaExStart.count	      	= gulConvNum;
      ptFAIDmaExStart.buffer0	      	= (USHORT *)hUserBuf;

      if ((ErrCde = DRV_FAIDmaExStart(DriverHandle,
          (LPT_FAIDmaExStart)&ptFAIDmaExStart)) != 0)
      {
          DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
          Application->MessageBox((LPCSTR)szErrMsg,"Driver Message",MB_OK);
          VirtualFree(
                       hUserBuf,
                       2 * gulConvNum,
                       MEM_DECOMMIT| MEM_RELEASE);
          DRV_DeviceClose((LONG far *)&DriverHandle);
          return ;
       }
    frmDisplay->Show();
    frmDisplay->lstBuffer->Hide();

     butStart->Enabled = False;
     butStop->Enabled  = True;
     butExit->Enabled  = False;

    butStop->Enabled = true;
    butStart->Enabled = false;
    lstDevice->Enabled = false;
    editScanNum->Enabled = false;
    cmbGain->Enabled = false;
    cmbChannel->Enabled = false;
    gulOverrunTime = editOverrunTime->Text.ToInt();


    bThreadloop = true;

    pThread = new MyThread(false);

}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::butStopClick(TObject *Sender)
{
  if ((ErrCde = DRV_FAITerminate(DriverHandle)) != 0)
   {
      DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
      Application->MessageBox((LPCSTR)szErrMsg,"Thread Message",MB_OK);
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::OnDestroy(TObject *Sender)
{
      if( butStop->Enabled )
        butStopClick(Sender);

}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::butExitClick(TObject *Sender)
{
   Close();

}
//---------------------------------------------------------------------------
__fastcall MyThread::MyThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall MyThread::Execute()
{
        //---- Place thread code here ----

         frmMain->CheckEvent();


}

//---------------------------------------------------------------------------
void TfrmMain::CheckEvent()
{
    //TODO: Add your source code here
    USHORT usEventType;
    char szData[32];

⌨️ 快捷键说明

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