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

📄 daform.cpp

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

#include <vcl.h>
#pragma hdrstop

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


#define     MAX_DEVICES     100

static      DEVFEATURES     DevFeatures;    // structure for device features

static      PT_AOVoltageOut  ptAOVoltageOut;  // structure for AOVoltageOut table
static      PT_DeviceGetFeatures  ptDevFeatures;


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
FLOAT       fVoltage;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDASoftForm *DASoftForm;
//---------------------------------------------------------------------------
__fastcall TDASoftForm::TDASoftForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TDASoftForm::FormCreate(TObject *Sender)
{
    int         nOutEntries;
    timer->Enabled = false;


    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;
    scrVoltage->Max = 100;
    scrVoltage->Min = 0;

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

void __fastcall TDASoftForm::timerTimer(TObject *Sender)
{
    ptAOVoltageOut.chan = cmbChan->ItemIndex;
    ptAOVoltageOut.OutputValue = fVoltage;

    if((ErrCde = DRV_AOVoltageOut(DriverHandle,
                 (LPT_AOVoltageOut)&ptAOVoltageOut)) != 0)
    {
       DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
       timer->Enabled = false;

       Application->MessageBox((LPCSTR)szErrMsg, "Driver Message", MB_OK);
    }
    char buff[20];
    sprintf(buff,"%f", fVoltage);
    editVolt->Text=buff ;



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




void __fastcall TDASoftForm::lstDeviceClick(TObject *Sender)
{
      USHORT i;

    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);

    }

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

    // initialize Input Range List Combobox with device features


    // Initialize Start Channel
    cmbChan->Clear();
    for (i = 0 ; i < DevFeatures.usMaxAOChl ; i++)
    {
        itoa((int)i, szBuffer, 10);
        cmbChan->Items->Add((LPSTR)szBuffer);
        cmbChan->ItemIndex = 0;

    }


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

void __fastcall TDASoftForm::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;
    }
    fVoltage = 0;
    
    timer->Enabled = true;
    butStart->Enabled = false;
    butStop->Enabled = true;
    lstDevice->Enabled = false;


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

void __fastcall TDASoftForm::ExitClick(TObject *Sender)
{
    DASoftForm->Close();

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

void __fastcall TDASoftForm::scrVoltageChange(TObject *Sender)
{

    fVoltage = scrVoltage->Position*5.0/100.0;

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

void __fastcall TDASoftForm::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);
     }

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

⌨️ 快捷键说明

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