init.cpp

来自「16 relay output channels and 16 isolated」· C++ 代码 · 共 630 行 · 第 1/2 页

CPP
630
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "stdio.h"
#include "init.h"
#include "..\..\..\include\driver.h"
#include "CheckForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define     MAX_DEVICES     100
#define     INFINITE        0xFFFFFFFF      // Infinite timeout

static      DEVFEATURES     DevFeatures;    // structure for device features
static      PT_FAIIntStart     ptFAIIntStart;     // FAIIntStart table
static      PT_FAITransfer     ptFAITransfer;     // FAITransfer table
static      PT_DeviceGetFeatures  ptDevFeatures;  // Devfeatures table
static      PT_EnableEvent     ptEnableEvent;     // Enable event
static      PT_CheckEvent      ptCheckEvent;      // Check event

BOOL        bRun = FALSE;                       // flag for running
int      gwDevice = 0, gwSubDevice = 0;      // Device index
USHORT      gwChannel = 0;                      // input channel
USHORT      gwGain = 0;                         // gain index
SHORT       gnNumOfDevices, gnNumOfSubdevices;  // number of installed devices
USHORT      gwScanTime = 1000;                  // scan time
DWORD       gdwStartTime;                       // start time
DWORD       gdwElapseTime = 0;                  // elapse time
USHORT      gwOverrunCount = 0;                 // overrun count

HANDLE      hEvent;
HANDLE      hThreadHandle;
DWORD       dwThreadID;

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

// used for fixed memory for the installed devices
DEVLIST     DeviceList[MAX_DEVICES];
DEVLIST     SubDeviceList[MAX_DEVICES];

static  LONG   DriverHandle = (LONG)NULL;         // driver handle
static  DWORD  gdwPacerRate = 100;                // pacer rate
static  ULONG  gulConvNum = 100;                  // conversion number
static  USHORT gwStartChl = 0;                    // scan channels
static  USHORT gwDataType = 1;                    // display type : FLOAT(1)
static  USHORT gwEvtFlag = 1;                     // event enable(0)
static  USHORT gwGainCode;
static  USHORT gwExtTrig = 0;       // external or internal trigger

static  USHORT gwCyclicMode = 0;    // cyclic or non-cyclic mode

static  USHORT gwOverrun   = 0;     // return by FAICheck, FAITransfer

static  bool gbFifoEnable = false;
static  int giFifoSize = FIFO_SIZE;

HGLOBAL hBuf, hUserBuf;
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
    int         nOutEntries;

    bRun = False;
/*
    ULONG DeviceNum;
    char DeviceName[50];
    HWND hWnd;
    hWnd = Form1->WindowHandle;
    DRV_SelectDevice(hWnd,False,&DeviceNum,DeviceName);
*/

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

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

    // 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;
    }
      butStop->Enabled = False;

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


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

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

    lstOverallGain->Clear();
    gwDevice = 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;
    }

    // initialize Input Range List Combobox with device features
    if (DevFeatures.usNumGain != 0)
    {
        lstOverallGain->Enabled = True;

        for (i = 0; i < DevFeatures.usNumGain; i ++)
           lstOverallGain->Items->Add((LPSTR)DevFeatures.glGainList[i].szGainStr);

        lstOverallGain->ItemIndex = gwGainCode;
    }
    else
    {
        lstOverallGain->Enabled = False;
    }

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

    }

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

    editFifo->Enabled = false;
    gbFifoEnable = false;
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::butStartClick(TObject *Sender)
{

     // get some infromation from Form1
     gwGainCode   = (USHORT)lstOverallGain->ItemIndex;
     gwStartChl   = (USHORT)cmbStartChl->ItemIndex;
     gwEvtFlag    = (USHORT)chkEventEnable->Checked;
     gwCyclicMode = (USHORT)chkCyclic->Checked;
     gwDataType   = (USHORT)chkFloatData->Checked;
     gulConvNum   = (ULONG)atol(editDataCounts->Text.c_str());
     gdwPacerRate = (DWORD)atol(editSpeed->Text.c_str());
     giFifoSize   = atoi(editFifo->Text.c_str());

     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 gain code
     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;
     }

     // allocate memory
     if((hBuf=(USHORT far *)GlobalAlloc(GHND, sizeof(USHORT) * gulConvNum)) == 0)
     {
         lstrcpy((char *) szErrMsg, "Not enough memory for buffer");
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

     // allocate memory
     if((hUserBuf=(FLOAT far *)GlobalAlloc(GHND, sizeof(FLOAT) * gulConvNum)) == 0)
     {
         lstrcpy((char *) szErrMsg, "Not enough memory for buffer");
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

     ptFAIIntStart.buffer     = (USHORT far *)GlobalLock(hBuf);
     ptFAITransfer.DataBuffer = (FLOAT far  *)GlobalLock(hUserBuf);

     // enable event
     ptEnableEvent.EventType = ADS_EVT_INTERRUPT  |
                               ADS_EVT_BUFCHANGE  |
                               ADS_EVT_TERMINATED |
                               ADS_EVT_OVERRUN;
     ptEnableEvent.Enabled = gwEvtFlag;
     if (chkFifo->Checked && giFifoSize > 0)
         ptEnableEvent.Count   = giFifoSize;
     else
         ptEnableEvent.Count   = 1;

     if ((ErrCde = DRV_EnableEvent(DriverHandle,
         (LPT_EnableEvent)&ptEnableEvent)) != 0)
     {
         DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
         GlobalUnlock(hBuf);
         GlobalUnlock(hUserBuf);
         GlobalFree(hBuf);
         GlobalFree(hUserBuf);
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

     ptFAIIntStart.TrigSrc    = gwExtTrig;
     ptFAIIntStart.SampleRate = gdwPacerRate;
     ptFAIIntStart.chan       = gwStartChl;
     ptFAIIntStart.gain       = DevFeatures.glGainList[gwGainCode].usGainCde;
     ptFAIIntStart.count      = gulConvNum;
     ptFAIIntStart.cyclic     = gwCyclicMode;
     if (gbFifoEnable && giFifoSize > 0)
         ptFAIIntStart.IntrCount  = giFifoSize;
     else
         ptFAIIntStart.IntrCount  = 1;

     if ((ErrCde = DRV_FAIIntStart(DriverHandle,
         (LPT_FAIIntStart)&ptFAIIntStart)) != 0)
     {
         DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
         GlobalUnlock(hBuf);
         GlobalUnlock(hUserBuf);
         GlobalFree(hBuf);
         GlobalFree(hUserBuf);
         DRV_DeviceClose((LONG far *)&DriverHandle);
         Application->MessageBox((char *)szErrMsg, "Driver Message", MB_OK);
         return;
     }

     bRun = True;
     butStart->Enabled = False;
     butStop->Enabled  = True;
     butExit->Enabled  = False;
     frmCheck->txtInt->Text=IntToStr(0);
     frmCheck->txtBuffChange->Text=IntToStr(0);

     frmCheck->Show();

     // start thread
     if (gwEvtFlag)
     {
         //Start the background thread
         MyThread = new TMyThread;
     }

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

void __fastcall TfrmMain::butStopClick(TObject *Sender)
{
    LPVOID temp;
    ULONG i;

    if (bRun)
    {
        if ((ErrCde = DRV_FAITerminate(DriverHandle)) != 0)
        {
            DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
            GlobalUnlock(hBuf);

⌨️ 快捷键说明

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