freqform.cpp

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

CPP
165
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FreqForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmFreq *frmFreq;
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
__fastcall TfrmFreq::TfrmFreq(TComponent* Owner)
        : TForm(Owner)
{
   lDriverHandle = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::FormCreate(TObject *Sender)
{
    if(!lDriverHandle){
       DRV_DeviceClose(&lDriverHandle);
       lDriverHandle = 0;
    }
    //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;
    }
    cmbChannel->Clear();
    for(int i=0; i < DevFeatures.usMaxTimerChl; i+=2){
        cmbChannel->Items->Add(AnsiString(i));
    }
    cmbChannel->ItemIndex = 0;

}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::btnSelectDeviceClick(TObject *Sender)
{
    //This function is for getting the device number
    FormCreate(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::btnStartClick(TObject *Sender)
{
    //start counter
    ptCounterFreqStart.counter = cmbChannel->Text.ToInt();
    ptCounterFreqStart.GatePeriod = EditGP->Text.ToInt();
    //the following  parameters is not concerned in this demo, for more information
    //please refer to the hardware manual and software manual
    ptCounterFreqStart.GateMode = 0;
    ErrCde = DRV_CounterFreqStart(lDriverHandle,(LPT_CounterFreqStart)&ptCounterFreqStart);
    if(ErrCde != SUCCESS){
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBoxA((char* )szErrMsg, "Driver Message");
        return;
    }
    //Start timer
    ScanTimer->Interval = tbScanTime->Position;
    ScanTimer->Enabled = True;
    btnStart->Enabled = False;
    btnStop->Enabled = True;
    grpChannel->Enabled = False;
    grpDeviceSelection->Enabled = False;
    btnExit->Enabled = False;
    UpDownGP->Enabled = False;
    EditGP->Enabled = False;

}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::ScanTimerTimer(TObject *Sender)
{
    //read the Frequency
    float fFreq;
    AnsiString strFreq;
    ptCounterFreqRead.counter = cmbChannel->Text.ToInt();
    ptCounterFreqRead.freq    = (float far*)&fFreq;
    ErrCde = DRV_CounterFreqRead(lDriverHandle,(LPT_CounterFreqRead)&ptCounterFreqRead);
    if(ErrCde != SUCCESS){
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBoxA((char* )szErrMsg, "Driver Message");
        ScanTimer->Enabled = False;
        return;
    }
    //display the data
    TVarRec args[3] = {fFreq};
    strFreq = Format("%.3f",args,0);
    stFreq->Caption = (strFreq+"HZ");
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::btnExitClick(TObject *Sender)
{
    Application->Terminate();        
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::btnStopClick(TObject *Sender)
{
    
    ScanTimer->Enabled = False;
    ErrCde = DRV_CounterReset(lDriverHandle,cmbChannel->Text.ToInt());
    if(ErrCde != SUCCESS){
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBoxA((char*)szErrMsg, "Driver Message");
     }
     
     grpDeviceSelection->Enabled = True;
     grpChannel->Enabled = True;
     btnExit->Enabled = True;
     btnStart->Enabled = True;
     EditGP->Enabled = True;
     UpDownGP->Enabled = True;
     btnStop->Enabled = False;


}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::FormClose(TObject *Sender, TCloseAction &Action)
{

    if(lDriverHandle){
        btnStopClick(Sender);
        DRV_DeviceClose((LONG far *)&lDriverHandle);
    }

}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::tbScanTimeChange(TObject *Sender)
{
    if( False == btnStart->Enabled){
        ScanTimer->Enabled = False;
        if(0 == tbScanTime->Position){
            ScanTimer->Interval = 0;
        }else{
            ScanTimer->Interval = tbScanTime->Position;
        }
        ScanTimer->Enabled = True;
    }        
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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