freqform.cpp
来自「16 relay output channels and 16 isolated」· C++ 代码 · 共 178 行
CPP
178 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "FreqForm.h"
#include "..\..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmFreq *frmFreq;
//---------------------------------------------------------------------------
static LONG lDriverHandle; //Driver handle
static char szErrMsg[80]; // Use for MESSAGEBOX function
static LRESULT ErrCde; // Return error code
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_CounterFreqStart ptCounterFreqStart;
static PT_CounterFreqRead ptCounterFreqRead;
static BOOL bRun = False;
static BOOL bOpen = False;
float fFreq = 0;
//---------------------------------------------------------------------------
__fastcall TfrmFreq::TfrmFreq(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::FormCreate(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
if(False == bOpen){
ErrCde = DRV_DeviceOpen(lDeviceNum, (LONG far *) &lDriverHandle);
if (ErrCde != SUCCESS){
strcpy(szErrMsg,"Device open error !");
Application->MessageBoxA((LPCSTR)szErrMsg,"Device Open");
return;
}
bOpen = True;
}
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);
bOpen = False;
return;
}
if (0 == DevFeatures.usMaxTimerChl){
Application->MessageBoxA((LPCSTR)"No Counter Channel","Driver Message");
DRV_DeviceClose((LONG far *)&lDriverHandle);
bOpen = False;
return;
}
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();
//the following 2 parameters is not concerned in this demo, for more information
//please refer to the hardware manual and software manual
ptCounterFreqStart.GatePeriod = 0;
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;
}
if(tbScanTime->Position>0 )
ScanTimer->Interval = 1000/tbScanTime->Position;
ScanTimer->Enabled = True;
bRun = True;
btnStart->Enabled = False;
btnStop->Enabled = True;
grpChannel->Enabled = False;
grpDeviceSelection->Enabled = False;
btnExit->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::ScanTimerTimer(TObject *Sender)
{
//read the Frequency
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");
}
bRun = FALSE;
grpDeviceSelection->Enabled = True;
grpChannel->Enabled = True;
btnExit->Enabled = True;
btnStart->Enabled = True;
btnStop->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::FormClose(TObject *Sender, TCloseAction &Action)
{
if(True == bRun){
btnStopClick(Sender);
}
if(True == bOpen){
DRV_DeviceClose((LONG far *)&lDriverHandle);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmFreq::tbScanTimeChange(TObject *Sender)
{
if( True == bRun){
ScanTimer->Enabled = False;
if(0 == tbScanTime->Position){
ScanTimer->Interval = 0;
}else{
ScanTimer->Interval = 1000 / tbScanTime->Position;
}
ScanTimer->Enabled = True;
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?