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

📄 mainform.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop


#include "MainForm.h"
#include "DisplayForm.h"
#include "GainListForm.h"
#include "stdio.h"
#include "..\..\..\include\driver.h"


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

#define     MAX_DEVICES     100

DEVFEATURES     DevFeatures;    // structure for device features
static      PT_MAIConfig     ptMAIConfig;     // structure for AIConfig table
PT_DeviceGetFeatures  ptDevFeatures;
PT_AIGetConfig  lpAIGetConfig;
DEVCONFIG_AI   lpDevConfig_AI ;
PT_MAIVoltageIn  ptMAIVoltageIn;  // structure for AIVoltageIn table
FLOAT       fVoltageArray[64];

USHORT      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
USHORT      usGainCode[64];
USHORT      usGainIndx[64];
USHORT      usStartChan, usChanNum;

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

//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
    int         nOutEntries;



    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);        
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::butStartClick(TObject *Sender)
{
    USHORT   iMaxChannelNum;
    USHORT   iMaxSingleChannel;
    USHORT   iMaxDiffChannel;

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

    usStartChan = cmbChannel->ItemIndex;
    usChanNum =  editScanNum->Text.ToInt();

    //get the max channel number
    iMaxSingleChannel = DevFeatures.usMaxAISiglChl;
    iMaxDiffChannel = DevFeatures.usMaxAIDiffChl;
    if(lpDevConfig_AI.ulChanConfig == 1)
       iMaxChannelNum = iMaxDiffChannel;
    else if ( iMaxSingleChannel > iMaxDiffChannel )
    {
       iMaxChannelNum = iMaxSingleChannel;
    }
    else
    {
       iMaxChannelNum = iMaxDiffChannel;
    }
    if ( lpDevConfig_AI.ulChanConfig == 1)
    {
        if ( (usChanNum <0)||(usChanNum > DevFeatures.usMaxAIDiffChl -usStartChan))
        {
            Application->MessageBox("Invalidate logical channel number!",
                                 "Error!",
                                 MB_OK);
            return ;
        }
    }
    if ( (usChanNum <0)||(usChanNum > iMaxChannelNum-usStartChan))
    {
         Application->MessageBox("Invalidate logical channel number!",
                                 "Error!",
                                 MB_OK);
         return ;
    }

    int phiChan = usStartChan;

    for (int i=0 ; i< usChanNum ; i++, phiChan++)
    {
       usGainCode[i] =
         DevFeatures.glGainList[usGainIndx[PhyChanToLogChan(&lpDevConfig_AI,phiChan)]].usGainCde;

       if ( lpDevConfig_AI.ulChanConfig != 1)
       {
           if(phiChan < 32)
           {
                if( ( lpDevConfig_AI.ulChanConfig & (1<< phiChan) )
                 && (phiChan % 2 == 0) )
                phiChan++;
           }
           else
           {
                if( ( lpDevConfig_AI.ulChanConfigEx[0] & (1<< (phiChan-32)) )
                 && (phiChan % 2 == 0) )
                 phiChan++;
           }
       }


    }
    ptMAIConfig.GainArray = (USHORT far *) &usGainCode[0];
    ptMAIConfig.NumChan   = usChanNum;
    ptMAIConfig.StartChan = usStartChan;

   if ((ErrCde = DRV_MAIConfig(DriverHandle,
        (LPT_MAIConfig)&ptMAIConfig)) != 0)
   {
       DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
       Application->MessageBox((LPCSTR)szErrMsg,"Driver Message",MB_OK);
       DRV_DeviceClose((LONG far *)&DriverHandle);
       return;
   }
    Canvas->FillRect(TRect(10,200,420,300));
    frmDisplay->ScanTimer->Enabled = true;
    ptMAIVoltageIn.StartChan = usStartChan;
    ptMAIVoltageIn.NumChan = usChanNum;
    ptMAIVoltageIn.GainArray = (USHORT far  *)&usGainCode[0];
    ptMAIVoltageIn.TrigMode = 0;                  // internal trigger
    ptMAIVoltageIn.VoltageArray = (FLOAT far *)fVoltageArray;
    frmDisplay->ShowModal();


 

}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::lstDeviceClick(TObject *Sender)
{

    USHORT i;
    USHORT iMaxChannelNum;
    USHORT iMaxSingleChannel;
    USHORT iMaxDiffChannel;

    cmbChannel->Clear();
     cmbGain->Clear();
    gwDevice = (USHORT)lstDevice->ItemIndex;

    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);
        return;

    }

    // 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);
        return;

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

    }
    //int tempNum=0;
    //tempNum = DevFeatures.usMaxAISiglChl;

    //get the max channel number
    iMaxSingleChannel = DevFeatures.usMaxAISiglChl;
    iMaxDiffChannel = DevFeatures.usMaxAIDiffChl;
    if(lpDevConfig_AI.ulChanConfig == 1)
       iMaxChannelNum = iMaxDiffChannel;
    else if ( iMaxSingleChannel > iMaxDiffChannel )
    {
       iMaxChannelNum = iMaxSingleChannel;
    }
    else
    {
       iMaxChannelNum = iMaxDiffChannel;
    }

   // Here add analog input channel.
   if ( iMaxChannelNum > 0)
   {
       for(i=0; i< iMaxChannelNum; i++)
       {
          cmbChannel->Items->Add(IntToStr(i));
          cmbChannel->ItemIndex = 0;
          butStart->Enabled = true;
        }
        cmbChannel->ItemIndex = 0;
    }
    // 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;
    }

     butStart->Enabled = true;


    // close device
    DRV_DeviceClose((LONG far *)&DriverHandle);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::butExitClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::radOverallClick(TObject *Sender)
{
   cmbGain->Enabled = true;
   butGainList->Enabled = false;
}
//---------------------------------------------------------------------------

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

    int temp=0;
    char tempstr[80];
    usStartChan = cmbChannel->ItemIndex;
    usChanNum =  editScanNum->Text.ToInt();
    for(int i=usStartChan; i< DevFeatures.usMaxAISiglChl; i++)
    {
       // If the channel number between 0 and 31
       if (i < 32)
       {
          if( ( lpDevConfig_AI.ulChanConfig  & (1 << i) )
             && (i % 2 == 0) )
             i++;
       }
       else
       {  // If the channel number between 32 and 63
          if( ( lpDevConfig_AI.ulChanConfigEx[0] & (1 << (i-32)) )
             && (i % 2 == 0) )
             i++;
       }

        temp++;
  }

    if( usChanNum > temp)
    {
       sprintf(tempstr,"The Maximum Scan Channnel is %d.",temp);
       Application->MessageBox(tempstr,"Channel Error", MB_OK);
       return;
     }

    frmGainList->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::radGainListClick(TObject *Sender)
{
   cmbGain->Enabled = false;
   butGainList->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::cmbGainChange(TObject *Sender)
{
    for (int i=0 ; i< 64 ; i++)
      usGainIndx[i] =cmbGain->ItemIndex;

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

⌨️ 快捷键说明

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