📄 formstart.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "FormStart.h"
#include "FormRun.h"
#include "..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const MaxEntries = 255;
long DeviceHandle;
DEVLIST DeviceList[MaxEntries + 1];
DEVLIST SubDeviceList[MaxEntries + 1];
DEVFEATURES DevFeatures;
PT_DeviceGetFeatures ptDevGetFeatures;
PT_AIConfig ptAIConfig;
PT_AIGetConfig ptAIGetConfig;
DEVCONFIG_AI Devconfig_AI;
int gnNumOfSubdevices;
int AICtrMode;
long ErrCde;
char szErrMsg[80];
bool bRun;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
int i;
short gnNumOfDevices;
short nOutEntries;
bRun = False;
// Add type of PC Laboratory Card
ErrCde = DRV_DeviceGetList(&DeviceList[0], MaxEntries, &nOutEntries);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
// Return the number of devices which you install in the system using
// Device Installation
ErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
for (i = 0; i < gnNumOfDevices; i++)
{
lstDevice->Items->Add(DeviceList[i].szDeviceName);
}
labModule->Enabled = False;
lstModule->Enabled = False;
labChannel->Enabled = False;
lstChannel->Enabled = False;
labVoltageRange->Enabled = False;
lstVoltageRange->Enabled = False;
cmdRun->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::lstDeviceClick(TObject *Sender)
{
int i;
int tempNum;
char tempStr[30];
bool TestRes;
short nOutEntries;
long dwDeviceNum;
int iMaxSingleChannel;
int iMaxDiffChannel;
lstModule->Clear();
lstChannel->Clear();
lstVoltageRange->Clear();
TestRes = False;
// Avoid to open Advantech Demo Card
/*if (strstr(lstDevice->Items->Text.c_str(), "DEMO"))
TestRes = True;
else
TestRes = False;
if (TestRes)
{
labModule->Enabled = False;
lstModule->Enabled = False;
labChannel->Enabled = False;
lstChannel->Enabled = False;
labVoltageRange->Enabled = False;
lstVoltageRange->Enabled = False;
lstChannel->Items->Add("No Use");
cmdRun->Enabled = False;
} */
if (! TestRes)
{
// Check if there is any device attatched on this COM port or CAN
gnNumOfSubdevices = DeviceList[lstDevice->ItemIndex].nNumOfSubdevices;
if (gnNumOfSubdevices > MaxDev)
{
gnNumOfSubdevices = MaxDev;
}
// retrieve the information of all installed devices
if (gnNumOfSubdevices != 0)
{
dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;
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;
}
// Data Acquisition & Control or Digital I/O 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;
}
else
bRun = True;
ptDevGetFeatures.buffer = (LPDEVFEATURES)&DevFeatures;
ptDevGetFeatures.size = sizeof(DEVFEATURES);
ErrCde = DRV_DeviceGetFeatures(DeviceHandle, &ptDevGetFeatures);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
DRV_DeviceClose((LONG far *)&DeviceHandle);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
ptAIGetConfig.size = sizeof(DEVCONFIG_AI);
ptAIGetConfig.buffer = &Devconfig_AI;
ErrCde = DRV_AIGetConfig(DeviceHandle, &ptAIGetConfig);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
DRV_DeviceClose((LONG far *)&DeviceHandle);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
AICtrMode = Devconfig_AI.usGainCtrMode;
int boardid = Devconfig_AI.dwBoardID;
//tempNum = DevFeatures.usMaxAISiglChl;
//get the max channel number
iMaxSingleChannel = DevFeatures.usMaxAISiglChl;
iMaxDiffChannel = DevFeatures.usMaxAIDiffChl;
if ( iMaxSingleChannel > iMaxDiffChannel )
{
tempNum = iMaxSingleChannel;
}
else
{
tempNum = iMaxDiffChannel;
}
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;
labChannel->Enabled = True;
lstChannel->Enabled = True;
}
// add gain code list
tempNum = DevFeatures.usNumGain;
if (DevFeatures.usNumGain > 0)
{
for (i = 0; i < DevFeatures.usNumGain; i++)
{
lstVoltageRange->Items->Add(DevFeatures.glGainList[i].szGainStr);
}
lstVoltageRange->ItemIndex = 0;
lstVoltageRange->Enabled = True;
labVoltageRange->Enabled = True;
}
cmdRun->Enabled = True;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::lstModuleClick(TObject *Sender)
{
int i;
long dwDeviceNum;
int tempNum;
char tempStr[30];
lstChannel->Clear();
lstVoltageRange->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 = &DevFeatures;
ErrCde = DRV_DeviceGetFeatures(DeviceHandle, &ptDevGetFeatures);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
// Add analog input channel item
ptAIGetConfig.buffer = &Devconfig_AI;
ErrCde = DRV_AIGetConfig(DeviceHandle, &ptAIGetConfig);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
tempNum = DevFeatures.usMaxAISiglChl;
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;
labChannel->Enabled = True;
lstChannel->Enabled = True;
}
// add gain code list
tempNum = DevFeatures.usNumGain;
if (DevFeatures.usNumGain > 0)
{
for (i = 0; i < DevFeatures.usNumGain; i++)
{
lstVoltageRange->Items->Add(DevFeatures.glGainList[i].szGainStr);
}
lstVoltageRange->ItemIndex = 0;
lstVoltageRange->Enabled = True;
labVoltageRange->Enabled = True;
}
cmdRun->Enabled = True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cmdRunClick(TObject *Sender)
{
int tempNum;
tempNum = lstVoltageRange->ItemIndex;
AICtrMode = Devconfig_AI.usGainCtrMode;
ptAIConfig.DasChan = lstChannel->ItemIndex;
// Gain code no use for ADAM series
if (gnNumOfSubdevices == 0)
{
ptAIConfig.DasGain = DevFeatures.glGainList[tempNum].usGainCde;
}
ErrCde = DRV_AIConfig(DeviceHandle, &ptAIConfig);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
return;
}
Form1->Hide();
Application->CreateForm(__classid(TForm2), &Form2);
Form2->Show();
Form2->CmdRead->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cmdExitClick(TObject *Sender)
{
if (bRun)
{
ErrCde = DRV_DeviceClose(&DeviceHandle);
if (ErrCde != 0)
{
DRV_GetErrorMessage(ErrCde, szErrMsg);
Application->MessageBox(szErrMsg, "Error!!", MB_OK);
}
}
Application->Terminate();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -