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

📄 unit1.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
        char  DeviceName[ 50 ];
        
        m_ulDevNum = 0;
        DRV_SelectDevice( Handle, FALSE, &m_ulDevNum, DeviceName );
	editDevName->Text = DeviceName;

        // open device
        m_ErrCde = DRV_DeviceOpen( m_ulDevNum, &m_DriverHandle );
	if ( m_ErrCde != SUCCESS )
        {
           DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
           MessageBox(Handle, m_szErrMsg, "Device Open", MB_OK);
        }

        m_Unit = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
        m_bContinue = FALSE;

        // Terminate thread
        KillThread();

        // Close device
        Sleep(50L);
	if ( m_DriverHandle != NULL )
           DRV_DeviceClose( &m_DriverHandle );        
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnSelDevClick(TObject *Sender)
{
        char DeviceName[50];

        if ( m_DriverHandle != NULL )
           DRV_DeviceClose( &m_DriverHandle );

	DRV_SelectDevice( Handle, FALSE, &m_ulDevNum, DeviceName );
	editDevName->Text = DeviceName;
	m_ErrCde = DRV_DeviceOpen( m_ulDevNum, &m_DriverHandle);
        if ( m_ErrCde != SUCCESS )
        {
           DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
           MessageBox(Handle, m_szErrMsg, "Device Open", MB_OK);
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnSetTimerClick(TObject *Sender)
{
        ULONG ulTimerInterval;
        ULONG ulSize;
        USHORT usDividor;
        float fFreq, fFreqBase;
        float* pTimerFreq;
        PT_TimerCountSetting ptTimerCountSetting;

        if (m_Unit == 3) // s
                ulTimerInterval = StrToInt(editTimerInt->Text) * 1000000;
        else if (m_Unit == 2) //ms
                ulTimerInterval = StrToInt(editTimerInt->Text) * 1000;
        else  //us
                ulTimerInterval = StrToInt(editTimerInt->Text);

        fFreq = 1000000.0 / ulTimerInterval;

	if (fFreq > TIMER_BASE_5KHZ)
		fFreqBase = TIMER_BASE_50KHZ;
	else if ((fFreq <= TIMER_BASE_5KHZ) && (fFreq > TIMER_BASE_500HZ))
		fFreqBase = TIMER_BASE_5KHZ;
	else if ((fFreq <= TIMER_BASE_500HZ) && (fFreq > TIMER_BASE_50HZ))
		fFreqBase = TIMER_BASE_500HZ;
	else if ((fFreq <= TIMER_BASE_50HZ) && (fFreq > TIMER_BASE_5HZ))
		fFreqBase = TIMER_BASE_50HZ;
	else
		fFreqBase = TIMER_BASE_5HZ;

        usDividor = (USHORT)(ulTimerInterval * fFreqBase / 1000000 + 0.5);
	if (usDividor == 0)
		usDividor = 1;

        // Set Timer Base
        m_ErrCde = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrClockFrequency, NULL, &ulSize);
        pTimerFreq = new float[ulSize / sizeof(float)];
        m_ErrCde = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrClockFrequency, pTimerFreq, &ulSize);
        pTimerFreq[4] = fFreq;
        m_ErrCde = DRV_DeviceSetProperty(m_DriverHandle, CFG_CntrClockFrequency, pTimerFreq, ulSize);
        if ( m_ErrCde != SUCCESS )
        {
           DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
           MessageBox(Handle, m_szErrMsg, "Driver Message", MB_OK);
        }
        delete[]  pTimerFreq;

	// PCI-1784 Counter 4 is a timer
	ptTimerCountSetting.Count = usDividor; 
	ptTimerCountSetting.counter = 4;
	m_ErrCde = DRV_TimerCountSetting(m_DriverHandle, &ptTimerCountSetting);
        if ( m_ErrCde != SUCCESS )
        {
           DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
           MessageBox(Handle, m_szErrMsg, "Driver Message", MB_OK);
        }

        // Real Timer Interval
        ulTimerInterval = 1000000 / fFreqBase * usDividor;
        if (ulTimerInterval >= 1000000)
        {
                ulTimerInterval = ulTimerInterval / 1000000;
                labUnit->Caption = "s";
        }
        else if ((ulTimerInterval >= 1000) && (ulTimerInterval < 1000000))
        {
                ulTimerInterval = ulTimerInterval / 1000;
                labUnit->Caption = "ms";
        }
        else
        {
                labUnit->Caption = "us";
        }
        editRealTimer->Text = IntToStr(ulTimerInterval);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::radUsClick(TObject *Sender)
{
        m_Unit = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::radMsClick(TObject *Sender)
{
        m_Unit = 2;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::radSClick(TObject *Sender)
{
        m_Unit = 3;        
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnEnableClick(TObject *Sender)
{
        PT_EnableEvent ptEnalbeEvent;

        m_ErrCde = DRV_CounterReset(m_DriverHandle, 0);

	ptEnalbeEvent.Count = 1;
	ptEnalbeEvent.Enabled = TRUE;
	ptEnalbeEvent.EventType = ADS_EVT_INTERRUPT_TIMER4;
	m_ErrCde = DRV_EnableEvent(m_DriverHandle, &ptEnalbeEvent);
        if ( m_ErrCde != SUCCESS )
        {
           DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
           MessageBox(Handle, m_szErrMsg, "Driver Message", MB_OK);
        }

        btnSelDev->Enabled = FALSE;
        btnEnable->Enabled = FALSE;
        btnDisable->Enabled = TRUE;
        btnClose->Enabled = FALSE;

        m_bContinue = TRUE;
        pEventThread = new EventThread(false);
}
//---------------------------------------------------------------------------
void TForm2::KillThread()
{
    DWORD ExitCode;

     m_bContinue = FALSE;

     if (pEventThread)
     {
          pEventThread->Terminate();
          GetExitCodeThread((void *)(pEventThread->Handle), &ExitCode);
          if(ExitCode==STILL_ACTIVE)
                TerminateThread((void *)(pEventThread->Handle), ExitCode);
     }

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

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall EventThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------

__fastcall EventThread::EventThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall EventThread::Execute()
{
        //---- Place thread code here ----
        Form2->CheckEvent();
}
//---------------------------------------------------------------------------
void TForm2::CheckEvent()
{
        USHORT usEventType;
        PT_CheckEvent ptCheckEvent;
        ULONG dwEventCount;

        ptCheckEvent.EventType = &usEventType;
        ptCheckEvent.Milliseconds  = 1000;

        dwEventCount = 0;
        while (m_bContinue)
        {
                if (DRV_CheckEvent(m_DriverHandle, &ptCheckEvent) == SUCCESS)
                {
                        if (usEventType == ADS_EVT_INTERRUPT_TIMER4)
			{
				dwEventCount++;
                                editEventCount->Text = IntToStr(dwEventCount);
                        }
                }
        }
}
void __fastcall TForm2::btnDisableClick(TObject *Sender)
{
        PT_EnableEvent ptEnalbeEvent;

        m_bContinue = FALSE;

	ptEnalbeEvent.Count = 1;
	ptEnalbeEvent.Enabled = FALSE;
	ptEnalbeEvent.EventType = ADS_EVT_INTERRUPT_TIMER4;
	m_ErrCde = DRV_EnableEvent(m_DriverHandle, &ptEnalbeEvent);
        if ( m_ErrCde != SUCCESS )
        {
           DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
           MessageBox(Handle, m_szErrMsg, "Driver Message", MB_OK);
        }

        btnSelDev->Enabled = TRUE;
        btnEnable->Enabled = TRUE;
        btnDisable->Enabled = FALSE;
        btnClose->Enabled = TRUE;

}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnCloseClick(TObject *Sender)
{
        Close();        
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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