init.cpp

来自「16 relay output channels and 16 isolated」· C++ 代码 · 共 634 行 · 第 1/2 页

CPP
634
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Init.h"
#include "DataShow.h"
#include "Convert.h"
#include "GainListForm.h"
#include "stdio.h"
#include "..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFrmInit *FrmInit;

const   MaxEntries = 255;
DEVLIST                 DeviceList[MaxEntries + 1];
DEVFEATURES             DevFeatures;
PT_DeviceGetFeatures    ptDevGetFeatures;
DEVCONFIG_AI            Devconfig_AI;
PT_AIGetConfig          ptAIGetConfig;
PT_EnableEvent          ptEnableEvent;     // Enable event
PT_CheckEvent           ptCheckEvent;      // Check event
PT_FAIIntScanStart      ptFAIStart;
PT_FAITransfer          ptFAITransfer;     // FAITransfer table

int gwDevice = 0;      // The selected device in DeviceList[]

USHORT      usStartChan;   // Start channel(physical)
USHORT      usChanNum;     // The count of channels(logical)

long    DeviceHandle;
long    ErrCde;
char    szErrMsg[80];
USHORT      usGainCode[64];
USHORT      usGainIndx[64];     ///Logical Channel Gain Code Index
int     giMaxLogChanNum;
int     giEvtFlag=1;                          // event enable(0)
int     giFifoEnable=0;                       // Fifo
int     giFifoSize;                         // Fifo Size
HGLOBAL hBuf,hUserBuf;
ULONG   gulConvNum;
ULONG   gdwPacerRate;
USHORT  usEventType=0;
USHORT  guDataType = 1;           // display type : FLOAT(1)
USHORT  gwOverrun;
ULONG   ulIntCounter     = 0;  // counter by adInterruptEvent
ULONG   ulBfChgCounter   = 0;
ULONG   ulOverrunCounter = 0;
bool gbThreadFlag = false;
// li.zhe add
USHORT  gwExtTrig = 0;  // 0 = internal trigger

long ChkErr(long lErrCde)
{
    if (lErrCde != 0)
    {
        DRV_GetErrorMessage(lErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return 1;
    }
    else
        return 0;
}


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

void TFrmInit::GainCodeFilling()
{
     for (int i=0 ; i< 64 ; i++)
      usGainIndx[i] =cmbOverallGain->ItemIndex;

}

//---------------------------------------------------------------------------
__fastcall TFrmInit::TFrmInit(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFrmInit::FormCreate(TObject *Sender)
{
    int i;
    short gnNumOfDevices;
    int MaxEntries;
    short nOutEntries;

    MaxEntries = 100;

    // Add type of PC Laboratory Card
    ErrCde = DRV_DeviceGetList(&DeviceList[0], MaxEntries, &nOutEntries);
    if (ChkErr(ErrCde))
        return;

    // Return the number of devices which you install in the system using
    // Device Installation
    ErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
    if (ChkErr(ErrCde))
        return;

    for (i = 0; i < gnNumOfDevices; i++)
    {
        lstDevice->Items->Add(DeviceList[i].szDeviceName);
    }

    giEvtFlag = 1;                  // event enable(0)
    giFifoEnable = 0;               // Fifo disable(0)
    giFifoSize = FIFO_SIZE;

    lstDevice->ItemIndex = 0;
    lstDeviceClick(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TFrmInit::lstDeviceClick(TObject *Sender)
{
    int i;
    int tempNum;
    long dwDeviceNum;

    //
    cmbOverallGain->Enabled = true;
    butConvert->Enabled = false;
        
    // Clear gain code selection items
    cmbOverallGain->Clear();

    gwDevice = lstDevice->ItemIndex;

    // Open selected device for getting more informations
    dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;
    ErrCde = DRV_DeviceOpen(dwDeviceNum, &DeviceHandle);
    if (ChkErr(ErrCde))
        return;

    // Device's informations is stored as Features and configurations
    ptDevGetFeatures.buffer = &DevFeatures;
    ErrCde = DRV_DeviceGetFeatures(DeviceHandle, &ptDevGetFeatures);
    if (ChkErr(ErrCde))
    {
        DRV_DeviceClose(&DeviceHandle);
        return;
    }

    // Add Gain code selection list's items
    tempNum = DevFeatures.usNumGain;
    if (tempNum > 0)
    {
        for (i = 0; i < tempNum; i++)
        {
            cmbOverallGain->Items->Add(DevFeatures.glGainList[i].szGainStr);
        }
    }
    cmbOverallGain->ItemIndex = 0;

    // Reset Gain code setting
    for (i = 0; i <64; i++)
        usGainIndx[i] = 0;
    GainCodeFilling();
               
    // Add analog input start channel items
    cmbStartChl->Clear();


    ptAIGetConfig.buffer = &Devconfig_AI;
    ErrCde = DRV_AIGetConfig(DeviceHandle, &ptAIGetConfig);
    if (ChkErr(ErrCde))
    {
        DRV_DeviceClose(&DeviceHandle);
        return;
    }
    giMaxLogChanNum = GetMaxLogChanNum(&Devconfig_AI,&DevFeatures);
    
    if(Devconfig_AI.ulChanConfig == 1)
         tempNum = DevFeatures.usMaxAIDiffChl;
    else if(DevFeatures.usMaxAIDiffChl > DevFeatures.usMaxAISiglChl)
         tempNum = DevFeatures.usMaxAIDiffChl;
    else
         tempNum = DevFeatures.usMaxAISiglChl;

     if (tempNum > 0)
    {
        for (i = 0; i < tempNum; i++)
        {
            cmbStartChl->Items->Add(IntToStr(i).c_str());
        }

        cmbStartChl->ItemIndex = 0;
        cmbStartChlClick(Sender);
    }

    // Close device
    DRV_DeviceClose(&DeviceHandle);

    radOverall->Enabled = true;
    butConvert->Enabled = true;
}
//---------------------------------------------------------------------------



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

void __fastcall TFrmInit::cmbStartChlClick(TObject *Sender)
{
    int tempNum;
    int i;

    // Add analog input channel count item
    cmbChlCount->Clear();
    for (i = 1; i <= giMaxLogChanNum; i++)
        cmbChlCount->Items->Add(IntToStr(i).c_str());

    // Default selection
    cmbChlCount->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFrmInit::butConvertClick(TObject *Sender)
{
    PT_EnableEvent ptEnableEvent;
    int i, j;
    ulIntCounter     = 0;  // counter by adInterruptEvent
    ulBfChgCounter   = 0;
    ulOverrunCounter = 0;
    gulConvNum =  editConv->Text.ToInt();
    gdwPacerRate = editPacerRate->Text.ToInt();
    guDataType =  (USHORT)chkFloatData->Checked;
    // 1. Open device
    ErrCde = DRV_DeviceOpen(DeviceList[lstDevice->ItemIndex].dwDeviceNum, &DeviceHandle);
    if (ChkErr(ErrCde))
        return;

    ptAIGetConfig.size = sizeof(LPDEVCONFIG_AI);
    ptAIGetConfig.buffer = &Devconfig_AI;
    ErrCde = DRV_AIGetConfig(DeviceHandle, &ptAIGetConfig);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
    }

    // 2. Set gain list
    usStartChan=cmbStartChl->Text.ToInt();
    usChanNum= cmbChlCount->Text.ToInt();

    int logChan = PhyChanToLogChan(&Devconfig_AI,usStartChan);
    for (int i=0 ; i< usChanNum ; i++)
    {
       usGainCode[i] =
         DevFeatures.glGainList[usGainIndx[(i + logChan) % giMaxLogChanNum]].usGainCde;
    }

    ptEnableEvent.EventType = ADS_EVT_INTERRUPT |
                              ADS_EVT_BUFCHANGE |
                              ADS_EVT_OVERRUN   |
                              ADS_EVT_TERMINATED;
    ptEnableEvent.Enabled = giEvtFlag;
    giFifoSize = editFifo->Text.ToInt();
    if (chkFifo->Checked && giFifoSize > 0)
        ptEnableEvent.Count = giFifoSize;
    else
        ptEnableEvent.Count = 1;
    ErrCde = DRV_EnableEvent(DeviceHandle, &ptEnableEvent);
    if (ChkErr(ErrCde))
        return;

⌨️ 快捷键说明

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