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

📄 formstar.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "FORMSTAR.h"
#include "FORMRUN.h"
#include "..\..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tfrmstart *frmstart;

const short MaxEntries = 255;
DEVLIST DeviceList[MaxEntries + 1];
DEVLIST SubDeviceList[MaxEntries + 1];
bool bRun;
long ErrCde;
char szErrMsg[80];
long DeviceHandle;
short gnNumOfSubdevices;
DEVFEATURES lpDevFeatures;
PT_DeviceGetFeatures ptDevGetFeatures;
PT_CounterEventStart ptCounterEventStart;
PT_CounterEventRead ptCounterEventRead;

//---------------------------------------------------------------------------
__fastcall Tfrmstart::Tfrmstart(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tfrmstart::FormCreate(TObject *Sender)
{
    short MaxEntries, OutEntries;
    short NumOfDevice;
    int i;

    bRun = False;
    MaxEntries = 100;

    // Add type of PC Laboratory Card
    ErrCde = DRV_DeviceGetList(&DeviceList[0], MaxEntries, &OutEntries);
    if (ErrCde)
    {
        DRV_GetErrorMessage(ErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return;
    }

    // Here MaxEntries = OutEntries
    ErrCde = DRV_DeviceGetNumOfList(&NumOfDevice);
    if (ErrCde)
    {
        DRV_GetErrorMessage(ErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return;
    }

    if (NumOfDevice > 0)
    {
        for (i = 0; i < OutEntries; i++)
        {
            lstDevice->Items->Add(DeviceList[i].szDeviceName);
        }
    }

    labChannel->Enabled = False;
    lstChannel->Enabled = False;
//  labGateMode->Enabled = False;
//  lstGateMode->Enabled = False;

    cmdRun->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall Tfrmstart::lstDeviceClick(TObject *Sender)
{
    int tempNum, i;
    short nOutEntries;
    char tempStr[100];
    long dwDeviceNum;
    bool TestRes;

    lstChannel->Items->Clear();
    lstGateMode->Items->Clear();

    if (lstDevice->Items->Strings[lstDevice->ItemIndex].Pos("DEMO"))
        TestRes = True;
    else
        TestRes = False;

    // Avoid to open Advantech Demo Card
    if (TestRes)
    {
        labModule->Enabled = False;
        lstModule->Enabled = False;
        labChannel->Enabled = False;
        lstChannel->Enabled = False;
        lstChannel->Items->Add("No Use");
        cmdRun->Enabled = False;
    }

    if (!TestRes)
    {
        // Check if there is any device attached on this COM or CAN
        gnNumOfSubdevices = DeviceList[lstDevice->ItemIndex].nNumOfSubdevices;
        if (gnNumOfSubdevices > MaxDev)
            gnNumOfSubdevices = MaxDev;

        dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;

        // COM and CAN bus
        if (gnNumOfSubdevices != 0)
        {
            ErrCde = DRV_DeviceGetSubList(dwDeviceNum, &SubDeviceList[0], gnNumOfSubdevices, &nOutEntries);
            if (ErrCde != 0)
            {
                DRV_GetErrorMessage(ErrCde, szErrMsg);
                Application->MessageBox(szErrMsg, "Error!!", MB_OK);
                return;
            }
            for (i = 0; i < gnNumOfSubdevices; i++)
            {
                lstModule->Items->Add(SubDeviceList[i].szDeviceName);
            }
            lstModule->Enabled = True;
            labModule->Enabled = True;
            labChannel->Enabled = True;
            lstChannel->Enabled = True;
            cmdRun->Enabled = True;
            lstChannel->ItemIndex = 0;
        }

        // PCL DAS & DIO card
        if (gnNumOfSubdevices == 0)
        {
            dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;
            ErrCde = DRV_DeviceOpen(dwDeviceNum, &DeviceHandle);
            if (ErrCde != 0)
            {
                DRV_GetErrorMessage(ErrCde, szErrMsg);
                Application->MessageBox(szErrMsg, "Error!!", MB_OK);
                return;
            }

            ptDevGetFeatures.buffer = &lpDevFeatures;
            ErrCde = DRV_DeviceGetFeatures(DeviceHandle, &ptDevGetFeatures);
            if (ErrCde != 0)
            {
                DRV_GetErrorMessage(ErrCde, szErrMsg);
                Application->MessageBox(szErrMsg, "Error!!", MB_OK);
                return;
            }

            // Add Event Timer channel list
            tempNum = lpDevFeatures.usMaxTimerChl;
            if (tempNum > 0)
            {
                for (i = 0; i < tempNum; i++)
                {
                    strcpy(tempStr, "Chan#");
                    strcat(tempStr, IntToStr(i).c_str());
                    lstChannel->Items->Add(tempStr);
                }

               lstChannel->ItemIndex = 0;
                // Since you have selected a PC-Lab Card, you can choose the channel ans you want
                // Add the selected Items for Gate mode
                lstGateMode->Items->Add("No gating");
                lstGateMode->Items->Add("Active high level");
                lstGateMode->Items->Add("Active low level");
                lstGateMode->Items->Add("Active high edge");
                lstGateMode->Items->Add("Active low edge");
                lstGateMode->ItemIndex = 0;
                labChannel->Enabled = True;
                lstChannel->Enabled = True;
                // The hardware dones not support those function at the present time
                // The gating mode is used for AMD Am9513A only
                labGateMode->Enabled = True;
                lstGateMode->Enabled = True;
                cmdRun->Enabled = True;
            }
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall Tfrmstart::cmdExitClick(TObject *Sender)
{
    if (bRun)
        DRV_DeviceClose(&DeviceHandle);
     Close();
}
//---------------------------------------------------------------------------
void __fastcall Tfrmstart::cmdRunClick(TObject *Sender)
{
    ptCounterEventStart.counter = lstChannel->ItemIndex;
    ptCounterEventStart.GateMode = lstGateMode->ItemIndex;
    ErrCde = DRV_CounterEventStart(DeviceHandle, &ptCounterEventStart);
    if (ErrCde != 0)
    {
        DRV_GetErrorMessage(ErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return;
    }
    frmrun->Show();
    frmrun->CmdRead->SetFocus();
    frmrun->tmrRead->Enabled = True;
}
//---------------------------------------------------------------------------
void __fastcall Tfrmstart::lstModuleClick(TObject *Sender)
{
    int tempNum, i;
    long dwDeviceNum;
    char tempStr[100];

    lstChannel->Items->Clear();
    lstGateMode->Items->Clear();

    // open COM device or CAN device
    dwDeviceNum = SubDeviceList[lstModule->ItemIndex].dwDeviceNum;
    ErrCde = DRV_DeviceOpen(dwDeviceNum, &DeviceHandle);
    if (ErrCde != 0)
    {
        DRV_GetErrorMessage(ErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return;
    }
    else
        bRun = True;

    ptDevGetFeatures.buffer = &lpDevFeatures;
    ErrCde = DRV_DeviceGetFeatures(DeviceHandle, &ptDevGetFeatures);
    if (ErrCde != 0)
    {
        DRV_GetErrorMessage(ErrCde, szErrMsg);
        Application->MessageBox(szErrMsg, "Error!!", MB_OK);
        return;
    }

    // Add Event Timer channel list
    tempNum = lpDevFeatures.usMaxTimerChl;
    if (tempNum > 0)
    {
        for (i = 0; i < tempNum; i++)
        {
            strcpy(tempStr, "Chan#");
            strcat(tempStr, IntToStr(i).c_str());
            lstChannel->Items->Add(tempStr);
        }

        // Since you have selected a PC-Lab Card, you can choose the channel ans you want
        // Add the selected Items for Gate mode. No use in present products
        lstGateMode->Items->Add("No gating");
        lstGateMode->Items->Add("Active high level");
        lstGateMode->Items->Add("Active low level");
        lstGateMode->Items->Add("Active high edge");
        lstGateMode->Items->Add("Active low edge");

        labChannel->Enabled = True;
        lstChannel->Enabled = True;
        // The hardware dones not support those function at the present time
        labGateMode->Enabled = True;
        lstGateMode->Enabled = True;
        cmdRun->Enabled = True;
    }

    // WARNING : No gain code list for ADAM series
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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