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

📄 digform.cpp

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

#include <vcl.h>
#pragma hdrstop

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


#define     MAX_DEVICES     100

static      DEVFEATURES     DevFeatures;    // structure for device features

static      PT_DioReadPortByte  ptDioReadPortByte;  // structure for DioReadPortByte table
static      PT_DeviceGetFeatures  ptDevFeatures;
static      PT_DioSetPortMode  ptDioPortMode;


USHORT      gwDevice = 0, gwSubDevice = 0;      // Device index

SHORT       gnNumOfDevices, gnNumOfSubdevices;  // number of installed devices

char        szErrMsg[80];               // Use for MESSAGEBOX function
char        szBuffer[40];	       	// Temperatory buffer
LRESULT     ErrCde;                     // Return error code

DEVLIST     DeviceList[MAX_DEVICES];
DEVLIST     SubDeviceList[MAX_DEVICES];
static  LONG   DriverHandle = (LONG)NULL;         // driver handle

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDISOFTForm *DISOFTForm;
//---------------------------------------------------------------------------
__fastcall TDISOFTForm::TDISOFTForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TDISOFTForm::FormCreate(TObject *Sender)
{
    int         nOutEntries;
    timer->Enabled = false;
    imgBit0->Picture->LoadFromFile("GrayLED.bmp");
    imgBit1->Picture->LoadFromFile("GrayLED.bmp");
    imgBit2->Picture->LoadFromFile("GrayLED.bmp");
    imgBit3->Picture->LoadFromFile("GrayLED.bmp");
    imgBit4->Picture->LoadFromFile("GrayLED.bmp");
    imgBit5->Picture->LoadFromFile("GrayLED.bmp");
    imgBit6->Picture->LoadFromFile("GrayLED.bmp");
    imgBit7->Picture->LoadFromFile("GrayLED.bmp");

    if ((ErrCde = DRV_DeviceGetNumOfList((SHORT far *)&gnNumOfDevices)) !=
        SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        exit(0);
    }

    if (gnNumOfDevices > MAX_DEVICES)
        gnNumOfDevices = MAX_DEVICES;

    // Add type of PC Laboratory Card
    if ((ErrCde = DRV_DeviceGetList((DEVLIST far *) &DeviceList[0],
        (SHORT)gnNumOfDevices, (SHORT far *)&nOutEntries)) != (LONG)SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        exit(0);
    }

    // Here MaxEntries = nOutEntries
    for(int i = 0; i < gnNumOfDevices ; i++)
        lstDevice->Items->Add((LPSTR)DeviceList[i].szDeviceName);
    if(gnNumOfDevices <=0)
     {
        Application->MessageBox("No Installed Device!", "Driver Message", MB_OK);
        butStart->Enabled = False;
         butStop->Enabled = False;
         return;
      }   

    lstDevice->ItemIndex = gwDevice;
    lstDeviceClick(Sender);

    butStop->Enabled = False;


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





void __fastcall TDISOFTForm::lstDeviceClick(TObject *Sender)
{
      USHORT i;
      PT_DioReadBit pDioReadBit;    //add by mining
      pDioReadBit.port=0;
      pDioReadBit.bit=0;//end by mining

    gwDevice = (USHORT)lstDevice->ItemIndex;

    ErrCde = DRV_DeviceOpen(DeviceList[gwDevice].dwDeviceNum,
             (LONG far *)&DriverHandle);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }

    // get device features
    ptDevFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
    ptDevFeatures.size = sizeof(DEVFEATURES);
    if ((ErrCde = DRV_DeviceGetFeatures(DriverHandle,
        (LPT_DeviceGetFeatures)&ptDevFeatures)) != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        DRV_DeviceClose((LONG far *)&DriverHandle);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        return;
    }

    //add by mining 
     //judge whether it supports DI function     
    if (ptDevFeatures.buffer->usMaxDIChl==0)
    {
        ErrCde = DRV_DioReadBit( DriverHandle ,&pDioReadBit);
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        butStart->Enabled = false;
        butStop->Enabled = false;
        lstDevice->Enabled = true;
        timer->Enabled = false;
        return;
    }
    else
    {
       butStart->Enabled = true;
    } //end by mining

    // initialize Input Range List Combobox with device features


    // Initialize Start Channel
    lstPort->Clear();
    for (i = 0 ; i <(DevFeatures.usMaxDIChl+7)/8 ; i++)
    {
        sprintf(szBuffer,"Port %d", i);
        lstPort->Items->Add((LPSTR)szBuffer);
        lstPort->ItemIndex = 0;

    }


    // close device
    DRV_DeviceClose((LONG far *)&DriverHandle);
}
//---------------------------------------------------------------------------

void __fastcall TDISOFTForm::butStartClick(TObject *Sender)
{
    if (gnNumOfSubdevices == 0)
        ErrCde = DRV_DeviceOpen(
            DeviceList[gwDevice].dwDeviceNum,
                (LONG far *)&DriverHandle);
    else
        ErrCde = DRV_DeviceOpen(
            SubDeviceList[gwSubDevice].dwDeviceNum,
                (LONG far *)&DriverHandle);

    if (ErrCde != SUCCESS)
    {
        strcpy(szErrMsg,"Device open error !");
        Application->MessageBox((LPCSTR)szErrMsg,"Device Open",MB_OK);
        return;
    }

    ptDioPortMode.port = lstPort->ItemIndex;
    ptDioPortMode.dir = INPORT;

    // not every digital I/O card could use DRV_DioSetPortMode function
    if (DevFeatures.usDIOPort > 0)
    {
        ErrCde = DRV_DioSetPortMode(DriverHandle, &ptDioPortMode);
        if (ErrCde)
        {
            DRV_GetErrorMessage(ErrCde, szErrMsg);
            Application->MessageBox(szErrMsg, "Error!!", MB_OK);
            return;
        }
    }
    
    timer->Enabled = true;
    butStart->Enabled = false;
    butStop->Enabled = true;
    lstDevice->Enabled = false;


}


//---------------------------------------------------------------------------


void __fastcall TDISOFTForm::butStopClick(TObject *Sender)
{
    butStart->Enabled = true;
    butStop->Enabled = false;
    lstDevice->Enabled = true;
    timer->Enabled = false;

   ErrCde = DRV_DeviceClose((LONG far *)&DriverHandle);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
     }

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

void __fastcall TDISOFTForm::butExitClick(TObject *Sender)
{

    DISOFTForm->Close();
}
//---------------------------------------------------------------------------

void __fastcall TDISOFTForm::timerTimer(TObject *Sender)
{
   USHORT DigValue;
   ptDioReadPortByte.port = lstPort->ItemIndex;
   ptDioReadPortByte.value = &DigValue;

    ErrCde = DRV_DioReadPortByte( DriverHandle ,(LPT_DioReadPortByte)&ptDioReadPortByte);
    if (ErrCde != SUCCESS)
    {
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        timer->Enabled = false;//adjust the sequence  by mining
        Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
        butStart->Enabled = true;
        butStop->Enabled = false;
        lstDevice->Enabled = true;
        //timer->Enabled = false;
     }

     char Gray[40]="GrayLED.bmp";
     char Red[40]="RedLED.bmp";


    if( DigValue & 0x1)
        imgBit0->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit0->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x2)
        imgBit1->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit1->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x4)
        imgBit2->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit2->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x8)
        imgBit3->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit3->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x10)
        imgBit4->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit4->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x20)
        imgBit5->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit5->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x40)
        imgBit6->Picture->LoadFromFile("RedLED.bmp");

    else
       imgBit6->Picture->LoadFromFile("GrayLED.bmp");

    if( DigValue & 0x80)
        imgBit7->Picture->LoadFromFile("RedLED.bmp");
    else
       imgBit7->Picture->LoadFromFile("GrayLED.bmp");


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

⌨️ 快捷键说明

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