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

📄 pwmform.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
//---------------------------------------------------------------------------
/*
  Program: PWM
  Description: BCB Demo program for PWM output
  Version: 1.0
  Date: 08/29/02                   Advantech. Co., Ltd.
*/
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "PWMForm.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;

//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{

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

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);

    //open device
    ErrCde = DRV_DeviceOpen(lDeviceNum, (LONG far *) &lDriverHandle);
    if (ErrCde != SUCCESS){
        strcpy(szErrMsg,"Device open error !");
        Application->MessageBoxA((char* )szErrMsg,"Device Open");
        return;
    }
    //get device features
    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;
    }

    //record some device specific parameters
    dwBoardID = DevFeatures.dwBoardID;
    usMaxCntNum = DevFeatures.usMaxTimerChl;

    //updata UI
    cmbChannel->Clear();
    for(int i=0; i<usMaxCntNum; i++){
       cmbChannel->Items->Add(AnsiString(i));
    }
    cmbChannel->ItemIndex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnSelectDeviceClick(TObject *Sender)
{
    FormCreate(Sender);
}
//---------------------------------------------------------------------------








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.0005<-10e-10 || txtPeriod0->Text.ToDouble()>60){
        Application->MessageBoxA("The value is out of bound","Warning");
    }
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::txtHiPeriod0Exit(TObject *Sender)
{
    if((txtHiPeriod0->Text.ToDouble()-0.0005 < -10e-10)|| txtHiPeriod0->Text.ToDouble()>=txtPeriod0->Text.ToDouble()){
        Application->MessageBoxA("The value must no less than 0.0005 and no greater than the period time","Warning");
    }
}
//---------------------------------------------------------------------------






void __fastcall TfrmMain::btnRunClick(TObject *Sender)
{
    LRESULT     ErrCde;
    char        szErrMsg[80];     

    ErrCde = DRV_DeviceOpen(lDeviceNum, (LONG far *) &lDriverHandle);
    if (ErrCde != SUCCESS){
        strcpy(szErrMsg,"Device open error !");
        Application->MessageBoxA((char* )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

        ptCounterPWMSetting.Port = cmbChannel->Text.ToInt();
        ptCounterPWMSetting.Period = txtPeriod0->Text.ToDouble();
        ptCounterPWMSetting.HiPeriod = txtHiPeriod0->Text.ToDouble();
        ptCounterPWMSetting.GateMode = 0;
        ptCounterPWMSetting.OutCount = txtOutCount->Text.ToInt();   //no use here

        ErrCde = DRV_CounterPWMSetting(lDriverHandle, (LPT_CounterPWMSetting)&ptCounterPWMSetting);
        if (ErrCde != SUCCESS){
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                Application->MessageBoxA((char*)szErrMsg, "Driver Message");
                return;
        }
       ErrCde = DRV_CounterPWMEnable(lDriverHandle,cmbChannel->Text.ToInt());
       if(ErrCde != SUCCESS){
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                Application->MessageBoxA((char* )szErrMsg, "Driver Message");
                return;
       }


    btnRun->Enabled = False;
    btnStop->Enabled = True;
    btnExit->Enabled = False;
    grpDevSelect->Enabled = False;
    btnSet->Enabled = True;
    txtOutCount->Enabled = False;
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::btnStopClick(TObject *Sender)
{
    LRESULT  ErrCde;
    char * szErrMsg[80];
    ErrCde = DRV_CounterReset(lDriverHandle, cmbChannel->Text.ToInt());
    if(ErrCde != SUCCESS){
          DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
          Application->MessageBoxA((char*)szErrMsg, "Driver Message");
    }
    DRV_DeviceClose((LONG far*)&lDriverHandle);
    btnExit->Enabled = True;
    btnRun->Enabled = True;
    btnStop->Enabled = False;
    grpDevSelect->Enabled = True;
    btnSet->Enabled = False;
      txtOutCount->Enabled = True;
}
//---------------------------------------------------------------------------




void __fastcall TfrmMain::btnSetClick(TObject *Sender)
{
     if(ptCounterPWMSetting.OutCount != 0)
     {
        Application->MessageBoxA("Can't change setting in noncyclic mode", "Driver Message");
        return;
     }
        ptCounterPWMSetting.Port = cmbChannel->Text.ToInt();
        ptCounterPWMSetting.Period = txtPeriod0->Text.ToDouble();
        ptCounterPWMSetting.HiPeriod = txtHiPeriod0->Text.ToDouble();
        ptCounterPWMSetting.GateMode = 0;
        ptCounterPWMSetting.OutCount = txtOutCount->Text.ToInt();   //no use here

        ErrCde = DRV_CounterPWMSetting(lDriverHandle, (LPT_CounterPWMSetting)&ptCounterPWMSetting);
        if (ErrCde != SUCCESS){
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                Application->MessageBoxA((char*)szErrMsg, "Driver Message");
                return;
        }
}
//---------------------------------------------------------------------------



⌨️ 快捷键说明

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