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

📄 mainform.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSelDevClick(TObject *Sender)
{
    char DeviceName[150];
   DWORD dwreturnCode;

   m_ulDevNum=0;
   DWORD dwExitCode;
   GBPresetValue->Enabled = false;
   GBWatchdogSet->Enabled = false;
   GBDO->Enabled = false;
   GBWatchdog->Enabled = false;
   if (m_DeviceHandle != 0)
   {
      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 DO channel Number
   m_DOChanNum = m_DeviceFeatures.usMaxDOChl;
   m_DOPortNum = m_DOChanNum / 8;
    if (m_DOChanNum == 0)
    {
        Application->MessageBox("The Device doesn't Support the DO function", "Driver Message", MB_OK);
        DRV_DeviceClose(&m_DeviceHandle);
        return;
    }
    ulDataLength = sizeof(DWORD);
    DWORD dwBoardID;
   m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_BoardID,
      &dwBoardID, &ulDataLength);
    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
    if (dwBoardID != BD_PCI1758UDO && dwBoardID != BD_PCI1758UDIO && dwBoardID != BD_MIC3758DIO)
    {
        Application->MessageBox("The Device is not a PCI-1758UDO Device.", "Driver Message", MB_OK);
        DRV_DeviceClose(&m_DeviceHandle);
        return;
    }
    ulDataLength = sizeof(DWORD);
   m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_WatchdogCounter,
      &WatchdogCounter, &ulDataLength);
   ulDataLength = sizeof(BYTE) * 16;
   m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_DoWatchdogValue,
      &(DOWatchdogStatus[0]), &ulDataLength);
   m_ErrCde = DRV_DeviceGetProperty(m_DeviceHandle, CFG_DoPresetValue,
      &(DOpresetStatus[0]), &ulDataLength);
   if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
   szBuff.sprintf("%d",WatchdogCounter);
   txtWatchdogCounter->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[1], DOpresetStatus[0]);
   txtP1->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[3], DOpresetStatus[2]);
   txtP2->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[5], DOpresetStatus[4]);
   txtP3->Text=szBuff;
    szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[7], DOpresetStatus[6]);
  txtP4->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[9], DOpresetStatus[8]);
   txtP5->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[11], DOpresetStatus[10]);
   txtP6->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[13], DOpresetStatus[12]);
   txtP7->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOpresetStatus[15], DOpresetStatus[14]);
   txtP8->Text=szBuff;


    szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[1], DOWatchdogStatus[0]);
   txtWP1->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[3], DOWatchdogStatus[2]);
   txtWP2->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[5], DOWatchdogStatus[4]);
   txtWP3->Text=szBuff;
    szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[7], DOWatchdogStatus[6]);
  txtWP4->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[9], DOWatchdogStatus[8]);
   txtWP5->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[11], DOWatchdogStatus[10]);
   txtWP6->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[13], DOWatchdogStatus[12]);
   txtWP7->Text=szBuff;
   szBuff.sprintf("%2.2X%2.2X", DOWatchdogStatus[15], DOWatchdogStatus[14]);
   txtWP8->Text=szBuff;

    cmbPort->Clear();
   for( i = 0; i< m_DOPortNum ;i++)
   {

      szBuff.sprintf("Port%X", i);
      cmbPort->Items->Add(szBuff);
   }
   cmbPort->ItemIndex = 0;
      GBPresetValue->Enabled = true;
      GBWatchdogSet->Enabled = true;
      GBDO->Enabled = true;
      GBWatchdog->Enabled = true;

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

   PT_DioWritePortByte ptDioWritePortByte ; // DioWritePortByte table
   USHORT chValue ;

   chValue = 0;
   if (chkDI0->Checked)
      chValue = chValue | ( 1 << 0 );
   if (chkDI1->Checked)
      chValue = chValue | ( 1 << 1 );
    if (chkDI2->Checked)
      chValue = chValue |( 1 << 2 );
   if (chkDI3->Checked)
      chValue = chValue | ( 1 << 3 );
   if (chkDI4->Checked)
      chValue = chValue | ( 1 << 4 );
   if (chkDI5->Checked)
      chValue = chValue | ( 1 << 5 );
   if (chkDI6->Checked)
      chValue = chValue | ( 1 << 6 );
   if (chkDI7->Checked)
      chValue = chValue | ( 1 << 7 );

   ptDioWritePortByte.port = cmbPort->ItemIndex;
   ptDioWritePortByte.mask = 0xFF;
   ptDioWritePortByte.state = chValue;

   m_ErrCde = DRV_DioWritePortByte(m_DeviceHandle, &ptDioWritePortByte);
    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnReadClick(TObject *Sender)
{

   PT_DioGetCurrentDOByte ptDioGetCurrentDOByte ; // DioWritePortByte table
   USHORT chValue;


   ptDioGetCurrentDOByte.port = cmbPort->ItemIndex;
   ptDioGetCurrentDOByte.value = &chValue;
   m_ErrCde = DRV_DioGetCurrentDOByte(m_DeviceHandle, &ptDioGetCurrentDOByte);

    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }

   txtPortStatus->Text = IntToHex(chValue,2);
   if ((chValue & (1 << 0)) != 0)
      chkDI0->Checked = true;
   else
      chkDI0->Checked = false;
   if ((chValue & (1 << 1)) != 0) 
      chkDI1->Checked = true;
   else
      chkDI1->Checked = false;
   if ((chValue & (1 << 2)) != 0) 
      chkDI2->Checked = true;
   else
      chkDI2->Checked = false;
   if ((chValue & (1 << 3)) != 0) 
      chkDI3->Checked = true;
   else
      chkDI3->Checked = false;
   if ((chValue & (1 << 4)) != 0) 
      chkDI4->Checked = true;
   else
      chkDI4->Checked = false;
   if ((chValue & (1 << 5)) != 0) 
      chkDI5->Checked = true;
   else
      chkDI5->Checked = false;
   if ((chValue & (1 << 6)) != 0) 
      chkDI6->Checked = true;
   else
      chkDI6->Checked = false;
   if ((chValue & (1 << 6)) != 0) 
      chkDI6->Checked = true;
   else
      chkDI6->Checked = false;
   if ((chValue & (1 << 7)) != 0)
      chkDI7->Checked = true;
   else
      chkDI7->Checked = false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnStartClick(TObject *Sender)
{

   PT_EnableEvent ptEnableEvent; // Enable event
   PT_WatchdogStart ptWatchdogStart;
  WatchdogCounter = StrToInt64(txtWatchdogCounter->Text);
   ulDataLength = sizeof(DWORD);
   DRV_DeviceSetProperty(m_DeviceHandle, CFG_WatchdogCounter,
      &WatchdogCounter, ulDataLength);

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

   ptEnableEvent.EventType = ADS_EVT_WATCHDOG_OVERRUN;
   ptEnableEvent.Enabled = 1;
   ptEnableEvent.Count = 1;
   txtWatchdogStatus->Text = "";

   m_ErrCde = DRV_EnableEvent(m_DeviceHandle, &ptEnableEvent);
    if (m_ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }
   if(chkAutoFeed->Checked)
      AutoFeedTimer->Enabled=true;
   else
      AutoFeedTimer->Enabled=false ;

   if ( pchkThread==NULL )
        pchkThread = new CheckThread(false);

   m_ErrCde = DRV_WatchdogStart(m_DeviceHandle, &ptWatchdogStart);
    if (m_ErrCde != SUCCESS)
    {

⌨️ 快捷键说明

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