pulseform.cpp
来自「16 relay output channels and 16 isolated」· C++ 代码 · 共 280 行
CPP
280 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "PulseForm.h"
#include "RunForm.h"
#include "..\..\..\include\driver.h"
const int MAX_DEVICES =100;
DEVFEATURES DevFeatures; // structure for device features
PT_DeviceGetFeatures ptDevFeatures;
SHORT gwDevice, gwSubDevice; // Device index
USHORT gnNumOfDevices = 0, gnNumOfSubdevices = 0; // number of installed devices
char szErrMsg[80]; // Use for MESSAGEBOXA function
char szBuffer[40]; // Temperatory buffer
LRESULT ErrCde; // Return error code
DEVLIST DeviceList[MAX_DEVICES];
DEVLIST SubDeviceList[MAX_DEVICES];
LONG lDriverHandle = (LONG)NULL; // driver handle
BOOL bRun;
PT_CounterPulseStart ptCounterPulseStart;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmStart *frmStart;
//---------------------------------------------------------------------------
__fastcall TfrmStart::TfrmStart(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmStart::FormCreate(TObject *Sender)
{
SHORT MaxEntries, OutEntries;
int i, j;
AnsiString tempStr;
bRun = False;
// Add type of PC Laboratory Card
ErrCde = DRV_DeviceGetList(DeviceList, MAX_DEVICES, &OutEntries);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg, "Error!!");
return;
}
ErrCde = DRV_DeviceGetNumOfList(&MaxEntries);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg, "Error!!");
return;
}
if (MaxEntries > 0) {
for(i=0; i<MaxEntries; i++){
tempStr = "";
for(j= 0; j<MaxDevNameLen; j++){
tempStr = tempStr + DeviceList[i].szDeviceName[j];
}
lstDevice->Items->Add(tempStr);
}
}
labChannel->Enabled = False;
lstChannel->Enabled = False;
labGateMode->Enabled = False;
lstGateMode->Enabled = False;
cmdRun->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TfrmStart::lstDeviceClick(TObject *Sender)
{
int tempNum, i, ii;
SHORT nOutEntries;
BOOL TestRes;
AnsiString tempStr;
LONG dwDeviceNum;
lstChannel->Items->Clear();
lstGateMode->Items->Clear();
tempNum = lstDevice->Items->Strings[lstDevice->ItemIndex].Pos("DEMO");
if (tempNum != 0){
TestRes = True;
}else{
TestRes = False;
}
// Avoid to open Advantech Demo Card
if (True == TestRes){
labModule->Enabled = False;
lstModule->Enabled = False;
labChannel->Enabled = False;
lstChannel->Enabled = False;
lstChannel->Items->Add("No Use");
cmdRun->Enabled = False;
}
if (False == 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, gnNumOfSubdevices, &nOutEntries);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg, "Error!!");
return;
}
for(i=0; i<gnNumOfSubdevices; i++){
tempStr = "";
for(ii=0; ii<MaxDevNameLen; ii++){
tempStr = tempStr + SubDeviceList[i].szDeviceName[ii];
}
lstModule->Items->Add(tempStr);
}
lstModule->Enabled = True;
labModule->Enabled = True;
labChannel->Enabled = True;
lstChannel->Enabled = True;
lstChannel->ItemIndex = 0;
cmdRun->Enabled = True;
}
// PCL DAS & DIO card
if (0 == gnNumOfSubdevices){
dwDeviceNum = DeviceList[lstDevice->ItemIndex].dwDeviceNum;
ErrCde = DRV_DeviceOpen(dwDeviceNum, &lDriverHandle);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char* )szErrMsg, "Error!!");
return;
}else{
bRun = True;
ptDevFeatures.buffer = &DevFeatures;
ErrCde = DRV_DeviceGetFeatures(lDriverHandle, &ptDevFeatures);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char* )szErrMsg, "Error!!");
return;
}
// Add Event Timer channel list }
tempNum = DevFeatures.usMaxTimerChl;
if (tempNum > 0){
for(i=0; i<tempNum; i++){
tempStr = "Chan#" + AnsiString(i);
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
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;
lstModule->Enabled = False;
lstChannel->ItemIndex = 0;
//lstGateMode->Enabled = True;
//The hardware dones not support those function at the present time.
// The gating mode is used for AMD Am9513A only
cmdRun->Enabled = True;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmStart::lstModuleClick(TObject *Sender)
{
int tempNum, i;
Longint dwDeviceNum;
AnsiString tempStr;
lstChannel->Items->Clear();
lstGateMode->Items->Clear();
//open COM device or CAN device
dwDeviceNum = SubDeviceList[lstModule->ItemIndex].dwDeviceNum;
ErrCde = DRV_DeviceOpen(dwDeviceNum, &lDriverHandle);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg, "Error!!");
return;
}else{
bRun = True;
}
ptDevFeatures.buffer = &DevFeatures;
ErrCde = DRV_DeviceGetFeatures(lDriverHandle, &ptDevFeatures);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char*)szErrMsg, "Error!!");
return;
}
// Add Event Timer channel list
tempNum = DevFeatures.usMaxTimerChl;
if (tempNum > 0){
for(i=0; i<tempNum; i++){
tempStr = "Chan#" + AnsiString(i);
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;
lstChannel->ItemIndex = 0;
// 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
}
//---------------------------------------------------------------------------
void __fastcall TfrmStart::cmdRunClick(TObject *Sender)
{
ptCounterPulseStart.counter = lstChannel->ItemIndex;
ptCounterPulseStart.period = 0.001; // period = 1 ms
ptCounterPulseStart.UpCycle = 0.0005;
//ptCounterPulseStart.GateMode := lstGateMode.ItemIndex;} // Gate mode no use here
ErrCde = DRV_CounterPulseStart(lDriverHandle, &ptCounterPulseStart);
if (ErrCde != SUCCESS){
DRV_GetErrorMessage(ErrCde, (LPSTR)szErrMsg);
Application->MessageBoxA((char* )szErrMsg, "Error!!");
return;
}
frmRun->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TfrmStart::FormClose(TObject *Sender, TCloseAction &Action)
{
if(True == bRun){
DRV_DeviceClose(&lDriverHandle);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmStart::cmdExitClick(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?