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

📄 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"
#include <stdio.h>
#include "..\..\..\..\..\Include\Driver.h"
#include "..\..\..\..\..\Include\paras.h"
#include "..\..\..\..\..\Include\event.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
        char  DeviceName[ 50 ];
        
        m_ulDevNum = 0;
        DRV_SelectDevice( Handle, FALSE, &m_ulDevNum, DeviceName );
	editSelDev->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);
        }

        cmbCounter->ItemIndex = 0;
        cmbInt->ItemIndex = 0;
        btnStop->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSelDevClick(TObject *Sender)
{
        char DeviceName[50];

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

	DRV_SelectDevice( Handle, FALSE, &m_ulDevNum, DeviceName );
	editSelDev->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 TForm1::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 TForm1::btnStartClick(TObject *Sender)
{
        ULONG dwEventID;
        TListItem* pListItem;
        USHORT i;
        USHORT usTmp;
        char strValue[50];
        ULONG ulSize;
        ULONG* pCompData;

        m_CurCounter = cmbCounter->ItemIndex;
        m_StartPort = StrToInt(editStartPort->Text);
        m_PortCount = StrToInt(editPortCount->Text );
        m_StartCntr = StrToInt(editStartCnt->Text);
        m_CntrCount = StrToInt(editCntrCount->Text);

        m_InterruptSrc = 0;
        switch (cmbInt->ItemIndex)
        {
                case 0: dwEventID = ADS_EVT_OVERFLOW_CNT0 + m_CurCounter; break;
                case 1: dwEventID = ADS_EVT_UNDERFLOW_CNT0 + m_CurCounter; break;
                case 2: dwEventID = ADS_EVT_INDEX_CNT0 + m_CurCounter; break;
                case 3: dwEventID = ADS_EVT_OVERCOMPARE_CNT0 + m_CurCounter; break;
                case 4: dwEventID = ADS_EVT_UNDERCOMPARE_CNT0 + m_CurCounter; break;
                case 5: dwEventID = ADS_EVT_DI_INTERRUPT0; break;
                case 6: dwEventID = ADS_EVT_DI_INTERRUPT1; break;
                case 7: dwEventID = ADS_EVT_DI_INTERRUPT2; break;
                case 8: dwEventID = ADS_EVT_DI_INTERRUPT3; break;
                case 9: dwEventID = ADS_EVT_INTERRUPT_TIMER4; break;
        }
        m_InterruptSrc = dwEventID;
        if ((dwEventID >= ADS_EVT_DI_INTERRUPT0) && (dwEventID <= ADS_EVT_DI_INTERRUPT3))
        {
                m_ErrCde = AdxDioEnableEventAndSpecifyDiPorts(m_DriverHandle,
                        dwEventID, m_StartPort, m_PortCount);

                m_ErrCde = AdxDioEnableEventAndSpecifyEventCounter(m_DriverHandle,
                        dwEventID, m_StartCntr, m_CntrCount);
        }
        else
        {
                m_ErrCde = AdxCntrEnableEventAndSpecifyEventCounter(m_DriverHandle,
                        dwEventID, m_StartCntr, m_CntrCount);
        }
        if ( m_ErrCde != SUCCESS )
        {
                DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
                MessageBox(Handle, m_szErrMsg, "Driver Message", MB_OK);
        }
        if ((dwEventID == (ULONG)(ADS_EVT_OVERCOMPARE_CNT0 + m_CurCounter)) || (dwEventID == (ULONG)(ADS_EVT_UNDERCOMPARE_CNT0 + m_CurCounter)))
        {
                // Set Compare Data
                m_ErrCde = DRV_DeviceGetProperty(m_DriverHandle,CFG_CntrCompareData, NULL, &ulSize);
                if (m_ErrCde == SUCCESS)
                {
                        pCompData = new ULONG[ulSize / sizeof(ULONG)];
                        m_ErrCde = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrCompareData, pCompData, &ulSize);
                        pCompData[m_CurCounter] = StrToInt(editCompData->Text);
                        m_ErrCde = DRV_DeviceSetProperty(m_DriverHandle, CFG_CntrCompareData, pCompData, ulSize);
                        delete[] pCompData;
                }
        }

        lsvCounter->Items->Clear();
        lsvDI->Items->Clear();

        btnSelDev->Enabled = FALSE;
        btnStart->Enabled = FALSE;
        btnStop->Enabled = TRUE;
        btnClose->Enabled = FALSE;

        m_bContinue = TRUE;
        pEventThread = new EventThread(false);
        
}
//---------------------------------------------------------------------------
void TForm1::CheckEvent()
{
        USHORT usEventType;
        PT_CheckEvent ptCheckEvent;
        BYTE pData[1];
        ULONG pCntrData[4];
        USHORT i;
        USHORT usTmp;
        TListItem* pListItem;
        char strValue[100];

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

        while(m_bContinue)
        {
                if (DRV_CheckEvent(m_DriverHandle, &ptCheckEvent) == SUCCESS)
                {
                        if ((usEventType >= ADS_EVT_DI_INTERRUPT0) &&
                                (usEventType <= ADS_EVT_DI_INTERRUPT3))
                        {
                                AdxDioGetLatestEventDiPortsState(m_DriverHandle,
                                        usEventType, pData, m_PortCount);

                                lsvDI->Items->Clear();
                                for (i = 0; i < m_PortCount; i++)
                                {
                                        usTmp = i + m_StartPort;
                                        sprintf(strValue, "Port%d", usTmp);
                                        pListItem = lsvDI->Items->Add();
                                        pListItem->Caption = strValue;

                                        sprintf( strValue, "%d", pData[i]);
                                        pListItem->SubItems->Add(strValue);
                                }
                        }
                        if ((usEventType != ADS_EVT_NO_EVENT) &&
                                (usEventType != ADS_EVT_TIME_OUT))
                        {
                                AdxCntrGetLatestEventCounterValue(m_DriverHandle,
                                        usEventType, pCntrData, m_CntrCount);

                                lsvCounter->Items->Clear();
                                for (i = 0; i < m_CntrCount; i++)
                                {
                                        usTmp = i + m_StartCntr;
                                        sprintf(strValue, "%d", usTmp);
                                        pListItem = lsvCounter->Items->Add();
                                        pListItem->Caption = strValue;

                                        sprintf( strValue, "%lu", pCntrData[i]);
                                        pListItem->SubItems->Add(strValue);
                                }
                        }
                }
        }
}
//---------------------------------------------------------------------------
void TForm1::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 ----
        Form1->CheckEvent();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnStopClick(TObject *Sender)
{
        ULONG dwEventID;

        btnSelDev->Enabled = TRUE;
        btnStart->Enabled = TRUE;
        btnStop->Enabled = FALSE;
        btnClose->Enabled = TRUE;

        m_bContinue = FALSE;
        dwEventID = m_InterruptSrc;
        if ((dwEventID >= ADS_EVT_DI_INTERRUPT0) && (dwEventID <= ADS_EVT_DI_INTERRUPT3))
                m_ErrCde = AdxDioDisableEvent(m_DriverHandle, dwEventID);
        else
                m_ErrCde = AdxCntrDisableEvent(m_DriverHandle, dwEventID);
        if ( m_ErrCde != SUCCESS )
        {
                DRV_GetErrorMessage(m_ErrCde, m_szErrMsg);
                MessageBox(Handle, m_szErrMsg, "Driver Message", MB_OK);
        }

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

⌨️ 快捷键说明

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