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

📄 diodform.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "DIODForm.h"
#include "stdio.h"
#include "..\..\..\include\driver.h"

#define     MAX_DEVICES     100

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainFrm *MainFrm;
//---------------------------------------------------------------------------
__fastcall TMainFrm::TMainFrm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::FormCreate(TObject *Sender)
{
        //
        // initial default setting value
        //
        m_DiValue = 0;								// read port Word  value
        m_DDiValue = 0;								// read port DWord value
        m_ValidMask = 0;							// read port Word  valid mask
        m_DValidMask = 0;							// read port DWord valid mask
        m_DoState = 0xA55A5AA5;                 // write port data
        m_WriteMask = 0xFFFF;	                // write port Word  mask
        m_DWriteMask = 0xFFFFFFFF;		// write port DWord mask
        m_GetDOValue = 0;
        m_DGetDOValue = 0;
        m_PortNum = 0;
        m_DIPortNum = 0;
        m_DOPortNum = 0;
        m_DIChanNum =0;
        m_DOChanNum = 0;
        m_ChanNum = 0;

        gwCheckBox[0] = chkD0;
        gwCheckBox[1] = chkD1;
        gwCheckBox[2] = chkD2;
        gwCheckBox[3] = chkD3;
        gwCheckBox[4] = chkD4;
        gwCheckBox[5] = chkD5;
        gwCheckBox[6] = chkD6;
        gwCheckBox[7] = chkD7;
        gwCheckBox[8] = chkD8;
        gwCheckBox[9] = chkD9;
        gwCheckBox[10] = chkD10;
        gwCheckBox[11] = chkD11;
        gwCheckBox[12] = chkD12;
        gwCheckBox[13] = chkD13;
        gwCheckBox[14] = chkD14;
        gwCheckBox[15] = chkD15;
        gwCheckBox[16] = chkD16;
        gwCheckBox[17] = chkD17;
        gwCheckBox[18] = chkD18;
        gwCheckBox[19] = chkD19;
        gwCheckBox[20] = chkD20;
        gwCheckBox[21] = chkD21;
        gwCheckBox[22] = chkD22;
        gwCheckBox[23] = chkD23;
        gwCheckBox[24] = chkD24;
        gwCheckBox[25] = chkD25;
        gwCheckBox[26] = chkD26;
        gwCheckBox[27] = chkD27;
        gwCheckBox[28] = chkD28;
        gwCheckBox[29] = chkD29;
        gwCheckBox[30] = chkD30;
        gwCheckBox[31] = chkD31;

        btnSelDevClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::btnSelDevClick(TObject *Sender)
{
   char DeviceName[150];
   DWORD dwreturnCode;

   //
   //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);
        exit(0);
    }

   //
   //Get the Device Feature
   //
   PT_DeviceGetFeatures ptGetFeatures;
   ptGetFeatures.buffer = &m_DeviceFeatures;
   ptGetFeatures.size = sizeof(DEVFEATURES);
   DRV_DeviceGetFeatures(m_DeviceHandle, &ptGetFeatures);

   //
   //Initial Button status
   //
   btnRead->Enabled = TRUE;
   btnDRead->Enabled = TRUE;
   btnWrite->Enabled = TRUE;
   btnDWrite->Enabled = TRUE;
   btnGetD->Enabled = TRUE;
   btnGetDD->Enabled = TRUE;

   //
   //Initial DI channel mode
   //
   m_DIChanNum = m_DeviceFeatures.usMaxDIChl;
   m_DIPortNum = m_DIChanNum/8;
   if (m_DIChanNum%8 != 0)
      m_DIPortNum++;

   if (m_DIChanNum == 0)
   {
        // Set Button status
        btnRead->Enabled = FALSE;
        btnDRead->Enabled = FALSE;

        Application->MessageBox("The Device doesn't Support the DI function", "Driver Message", MB_OK);
   }

   //
   //Initial DO channel mode
   //
   m_DOChanNum = m_DeviceFeatures.usMaxDOChl;
   m_DOPortNum = m_DOChanNum/8;
   if (m_DOChanNum%8 != 0)
      m_DOPortNum++;

   if (m_DOChanNum == 0)
   {
        // Set Button status
        btnWrite->Enabled = FALSE;
        btnDWrite->Enabled = FALSE;
        btnGetD->Enabled = FALSE;
        btnGetDD->Enabled = FALSE;

        // Set Checkbox status
        for( i = 0; i < 32; i++ )
        {
           gwCheckBox[i]->Enabled = FALSE;
        }

        Application->MessageBox("The Device doesn't Support the DO function", "Driver Message", MB_OK);
   }

   if ( m_DOChanNum == 0 && m_DIChanNum == 0 )
   {
        DRV_DeviceClose(&m_DeviceHandle);
        exit(0);
   }
   else if ( m_DIChanNum != 0 )
   {
         m_ChanNum = m_DIChanNum;
         m_PortNum = m_DIPortNum;
   }
   else if ( m_DOChanNum != 0 )
   {
         m_ChanNum = m_DOChanNum;
         m_PortNum = m_DOPortNum;
   }

   if ( m_DOChanNum != 0 )
   {
        if ( m_DOChanNum <= 32 )
        {
                for( i = m_DOChanNum; i < 32; i++ )
                {
                        gwCheckBox[i]->Enabled = FALSE;
                }
                for( i = 0; i < m_DOChanNum;i++ )
                {
                        gwCheckBox[i]->Enabled = TRUE;
                        gwCheckBox[i]->Checked =m_DoState&(0x1<<i);
                }
         }
         else
         {
                for ( i = 0; i < 32; i++ )
                {
                        gwCheckBox[i]->Enabled = TRUE;
                        gwCheckBox[i]->Checked =m_DoState&(0x1<<i);
                }
         }
   }


   cbbPortNum->Clear();
   for ( i = 0; i < m_PortNum ; i++)
   {
     szBuff.sprintf("%d",i);
     cbbPortNum->Items->AddObject(szBuff,NULL);
   }
   cbbPortNum->ItemIndex = 0;

   //
   // Set Write Mode Default Mask
   //
   szBuff.sprintf("%X",m_WriteMask );
   txtWriteMask->Text = szBuff;

   szBuff.sprintf("%X",m_DWriteMask );
   txtWriteDMask->Text = szBuff;
}

void __fastcall TMainFrm::btnExitClick(TObject *Sender)
{
   if ( m_DeviceHandle != NULL )
   {
      DRV_DeviceClose( &m_DeviceHandle );
   }
   
   Application->Terminate();
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::txtKeyPress(TObject *Sender, char &Key)
{
  if(Key >= '0' && Key <= '9')
        return;
  if(Key == 8)
        return;
  if(Key >= 'A' && Key <= 'F')
        return;
  if(Key >= 'a' && Key <= 'f')
  {
    Key = Key + 'A' - 'a';
    return;
  }
  Key = 0;
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::btnWriteClick(TObject *Sender)
{
   // Get Output Value
   int TmpChan;
   m_PortNum = StrToInt(cbbPortNum->Text);
   TmpChan = m_DOChanNum - m_PortNum * 8;

   if ( TmpChan >32 )
   {
        TmpChan = 32;
   }

   m_DoState = 0;

   for ( i = 0; i < TmpChan; i++ )
   {
        if ( gwCheckBox[i]->Checked )
        {
           m_DoState = m_DoState + (0x1<<i);
        }
   }

   // Get Output Word Mask
   szBuff = txtWriteMask->Text;
   m_WriteMask = strtoul(szBuff.c_str(),NULL,16);

   // Output Word
   m_ptDioWritePortWord.port = m_PortNum;
   m_ptDioWritePortWord.mask = m_WriteMask;
   m_ptDioWritePortWord.state = (USHORT)(m_DoState&0xffff);

   m_ErrCde = DRV_DioWritePortWord(
      m_DeviceHandle,
      (LPT_DioWritePortWord)&m_ptDioWritePortWord );

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

void __fastcall TMainFrm::btnDWriteClick(TObject *Sender)
{
   // Get Output Value
   int TmpChan;
   m_PortNum = StrToInt(cbbPortNum->Text);
   TmpChan = m_DOChanNum - m_PortNum * 8;

   if ( TmpChan >32 )
   {
        TmpChan = 32;
   }

   m_DoState = 0;

   for ( i = 0; i < TmpChan; i++ )
   {
        if ( gwCheckBox[i]->Checked )
        {
           m_DoState = m_DoState + (0x1<<i);
        }
   }

   // Get Output DWord Mask
   szBuff = txtWriteDMask->Text;
   m_DWriteMask = strtoul(szBuff.c_str(),NULL,16);

   // Output DWord
   m_ptDioWritePortDword.port = m_PortNum;
   m_ptDioWritePortDword.mask = m_DWriteMask;
   m_ptDioWritePortDword.state = m_DoState;

   m_ErrCde = DRV_DioWritePortDword(
      m_DeviceHandle,
      (LPT_DioWritePortDword)&m_ptDioWritePortDword );

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

void __fastcall TMainFrm::btnGetDClick(TObject *Sender)
{
        //Get current DO word
        m_PortNum = StrToInt(cbbPortNum->Text);
	m_ptDioGetCurrentDOWord.port = m_PortNum;
	m_ptDioGetCurrentDOWord.ValidChannelMask = ( USHORT far * )&m_ValidMask;
	m_ptDioGetCurrentDOWord.value = ( USHORT far * )&m_GetDOValue;
	m_ErrCde = DRV_DioGetCurrentDOWord(
           m_DeviceHandle,
           ( LPT_DioGetCurrentDOWord )&m_ptDioGetCurrentDOWord );
        if ( m_ErrCde != SUCCESS )
        {
            DRV_GetErrorMessage( m_ErrCde, ( LPSTR )szErrMsg );
            Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
            return;
        }

        //Display current DO word value
        szBuff.sprintf("%X",m_GetDOValue);
        txtGetCurrentDO->Text = szBuff;

        //Display current DO word valid mask
        szBuff.sprintf("%X",m_ValidMask);
        txtGetDOMask->Text = szBuff;
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::btnGetDDClick(TObject *Sender)
{
        //Get current DO Dword
        m_PortNum = StrToInt(cbbPortNum->Text);
	m_ptDioGetCurrentDODword.port = m_PortNum;
	m_ptDioGetCurrentDODword.ValidChannelMask = ( ULONG far * )&m_DValidMask;
	m_ptDioGetCurrentDODword.value = ( ULONG far * )&m_DGetDOValue;
	m_ErrCde = DRV_DioGetCurrentDODword(
           m_DeviceHandle,
           ( LPT_DioGetCurrentDODword )&m_ptDioGetCurrentDODword );
        if ( m_ErrCde != SUCCESS )
        {
            DRV_GetErrorMessage( m_ErrCde, ( LPSTR )szErrMsg );
            Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
            return;
        }

        //Display current DO Dword value
        szBuff.sprintf("%X",m_DGetDOValue);
        txtGetCurrentDOD->Text = szBuff;

        //Display current DO word valid mask
        szBuff.sprintf("%X",m_DValidMask);
        txtGetDODMask->Text = szBuff;
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::btnReadClick(TObject *Sender)
{
   // read port word
   m_PortNum = StrToInt(cbbPortNum->Text);
   m_ptDioReadPortWord.port = m_PortNum;
   m_ptDioReadPortWord.ValidChannelMask = ( USHORT far * )&m_ValidMask;
   m_ptDioReadPortWord.value = ( USHORT far * )&m_DiValue;
   m_ErrCde = DRV_DioReadPortWord(
      m_DeviceHandle,
      (LPT_DioReadPortWord)&m_ptDioReadPortWord);
   if ( m_ErrCde != SUCCESS )
   {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
	Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
   }

   // display the value
   szBuff.sprintf("%X",m_DiValue);
   txtReadData->Text = szBuff;

   // display the valid mask.
   szBuff.sprintf("%X",m_ValidMask);
   txtReadMask->Text = szBuff;
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::btnDReadClick(TObject *Sender)
{
   // read port Dword
   m_PortNum = StrToInt(cbbPortNum->Text);
   m_ptDioReadPortDword.port = m_PortNum;
   m_ptDioReadPortDword.ValidChannelMask = ( ULONG far * )&m_DValidMask;
   m_ptDioReadPortDword.value = ( ULONG far * )&m_DDiValue;
   m_ErrCde = DRV_DioReadPortDword(
      m_DeviceHandle,
      (LPT_DioReadPortDword)&m_ptDioReadPortDword);
   if ( m_ErrCde != SUCCESS )
   {
        DRV_GetErrorMessage(m_ErrCde,(LPSTR)szErrMsg);
	Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
   }

   // display the value
   szBuff.sprintf("%X",m_DDiValue);
   txtReadDData->Text = szBuff;

   // display the valid mask.
   szBuff.sprintf("%X",m_DValidMask);
   txtReadDMask->Text = szBuff;
}
//---------------------------------------------------------------------------


void __fastcall TMainFrm::btnSelectDeviceClick(TObject *Sender)
{
   if ( m_DeviceHandle != NULL )
   {
      DRV_DeviceClose( &m_DeviceHandle );
   }
   btnSelDevClick(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TMainFrm::btnPortNum(TObject *Sender)
{
   int TempPortNum;
   m_PortNum = StrToInt(cbbPortNum->Text);
   if( m_DOChanNum != 0 )
   {
        TempPortNum = ( m_DOPortNum - m_PortNum )*8;
        if ( TempPortNum <= 32 )
        {
                for ( i=TempPortNum; i < 32; i++ )
                {
                        gwCheckBox[i]->Enabled = FALSE;
                }
                for (i=0; i < TempPortNum; i++)
                {
                        gwCheckBox[i]->Enabled = TRUE;
                }
        }
        else
        {
                for ( i = 0; i < 32; i++ )
                {
                        gwCheckBox[i]->Enabled = TRUE;
                        gwCheckBox[i]->Checked =m_DoState&(0x1<<i);
                }
        }
   }
}




⌨️ 快捷键说明

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