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

📄 mainform.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "stdio.h"
#include "..\..\..\include\driver.h"
#include "..\..\..\include\paras.h"
#include "MainForm.h"

#include "DisForm.h"
const MAXAICHL = 8; // Only for USB-4718, for other devices, please refer to
                    // the hardware manual and software manual
const MAX_DEVICES = 255;
DEVLIST DeviceList[MAX_DEVICES + 1];
DEVLIST SubDeviceList[MAX_DEVICES + 1];
int gnNumOfDevices, gnNumOfSubdevices;
int AICtrMode;
long ErrCde;
SHORT nOutEntries;

bool bRun;
static ULONG dwDeviceNum;              //Advantech Device Number in your system
LONG   DeviceHandle;                  //Driver handle
static char szDescription[80];          //description for Select Device
PT_DeviceGetFeatures   ptDevGetFeatures ;
DEVFEATURES     lpDevFeatures;    // structure for device features
PT_TCMuxRead    ptTCMuxRead;
DEVCONFIG_AI  lpDevConfig_AI;
PT_AIGetConfig  lpAIGetConfig;
unsigned    uTypeValue[7] = {JTC, KTC, STC, TTC, BTC, RTC, ETC};

BOOL BurnOutSupport;
LONG BurnOutProperty[MAXAICHL];





//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
long ChkErr(long lErrCde)
{
    char szErrMsg[80];
    if (lErrCde != 0)
    {
        DRV_GetErrorMessage(lErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return 1;
    }
    else
        return 0;
}
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::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);
    }


  lstDevice->ItemIndex = 0;
  lstThermocouple->Items->Add("J type");
  lstThermocouple->Items->Add("K type");
  lstThermocouple->Items->Add("S type");
  lstThermocouple->Items->Add("T type");
  lstThermocouple->Items->Add("B type");
  lstThermocouple->Items->Add("R type");
  lstThermocouple->Items->Add("E type");
  lstThermocouple->ItemIndex = 0;

   lstModule->Enabled = False;
   labModule->Enabled = False;
   labChannel->Enabled = False;
   lstChannel->Enabled = False;
  lstVoltageRange->Enabled = False;
  labVoltageRange->Enabled = False;
  cmdRun->Enabled = False;
  lstDeviceClick(NULL);
}
//---------------------------------------------------------------------------

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

  int tempNum;
  int i;
  String tempStr;
  char   buff[40];
  bool TestRes;
  ULONG Length;

  lstModule->Items->Clear();
  lstChannel->Items->Clear();
  lstVoltageRange->Items->Clear();
  lstExpChl->Clear();

 tempStr=lstDevice->Items[0][lstDevice->ItemIndex]  ;

  if (tempStr.Pos("DEMO") > 0 )
     TestRes = true ;
  else
     TestRes = false;

  //{ Avoid to open Advantech Demo Card }
  if (TestRes)
  {
    labModule->Enabled = False;
    lstModule->Enabled = False;
    labChannel->Enabled = False;
    lstChannel->Enabled = False;
    labVoltageRange->Enabled = False;
    lstVoltageRange->Enabled = False;
    lstChannel->Items->Add( "No Use" );
    cmdRun->Enabled = False;
    return;
  }


   // { Check if there is any device attached on this COM or CAN }
    gnNumOfSubdevices = DeviceList[lstDevice->ItemIndex].nNumOfSubdevices;
    if (gnNumOfSubdevices > MaxDev)
       gnNumOfSubdevices = MaxDev;

    dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;

   // { COM and CAN bus }
    if (gnNumOfSubdevices != 0)
    {
       ErrCde = DRV_DeviceGetSubList(dwDeviceNum, SubDeviceList, gnNumOfSubdevices, &nOutEntries);
       if (ChkErr(ErrCde))
         return;
       for( i = 0; i < gnNumOfSubdevices; i++ )
           lstModule->Items->Add(SubDeviceList[i].szDeviceName);
       lstModule->Enabled = True;
       labModule->Enabled = True;
       labChannel->Enabled = True;
       lstChannel->Enabled = True;
       lstChannel->ItemIndex = 0;
       labVoltageRange->Enabled = True;
       lstVoltageRange->Enabled = True;
       cmdRun->Enabled = True;
    }

  dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;
  ErrCde = DRV_DeviceOpen(dwDeviceNum, &DeviceHandle);
    if (ChkErr(ErrCde))
        return;

    ptDevGetFeatures.buffer = &lpDevFeatures;
    ErrCde = DRV_DeviceGetFeatures(DeviceHandle, &ptDevGetFeatures);
    if (ChkErr(ErrCde))
        return;

    // Add analog input channel item
    lpAIGetConfig.buffer = &lpDevConfig_AI;
    ErrCde = DRV_AIGetConfig(DeviceHandle, &lpAIGetConfig);
     if (ChkErr(ErrCde))
        return;

    tempNum = lpDevFeatures.usMaxAISiglChl > lpDevFeatures.usMaxAIDiffChl ?
              lpDevFeatures.usMaxAISiglChl : lpDevFeatures.usMaxAIDiffChl;
     if (tempNum > 0)
     {
         for(i = 0; i<tempNum;i++ )
         {
              tempStr = "Chan# " + IntToStr(i);
              lstChannel->Items->Add(tempStr);
         }
     }
     labChannel->Enabled = True;
     lstChannel->Enabled = True;
     lstChannel->ItemIndex = 0;
     lstChannelClick(NULL);

     if (lpDevFeatures.usNumGain > 0)
     {
         for( i = 0; i < lpDevFeatures.usNumGain; i++ )
         {
             lstVoltageRange->Items->Add(lpDevFeatures.glGainList[i].szGainStr);
         }
         labVoltageRange->Enabled = True;
         lstVoltageRange->Enabled = True;
         lstVoltageRange->ItemIndex = 0;
     }

     //Burn test supported?
     cmbBTEnable->Clear();
     Length = sizeof(BurnOutProperty);
     ErrCde = DRV_DeviceGetProperty(DeviceHandle, CFG_BURNTEST, BurnOutProperty, &Length);
     if(ErrCde == InvalidInputParam)
     {
          ErrCde = DRV_DeviceGetProperty(DeviceHandle, CFG_BURNTEST, BurnOutProperty, &Length);
     }
     if(ErrCde == FunctionNotSupported)
     {
        cmbBTEnable->Enabled = 0;
        BurnOutSupport = 0;
     }
     else if(ErrCde == SUCCESS)
     {
        cmbBTEnable->Items->Add("Disable");
        cmbBTEnable->Items->Add("888888");
        cmbBTEnable->Items->Add("-888888");
        cmbBTEnable->Items->Add("Maximum value");
        cmbBTEnable->Items->Add("Minimum value");

        cmbBTEnable->Enabled = 1;
        cmbBTEnable->ItemIndex = BurnOutProperty[lstChannel->ItemIndex];
        BurnOutSupport = True;
     }
     else
     {
       if (ChkErr(ErrCde))
         return;
     }



     cmdRun->Enabled = True;


     DRV_DeviceClose(&DeviceHandle);


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


void __fastcall TfrmMain::cmdRunClick(TObject *Sender)
{
 int tempNum;
 ULONG Property;
 ULONG Length; 


  ptTCMuxRead.DasChan = lstChannel->ItemIndex;



  //{ No gain code for ADAM series }
  if( gnNumOfSubdevices == 0 )
  {
       DWORD BoardID = lpDevConfig_AI.Daughter[lstChannel->ItemIndex].dwBoardID;
       if( BoardID >0  && BoardID != BD_PCLD8710 )
       {
          tempNum = lstChannel->ItemIndex;
          ptTCMuxRead.DasGain = lpDevConfig_AI.Daughter[tempNum].fGain;
       //   { the gain for expand card is set in device installation }
          ptTCMuxRead.ExpChan = lstExpChl->ItemIndex;
       }
       else
       {
          tempNum = lstVoltageRange->ItemIndex;
          ptTCMuxRead.DasGain = lpDevFeatures.glGainList[tempNum].usGainCde;
          ptTCMuxRead.ExpChan = 0 ;
       }
  }

  ptTCMuxRead.TCType = uTypeValue[lstThermocouple->ItemIndex];
    ErrCde = DRV_DeviceOpen(dwDeviceNum, &DeviceHandle);
    if (ChkErr(ErrCde))
        return;

    if(BurnOutSupport)
  {
     BurnOutProperty[ptTCMuxRead.DasChan] = cmbBTEnable->ItemIndex;
     Length = sizeof(BurnOutProperty);
     ErrCde = DRV_DeviceSetProperty(DeviceHandle, CFG_BURNTEST, BurnOutProperty, Length);
     if (ChkErr(ErrCde))
        return;
  }
  frmDisplay->Show();
  frmDisplay->cmdRead->SetFocus();
  frmDisplay->tmrRead->Enabled = True;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::cmdExitClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::lstChannelClick(TObject *Sender)
{
    String tempStr;
      lstExpChl->Clear();
    lstExpChl->Enabled = false;
    if (lpDevConfig_AI.Daughter[lstChannel->ItemIndex].dwBoardID > 0)
    {
         for(int i = 0; i<lpDevConfig_AI.Daughter[lstChannel->ItemIndex].usNum;i++)
         {
                tempStr = "Chan#" + IntToStr(i);
                lstExpChl->Items->Add(tempStr);
          }
         lstExpChl->ItemIndex = 0;
         lstExpChl->Enabled = true ;

   }
   if (BurnOutSupport == True)
   {
      cmbBTEnable->ItemIndex = BurnOutProperty[lstChannel->ItemIndex];
   }
}
//---------------------------------------------------------------------------



⌨️ 快捷键说明

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