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

📄 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 "..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
static LONG lDriverHandle;                     //Driver handle
static char        szErrMsg[80];               // Use for MESSAGEBOX function
static LRESULT     ErrCde;                     // Return error code
USHORT      gwChannel = 0;                      // input channel

static ULONG lDeviceNum;                    //Advantech Device Number in your system
static char szDescription[80];              //description for Select Device
static      DEVFEATURES     DevFeatures;    // structure for device features
static      PT_DeviceGetFeatures  ptDevGetFeatures;
static      PT_PWMStartRead  ptPWMStartRead;
FLOAT       gfHiperiod = 0;              // read hiperiod data
FLOAT       gfLowperiod = 0;             // read lowperiod data
BOOL        bOpen = false;


//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSelectClick(TObject *Sender)
{
    //This function is for getting the device number
    if(1 == DRV_SelectDevice(Handle, True, (ULONG*)&lDeviceNum, szDescription)){
        Application->MessageBoxA("Can not Open Select Device Dialog", "Error");
    }
    labDeviceName->Caption = AnsiString(szDescription);

    //open the Device
     ErrCde = DRV_DeviceOpen(lDeviceNum, (LONG far *) &lDriverHandle);
     if (ErrCde != SUCCESS){
            strcpy(szErrMsg,"Device open error !");
            Application->MessageBoxA((LPCSTR)szErrMsg,"Device Open");
            return;
    }

    ptDevGetFeatures.buffer = (LPDEVFEATURES )&DevFeatures;
    ptDevGetFeatures.size   = sizeof(DEVFEATURES);
    ErrCde = DRV_DeviceGetFeatures(lDriverHandle, (PT_DeviceGetFeatures*)&ptDevGetFeatures);
    if (ErrCde != SUCCESS){
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBoxA((LPCSTR)szErrMsg,"Driver Message");
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return;
    }
    if (0 == DevFeatures.usMaxTimerChl){
        Application->MessageBoxA((LPCSTR)"No Counter Channel","Driver Message");
        DRV_DeviceClose((LONG far *)&lDriverHandle);
         return;
    }
    for(int i=0; i < DevFeatures.usMaxTimerChl; i+=2){
        cmbChannel->Items->Add(AnsiString(i));
    }
    cmbChannel->ItemIndex = 0;
    DRV_DeviceClose((LONG far *)&lDriverHandle);

}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
   btnSelectClick(NULL);       
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnStartClick(TObject *Sender)
{
    gwChannel = cmbChannel->ItemIndex;
    //open the Device
     ErrCde = DRV_DeviceOpen(lDeviceNum, (LONG far *) &lDriverHandle);
     if (ErrCde != SUCCESS){
            strcpy(szErrMsg,"Device open error !");
            Application->MessageBoxA((LPCSTR)szErrMsg,"Device Open");
            return;
    }
    bOpen = true;

    if(trbTime->Position>0 )
        ScanTimer->Interval = 1000/trbTime->Position;


    ScanTimer->Enabled = True;
     btnStart->Enabled = False;
    btnSelect->Enabled = False;
    btnStop->Enabled = True;
    cmbChannel->Enabled = False;
    btnExit->Enabled = False;
        
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ScanTimerTimer(TObject *Sender)
{
    ptPWMStartRead.usChan = gwChannel;
    ptPWMStartRead.flHiperiod = (FLOAT far *)&gfHiperiod;
    ptPWMStartRead.flLowperiod = (FLOAT far *)&gfLowperiod;
    if ((ErrCde = DRV_PWMStartRead(lDriverHandle,
               (LPT_PWMStartRead)&ptPWMStartRead)) != 0)
    {
         DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
         Application->MessageBox(szErrMsg, "Driver Message", MB_OK);
         btnSelect->Enabled = True;
         cmbChannel->Enabled = True;
         btnExit->Enabled = True;
         btnStart->Enabled = True;
         btnStop->Enabled = False;
         btnSelect->Enabled = true;
         ScanTimer->Enabled = False;
         return ;
    }
    edtHigh->Caption = FloatToStrF( gfHiperiod, ffFixed	, 10, 3);
    edtLow->Caption = FloatToStrF( gfLowperiod, ffFixed	, 10, 3);

}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnStopClick(TObject *Sender)
{
    ScanTimer->Enabled = False;
    ErrCde = DRV_CounterReset(lDriverHandle,gwChannel);
    if(ErrCde != SUCCESS){
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBoxA((char*)szErrMsg, "Driver Message");
    }
     btnSelect->Enabled = True;
     cmbChannel->Enabled = True;
     btnExit->Enabled = True;
     btnStart->Enabled = True;
     btnStop->Enabled = False;
    btnSelect->Enabled = true;
    DRV_DeviceClose((LONG far *)&lDriverHandle);
    bOpen = false;

}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnExitClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
   if(  bOpen == true)
        btnStopClick(NULL);

}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::trbTimeChange(TObject *Sender)
{
    if(trbTime->Position>0 )
        ScanTimer->Interval = 1000/trbTime->Position;
    else
        ScanTimer->Interval = 0;


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

⌨️ 快捷键说明

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