📄 pwmform.cpp
字号:
#include <vcl.h>
#pragma hdrstop
#include "PWMForm.h"
#include "../../../../include/driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
//the static varialble for PWM operation
static ULONG lDeviceNum; //Advantech Device Number in your system
static LONG lDriverHandle; //Driver handle
static char szDescription[80]; //description for Select Device
static DEVFEATURES DevFeatures; // structure for device features
static PT_DeviceGetFeatures ptDevGetFeatures;
static PT_CounterPWMSetting ptCounterPWMSetting;
TCheckBox * chkPWMEnable[3];
TEdit * txtPeriod[3];
TEdit * txtHiPeriod[3];
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
chkPWMEnable[0] = chkPWMEnable0;
chkPWMEnable[1] = chkPWMEnable1;
chkPWMEnable[2] = chkPWMEnable2;
txtPeriod[0]= txtPeriod0;
txtPeriod[1]= txtPeriod1;
txtPeriod[2]= txtPeriod2;
txtHiPeriod[0]= txtHiPeriod0;
txtHiPeriod[1]= txtHiPeriod1;
txtHiPeriod[2]= txtHiPeriod2;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::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");
btnExitClick(Sender);
}
labDeviceName->Caption = AnsiString(szDescription);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSelectDeviceClick(TObject *Sender)
{
//This function is for getting the device number
DRV_SelectDevice(Handle, True, (ULONG*)&lDeviceNum, szDescription);
labDeviceName->Caption = AnsiString(szDescription);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::chkPWMEnable0Click(TObject *Sender)
{
//Settings for port0
if (chkPWMEnable0->Checked){
txtPeriod0->Enabled = True;
txtHiPeriod0->Enabled = True;
}else{
txtPeriod0->Enabled = False;
txtHiPeriod0->Enabled = False;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::chkPWMEnable1Click(TObject *Sender)
{
//Settings for port1
if (chkPWMEnable1->Checked){
txtPeriod1->Enabled = True;
txtHiPeriod1->Enabled = True;
}else{
txtPeriod1->Enabled = False;
txtHiPeriod1->Enabled = False;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::chkPWMEnable2Click(TObject *Sender)
{
//Settings for port2
if (chkPWMEnable2->Checked){
txtPeriod2->Enabled = True;
txtHiPeriod2->Enabled = True;
}else{
txtPeriod2->Enabled = False;
txtHiPeriod2->Enabled = False;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnExitClick(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::txtPeriod0Exit(TObject *Sender)
{
//Check the validation of input data
if(txtPeriod0->Text.ToDouble()<=0){
Application->MessageBoxA("The value must be greater than 0","Warning");
txtPeriod0->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::txtHiPeriod0Exit(TObject *Sender)
{
if(txtHiPeriod0->Text.ToDouble()<=0 || txtHiPeriod0->Text.ToDouble()>=txtPeriod0->Text.ToDouble()){
Application->MessageBoxA("The value must be greater than 0 and less than the period time","Warning");
txtHiPeriod0->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::txtPeriod1Exit(TObject *Sender)
{
if(txtPeriod1->Text.ToDouble()<=0){
Application->MessageBoxA("The value must be greater than 0","warning");
txtPeriod1->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::txtHiPeriod1Exit(TObject *Sender)
{
if(txtHiPeriod1->Text.ToDouble()<=0 || txtHiPeriod1->Text.ToDouble()>=txtPeriod1->Text.ToDouble()){
Application->MessageBoxA("The value must be greater than 0 and less than the period time","Warning");
txtHiPeriod1->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::txtPeriod2Exit(TObject *Sender)
{
if(txtPeriod2->Text.ToDouble()<=0){
Application->MessageBoxA("The value must be greater than 0","warning");
txtPeriod2->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::txtHiPeriod2Exit(TObject *Sender)
{
if(txtHiPeriod2->Text.ToDouble()<=0 || txtHiPeriod2->Text.ToDouble()>=txtPeriod2->Text.ToDouble()){
Application->MessageBoxA("The value must be greater than 0 and less than the period time","Warning");
txtHiPeriod2->SetFocus();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnRunClick(TObject *Sender)
{
LRESULT ErrCde;
char szErrMsg[80];
int i;
if((False==chkPWMEnable0->Checked) && (False==chkPWMEnable1->Checked)
&& (False==chkPWMEnable2->Checked)){
Application->MessageBoxA("please select a port","Warning");
return;
}
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;
}
//start counter
for (i = 0; i < 3; i++)
{
if(chkPWMEnable[i]->Checked){
ptCounterPWMSetting.Port = i;
ptCounterPWMSetting.Period = txtPeriod[i]->Text.ToDouble();
ptCounterPWMSetting.HiPeriod = txtHiPeriod[i]->Text.ToDouble();
ptCounterPWMSetting.GateMode = 0;
ptCounterPWMSetting.OutCount = 0;
ErrCde = DRV_CounterPWMSetting(lDriverHandle, (LPT_CounterPWMSetting)&ptCounterPWMSetting);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
return;
}
ErrCde = DRV_CounterPWMEnable(lDriverHandle, i);
if(ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
return;
}
}
}
btnRun->Enabled = False;
btnStop->Enabled = True;
btnExit->Enabled = False;
grpPWMConfiguration->Enabled = False;
grpDevSelect->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnStopClick(TObject *Sender)
{
LRESULT ErrCde;
char szErrMsg[80];
int i;
for (i = 0; i < 3; i++)
{
ErrCde = DRV_CounterReset(lDriverHandle, i);
if(ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
}
}
DRV_DeviceClose((LONG far*)&lDriverHandle);
btnExit->Enabled = True;
btnRun->Enabled = True;
btnStop->Enabled = False;
grpPWMConfiguration->Enabled = True;
grpDevSelect->Enabled = True;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -