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

📄 mainform.cpp

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

#include <vcl.h>
#pragma hdrstop
#include "SettingForm.h"
#include "MainForm.h"

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


//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
m_DeviceHandle=0;
pchkThread=NULL;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnSelDevClick(TObject *Sender)
{
	char DeviceName[150];
   DWORD dwreturnCode;
   TListItem *lstItem;
   m_ulDevNum=0;
   DWORD dwExitCode;
   if (m_DeviceHandle != 0)
   {
      btnStopAllClick(Sender);
      if (pchkThread != NULL )
      {
         GetExitCodeThread((void*)(pchkThread->Handle), &dwExitCode);
         if (dwExitCode == STILL_ACTIVE)
            TerminateThread((void*)(pchkThread->Handle), dwExitCode);

         pchkThread->Terminate();
         delete pchkThread;
         pchkThread = NULL;
      }
      DRV_DeviceClose(&m_DeviceHandle);
   }

   for( i= 0; i< 128; i++)
     bDIEnableINT[i]= false;
     //Select Device
   DRV_SelectDevice(Handle, false, (ULONG *)&m_DeviceNum, DeviceName);
   txtDeviceName->Text = AnsiString(DeviceName);
   //Open the Device
   m_ErrCde = DRV_DeviceOpen(m_DeviceNum, &m_DeviceHandle);
    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
    //Get the Device Feature
   PT_DeviceGetFeatures ptGetFeatures;
   ptGetFeatures.buffer = &m_DeviceFeatures;
   ptGetFeatures.size = sizeof(DEVFEATURES);
   DRV_DeviceGetFeatures(m_DeviceHandle, &ptGetFeatures);
   //Get the DI channel Number
   m_DIChanNum = m_DeviceFeatures.usMaxDIChl;
   m_DIPortNum = m_DIChanNum / 8;

   ulDataLength = sizeof(DWORD) * 4;
   //Get the DI channel using Rising trigger
   m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_DiTriggerEnableRisingForAll,
      &(DIRisingTrigger[0]), &ulDataLength);
  //Get the DI channel using Falling trigger
    m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_DiTriggerEnableFallingForAll,
      &(DIFallingTrigger[0]), &ulDataLength);
 //Get the DI channel using filter
    m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_DiFilterEnableForAll,
      &(DIFilterEnable[0]), &ulDataLength);
   ulDataLength = sizeof(DWORD);
   //Get the Filter  counter
   m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_DiFilterIntervalCounter,
      &m_FilterCounter, &ulDataLength);

    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
   txtFilterCounter->Text = szBuff.sprintf("%d", m_FilterCounter);
   lstIntChannel->Clear();
   for( i = 0 ; i<m_DIChanNum ;i++)
   {
      lstItem = lstIntChannel->Items->Add();
      szBuff.sprintf("Port%X_%02d", i / 8, i % 8);
      lstItem->Caption = szBuff;
      lstItem->SubItems->Add("None");
      lstItem->SubItems->Add("No");
      lstItem->SubItems->Add("0");
   }
   cmbPort->Clear();
   for( i = 0 ; i<m_DIPortNum;i++)
      {
      szBuff.sprintf("Port%X", i);
      cmbPort->Items->Add(szBuff);
   }
   cmbPort->ItemIndex = 0;
  SetTrigerMode();
   SetFilter();
}

//---------------------------------------------------------------------------
void TfrmMain::SetFilter()
{

   TListItem *lstItem ;
   int DIPort, DIPortBit;
   for( i = 0 ; i<m_DIChanNum ;i++)
   {
      lstItem = lstIntChannel->Items->Item[i];
      DIPort = i / 8;
      DIPortBit = 1 << (i % 8);
      if ((DIFilterEnable[DIPort] & DIPortBit) != 0 )
         szBuff = "Yes";
      else
         szBuff = "No";

      lstItem->SubItems->Strings[1] = szBuff;
   }

}
//---------------------------------------------------------------------------
void TfrmMain::SetTrigerMode()
{
   TListItem *lstItem ;
   int DIPort, DIPortBit;
     for( i = 0 ; i<m_DIChanNum ;i++)
   {
      lstItem=lstIntChannel->Items->Item[i];
      DIPort = i / 8;
      DIPortBit = 1 << (i % 8);
      if (((DIRisingTrigger[DIPort] & DIPortBit) != 0) &&
         ((DIFallingTrigger[DIPort] & DIPortBit) != 0))
          szBuff = "Rising & Falling edge";
      else if ((DIRisingTrigger[DIPort] & DIPortBit) != 0)
           szBuff = "Rising edge";
      else if ((DIFallingTrigger[DIPort] & DIPortBit) != 0)
           szBuff = "Falling edge";
      else
         szBuff = "None";
      lstItem->SubItems->Strings[0] = szBuff;
      }
}

void __fastcall TfrmMain::btnExitClick(TObject *Sender)
{
     btnStopAllClick(Sender);
     Application->Terminate();   
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::btnRisingClick(TObject *Sender)
{
   TDISettingDlg *pDiDlg;

   pDiDlg = new TDISettingDlg(NULL);
   pDiDlg->DISetting = DIRisingTrigger;
   pDiDlg->labInfo->Caption = "Enable or disable Rising trigger for every DI channel:";
   if( pDiDlg->ShowModal() == mrOk )
   {

      SetTrigerMode();
      ulDataLength = sizeof(BYTE) * 16;
      DRV_DeviceSetProperty(m_DeviceHandle, CFG_DiTriggerEnableRisingForAll,
         &(DIRisingTrigger[0]),ulDataLength);
     //or
 /*
      ulDataLength = sizeof(BYTE) ;
      for( i=0; i<16; i++)
         DRV_DeviceSetProperty( m_DeviceHandle, CFG_DiTriggerEnableRisingPort0+i,
         &(DIRisingTrigger[i]) , ulDataLength);
   */
   }
   delete pDiDlg;

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

void __fastcall TfrmMain::btnFallingClick(TObject *Sender)
{
   TDISettingDlg *pDiDlg;

   pDiDlg = new TDISettingDlg(NULL);
   pDiDlg->DISetting = DIFallingTrigger;
   pDiDlg->labInfo->Caption = "Enable or disable Falling trigger for every DI channel:";
   if( pDiDlg->ShowModal() == mrOk )
   {

      SetTrigerMode();
      ulDataLength = sizeof(BYTE) * 16;
      DRV_DeviceSetProperty(m_DeviceHandle, CFG_DiTriggerEnableFallingForAll,
         &(DIFallingTrigger[0]),ulDataLength);
     //or
 /*
      ulDataLength = sizeof(BYTE) ;
      for( i=0; i<16; i++)
         DRV_DeviceSetProperty( m_DeviceHandle, CFG_DiTriggerEnableRisingPort0+i,
         &(DIRisingTrigger[i]) , ulDataLength);
   */
   }
   delete pDiDlg;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnFilterClick(TObject *Sender)
{
   TDISettingDlg *pDiDlg;

   pDiDlg = new TDISettingDlg(NULL);
   pDiDlg->DISetting = DIFilterEnable;
   pDiDlg->labInfo->Caption = "Enable or disable Filter function for every DI channel:";
   if( pDiDlg->ShowModal() == mrOk )
   {

      SetFilter();
      ulDataLength = sizeof(BYTE) * 16;
      DRV_DeviceSetProperty(m_DeviceHandle, CFG_DiFilterEnableForAll,
         &(DIFilterEnable[0]),ulDataLength);
     //or
 /*
      ulDataLength = sizeof(BYTE) ;
      for( i=0; i<16; i++)
         DRV_DeviceSetProperty( m_DeviceHandle, CFG_DiTriggerEnableRisingPort0+i,
         &(DIRisingTrigger[i]) , ulDataLength);
   */
   }
   delete pDiDlg;
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::ScanTimerTimer(TObject *Sender)
{

   USHORT DIValue;
   PT_DioReadPortByte ptDioReadPortByte ;
   ptDioReadPortByte.port = cmbPort->ItemIndex;
   ptDioReadPortByte.value = &DIValue;
   DRV_DioReadPortByte(m_DeviceHandle, &ptDioReadPortByte);
   szBuff.sprintf("%2.2X H", DIValue);
   editScanData->Text = szBuff;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnStartClick(TObject *Sender)
{
ScanTimer->Interval =  editScanTime->Text.ToInt();
ScanTimer->Enabled = true;
   btnStart->Enabled = false;
   btnStop->Enabled = true;

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

void __fastcall TfrmMain::btnStopClick(TObject *Sender)
{
 ScanTimer->Enabled = false;

    btnStart->Enabled = true;
   btnStop->Enabled = false;
   }
//---------------------------------------------------------------------------

void __fastcall TfrmMain::txtFilterCounterChange(TObject *Sender)
{
   float fFilterKHz = txtFilterCounter->Text.ToDouble();
   szBuff.sprintf("%g",10000.0 / (fFilterKHz * 2.0)) ;
   labFilterHz->Caption = szBuff;

   //Set the Filter  counter
      ulDataLength = sizeof(DWORD);
   m_FilterCounter = StrToInt64(txtFilterCounter->Text);
   m_ErrCde = DRV_DeviceSetProperty(m_DeviceHandle, CFG_DiFilterIntervalCounter,
      &m_FilterCounter, ulDataLength);
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::lstIntChannelChange(TObject *Sender,
      TListItem *Item, TItemChange Change)
{
   PT_EnableEvent EventSetting ;
   USHORT usTemp;
   DWORD dwExitCode;

   if( bDIEnableINT[Item->Index] ==  Item->Checked )
     return;
   EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + Item->Index;
   bDIEnableINT[ Item->Index ]= Item->Checked;
   if (Item->Checked)
   {
      EventSetting.Enabled = 1;
      EventSetting.Count = 1;
      dwEventCount[Item->Index] = 0;

   }
   else
   {
      EventSetting.Enabled = 0;
      EventSetting.Count = 0;
    }
   m_ErrCde = DRV_EnableEvent(m_DeviceHandle, &EventSetting);
    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
    
       if (pchkThread == NULL )
      {
         pchkThread = new CheckThread(true);
         pchkThread->Priority = tpHighest;
         pchkThread->Resume();
         dwStartTime = GetTickCount();
         IntTimer->Enabled = true;
      }

   usTemp = 0;
   for( i = 0 ;i <m_DIChanNum ;i++)
      usTemp = usTemp | bDIEnableINT[i];

   if ((usTemp == 0) && (pchkThread!=NULL))
   {
          GetExitCodeThread((void*)(pchkThread->Handle), &dwExitCode);
         if (dwExitCode == STILL_ACTIVE)
            TerminateThread((void*)(pchkThread->Handle), dwExitCode);
         pchkThread->Terminate();
         delete pchkThread;
         pchkThread = NULL;
         IntTimer->Enabled = false;

     }
     

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

void __fastcall TfrmMain::IntTimerTimer(TObject *Sender)
{
   float ratio[128];
   dwCurrentTime = GetTickCount();
   dwTime = dwCurrentTime - dwStartTime;

   if (dwTime > 900)
   {
      dwStartTime = dwCurrentTime;
      for( i = 0;i<m_DIChanNum; i++)
      {
            ratio[i] = dwEventCount[i] * 1000.0 / dwTime;
            dwEventCount[i] = 0;
      }
      for( i = 0;i<m_DIChanNum; i++)
      {
          szBuff.sprintf("%0.1f", ratio[i]);
         lstIntChannel->Items->Item[i]->SubItems->Strings[2] = szBuff;
        }
    }

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

void __fastcall TfrmMain::btnStopAllClick(TObject *Sender)
{
   DWORD dwExitCode;

     for( int i = 0 ;i <m_DIChanNum ;i++)
   {
      if(lstIntChannel->Items->Item[i]->Checked )
       lstIntChannel->Items->Item[i]->Checked= false;
   }

   if (pchkThread != NULL )
   {
        GetExitCodeThread((void*)(pchkThread->Handle), &dwExitCode);
         if (dwExitCode == STILL_ACTIVE)
            TerminateThread((void*)(pchkThread->Handle), dwExitCode);

         pchkThread->Terminate();
         delete pchkThread;
         pchkThread = NULL;
         IntTimer->Enabled = false;
   }
}
//---------------------------------------------------------------------------


void TfrmMain::CheckEventProc()
{
        //TODO: Add your source code here
   PT_CheckEvent ptCheckEvent ;
   USHORT usEventType;
   int indx;

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

   while (true)
   {
      DRV_CheckEvent(m_DeviceHandle, &ptCheckEvent);

      indx = usEventType - ADS_EVT_INTERRUPT_DI0;
      if ((indx >= 0) && (indx < 128))
         dwEventCount[indx] = dwEventCount[indx] + 1;

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

⌨️ 快捷键说明

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