form.cpp

来自「16 relay output channels and 16 isolated」· C++ 代码 · 共 362 行

CPP
362
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Form.h"
#include "ReadForm.h"
#include "..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;


const MaxEntries = 255;
long DeviceHandle;
DEVLIST DeviceList[MaxEntries + 1];
DEVLIST SubDeviceList[MaxEntries + 1];
int gnNumOfSubdevices;
int AICtrMode;
long ErrCde;
char szErrMsg[80];
bool bRun;
static ULONG lDeviceNum;                    //Advantech Device Number in your system
LONG   lDriverHandle;                  //Driver handle
static char szDescription[80];              //description for Select Device

DEVFEATURES     DevFeatures;    // structure for device features
DEVCONFIG_AI    DevCfg;         // structure for DEVCONFIG_AI table
PT_AIConfig     ptAIConfig;     // structure for AIConfig table
PT_AIVoltageIn  ptAIVoltageIn;  // structure for AIVoltageIn table
PT_AIVoltageInExp  ptAIVoltageInExp;  // structure for AIVoltageInExp table
PT_DeviceGetFeatures  ptDevGetFeatures;  // structure for DeviceGetFeatures
PT_AIGetConfig  ptAIGetConfig;  // structure for AIGetConfig table
USHORT      gwChannel = 0;                      // input channel
USHORT      usExpChan = 0;                      // expansion channel
USHORT      usBoardID = 0;                      // Board ID
USHORT      usMaxChannel = 0;                   // max channel
USHORT gwGain  ;

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------



void __fastcall TMainForm::BtnSelectDeivceClick(TObject *Sender)
{

    USHORT usMaxChannel;
    AnsiString temp;
    char   szExpName[15];                      // expansion name
    int i;
    DRV_SelectDevice(Handle, True, (ULONG*)&lDeviceNum, szDescription);
    labDeviceName->Caption = AnsiString(szDescription);
     if (gnNumOfSubdevices == 0)
        ErrCde = DRV_DeviceOpen(lDeviceNum,
                (LONG far *)&lDriverHandle);
    else
        ErrCde = DRV_DeviceOpen(lDeviceNum,
                (LONG far *)&lDriverHandle);

    if (ErrCde != SUCCESS)
    {
        strcpy(szErrMsg,"Device open error !");
        Application->MessageBox((LPCSTR)szErrMsg,"Device Open",MB_OK);
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return ;
    }


    //
    // get number of counter channels
    //

    ptDevGetFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
    ptDevGetFeatures.size = sizeof(DEVFEATURES);
    if ((ErrCde = DRV_DeviceGetFeatures(lDriverHandle,
        (LPT_DeviceGetFeatures)&ptDevGetFeatures)) != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((LPCSTR)szErrMsg,"Driver Message",MB_OK);
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return ;
    }

     CmbChannel->Clear();
      if ((DevFeatures.usMaxAIDiffChl   == 0) &&
            (DevFeatures.usMaxAISiglChl == 0))
      {

      }
      else
      {

         ptAIGetConfig.buffer = (LPDEVCONFIG_AI)&DevCfg;
         ptAIGetConfig.size = sizeof(DEVCONFIG_AI);

            // call AIGetConfig
        if ((ErrCde = DRV_AIGetConfig(lDriverHandle,
            (LPT_AIGetConfig)&ptAIGetConfig)) != SUCCESS)
        {
            DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
            Application->MessageBox((LPCSTR)szErrMsg,"Driver Message",MB_OK);
            DRV_DeviceClose((LONG far *)&lDriverHandle);
            return ;
        }

       usMaxChannel = DevFeatures.usMaxAISiglChl;

        CmbChannel->Clear();
        // setting dialog box for channel setting
        // fill in the channel listbox with the available channel
        // in the AI subsection (start with channel 0)
        for (int i=0; i< (int)usMaxChannel; ++i)
        {
            temp.sprintf("%d", i);
            if (i == (int)DevCfg.usCjcChannel) // CJC channel
            {
                temp+="(CJC Channel)";
            }
            else if (DevCfg.Daughter[i&0xf].dwBoardID!=0L)// exp. board exist
            {
                temp+="(";
                DRV_BoardTypeMapBoardName(DevCfg.Daughter[i&0xf].dwBoardID,
                                          (LPSTR)szExpName);
                temp+=szExpName;
                temp+=")";
            }
            CmbChannel->Items->Add(temp);

         }
         CmbChannel->ItemIndex =0;
        // initialize input channel
        // if the selected channel is associated with an expansion board,
        // fill in the Exp. channel listbox with the available expansion board
        // channel.
        if (DevCfg.usNumExpChan > 0 && DevCfg.Daughter[gwChannel&0xf].dwBoardID
            && (int)gwChannel != (int)DevCfg.usCjcChannel &&
            DevCfg.Daughter[gwChannel&0xf].dwBoardID != (DWORD)BD_PCLD8115)
       {
           CmbExpChan->Enabled=true;
           CmbExpChan->Clear();
            for (i=0; i<(int)DevCfg.Daughter[gwChannel&0xf].usNum; ++i)
	    {
	     	temp.sprintf("%d", i);
                CmbExpChan->Items->Add(temp);
	    }
            CmbExpChan->ItemIndex =0;

	}
	else
       	{
            CmbExpChan->Enabled=false;
            usExpChan = 0;
	 }

        if (DevCfg.Daughter[gwChannel&0xf].dwBoardID == (DWORD)BD_PCLD788)
        {
           CmbBoardID->Enabled=true;
           CmbBoardID->Clear();
            for (i=0; i<(int)DevCfg.Daughter[gwChannel&0xf].usCards; ++i)
            {
                temp.sprintf("%d", i);
                CmbBoardID->Items->Add(temp);
            }
            CmbBoardID->ItemIndex =0;
        }
        else
        {
            usBoardID = 0;
            CmbBoardID->Enabled=false;
        }

        // third: initialize Input Range List Combobox with device features
        if ((DevFeatures.usNumGain != 0) &&
            (DevCfg.Daughter[gwChannel&0xf].dwBoardID == 0) &&
            (DevCfg.usCjcChannel != gwChannel))
        {
             CmbRange->Enabled=true;
             CmbRange->Clear();
             for (i = 0; i < (int)DevFeatures.usNumGain; i ++)
                CmbRange->Items->Add(DevFeatures.glGainList[i].szGainStr);
             CmbRange->ItemIndex =0;
        }
        else
        {
            gwGain = 0;
            CmbRange->Enabled=false;
       }
    }


        // fourth: close device

        DRV_DeviceClose((LONG far *)&lDriverHandle);

    DRV_DeviceClose((LONG far *)&lDriverHandle);    
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::BtnStartClick(TObject *Sender)
{

    gwChannel=CmbChannel->ItemIndex;
    gwGain=CmbRange->ItemIndex;
    if( CmbRange->ItemIndex < 0 )
       gwGain = 0;

    usExpChan = CmbExpChan->ItemIndex;                      // expansion channel
    usBoardID = CmbBoardID->ItemIndex;                      // Board ID



     if (gnNumOfSubdevices == 0)
        ErrCde = DRV_DeviceOpen(lDeviceNum,
                (LONG far *)&lDriverHandle);
    else
        ErrCde = DRV_DeviceOpen(lDeviceNum,
                (LONG far *)&lDriverHandle);

    if (ErrCde != SUCCESS)
    {
        strcpy(szErrMsg,"Device open error !");
        Application->MessageBox((LPCSTR)szErrMsg,"Device Open",MB_OK);
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return ;
    }
    //
    // get number of  channels
    //

    ptDevGetFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
    ptDevGetFeatures.size = sizeof(DEVFEATURES);
    if ((ErrCde = DRV_DeviceGetFeatures(lDriverHandle,
        (LPT_DeviceGetFeatures)&ptDevGetFeatures)) != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((LPCSTR)szErrMsg,"Driver Message",MB_OK);
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return ;
    }

     //
    // configures the gain for the specifed analog input channel
    //

    if(DevCfg.Daughter[gwChannel&0xf].dwBoardID == 0)
    {
       ptAIConfig.DasGain = DevFeatures.glGainList[gwGain].usGainCde;
       ptAIConfig.DasChan = gwChannel;

       if ((ErrCde = DRV_AIConfig(lDriverHandle,
            (LPT_AIConfig)&ptAIConfig)) != 0)
       {
           DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
           Application->MessageBox((LPCSTR)szErrMsg,"Driver Message",MB_OK);
           DRV_DeviceClose((LONG far *)&lDriverHandle);
           return ;
       }
    }
    //

    MainForm->Hide();
    frmRead->Show();
    frmRead->Timer1->Enabled = true;
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::FormCreate(TObject *Sender)
{

  BtnSelectDeivceClick(Sender);
 

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

void __fastcall TMainForm::BtnExitClick(TObject *Sender)
{
   Application->Terminate();

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

void __fastcall TMainForm::CmbChannelChange(TObject *Sender)
{
      gwChannel = 0;
      gwGain = 0;
      usExpChan = 0;
      usBoardID = 0;
      int i;
      AnsiString temp;

      gwChannel = (WORD)CmbChannel->ItemIndex;

      // if the selected channel is associated with an expansion board,
      // fill in the Exp. channel listbox with the available expansion board
      // channel.
        if (DevCfg.usNumExpChan > 0 && DevCfg.Daughter[gwChannel&0xf].dwBoardID
            && (int)gwChannel != (int)DevCfg.usCjcChannel &&
            DevCfg.Daughter[gwChannel&0xf].dwBoardID != (DWORD)BD_PCLD8115)
       {
           CmbExpChan->Enabled=true;
           CmbExpChan->Clear();
            for ( i=0; i<(int)DevCfg.Daughter[gwChannel&0xf].usNum; ++i)
	    {
	     	temp.sprintf("%d", i);
                CmbExpChan->Items->Add(temp);
	    }
            CmbExpChan->ItemIndex =0;

	}
	else
       	{
            CmbExpChan->Enabled=false;
            usExpChan = 0;
	 }

        if (DevCfg.Daughter[gwChannel&0xf].dwBoardID == (DWORD)BD_PCLD788)
        {
           CmbBoardID->Enabled=true;
           CmbBoardID->Clear();
            for (i=0; i<(int)DevCfg.Daughter[gwChannel&0xf].usCards; ++i)
            {
                temp.sprintf("%d", i);
                CmbBoardID->Items->Add(temp);
            }
            CmbBoardID->ItemIndex =0;
        }
        else
        {
            usBoardID = 0;
            CmbBoardID->Enabled=false;
        }

        // third: initialize Input Range List Combobox with device features
        if ((DevFeatures.usNumGain != 0) &&
            (DevCfg.Daughter[gwChannel&0xf].dwBoardID == 0) &&
            (DevCfg.usCjcChannel != gwChannel))
        {
             CmbRange->Enabled=true;
             CmbRange->Clear();
             for (i = 0; i < (int)DevFeatures.usNumGain; i ++)
                CmbRange->Items->Add(DevFeatures.glGainList[i].szGainStr);
             CmbRange->ItemIndex =0;
        }
        else
        {
            gwGain = 0;
            CmbRange->Enabled=false;
       }
 
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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